fix: revert to cedlab_Chart_UL1 (already has all V2 fields); remove cedlab_Chart_UL2; fix AllSeriesV2 SQL with DailyUL1 logic
This commit is contained in:
@@ -3,18 +3,19 @@ GO
|
||||
|
||||
-- ============================================================
|
||||
-- SP: cedlab_Chart_AllSeriesV2
|
||||
-- Restituisce tutte le serie (CTF + UL) in un unico round-trip.
|
||||
-- Restituisce tutte le serie (CTF + tutti UL) in un unico round-trip.
|
||||
-- Usata da ChartDataServiceV2 per il grafico V2.
|
||||
--
|
||||
-- Logica UL: identica a cedlab_Chart_DailyUL1, estesa a tutti i
|
||||
-- sottostanti del certificato in un unico UNION ALL.
|
||||
-- sottostanti del certificato tramite INNER JOIN su dbo.Underlyings.
|
||||
--
|
||||
-- Output (ordinato IDUnderlyings ASC, Px_date ASC):
|
||||
-- IDUnderlyings INT -- 0 = CTF, altrimenti UnderlyingsID
|
||||
-- Px_date DATE
|
||||
-- Performance DECIMAL -- % su strike/nominal
|
||||
--
|
||||
-- Colonne con <<< verify: adatta al tuo schema reale.
|
||||
-- Note: cedlab_Chart_UL1 già restituisce tutti i campi metadata
|
||||
-- (IsWorstOf, PriceWorst, Barriere, NomeCFT, ecc.) — non serve una nuova SP.
|
||||
-- ============================================================
|
||||
|
||||
CREATE OR ALTER PROCEDURE [dbo].[cedlab_Chart_AllSeriesV2]
|
||||
@@ -23,32 +24,42 @@ AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON;
|
||||
|
||||
-- Recupera IDCertificates
|
||||
DECLARE @IDCertificates INT;
|
||||
DECLARE @Nominal DECIMAL(18, 6);
|
||||
|
||||
SELECT TOP 1
|
||||
@IDCertificates = c.IDCertificates, -- <<< verify
|
||||
@Nominal = c.Nominal_amount -- <<< verify: valore nominale per CTF %
|
||||
FROM [dbo].[Certificates] c -- <<< verify: nome tabella certificati
|
||||
WHERE c.ISIN = @isin;
|
||||
@IDCertificates = p.CertificatesID
|
||||
FROM dbo.Prices p
|
||||
INNER JOIN dbo.Certificates c ON c.IDCertificates = p.CertificatesID -- <<< verify: tabella e colonna ISIN in Certificates
|
||||
WHERE c.ISIN = @isin
|
||||
AND p.PX_LAST_EOD IS NOT NULL
|
||||
AND p.PX_LAST_EOD <> 0;
|
||||
|
||||
-- Nominal per calcolo performance CTF: adatta se usi un'altra colonna
|
||||
SELECT TOP 1
|
||||
@Nominal = c.Nominal_amount -- <<< verify: colonna nominale in Certificates
|
||||
FROM dbo.Certificates c
|
||||
WHERE c.IDCertificates = @IDCertificates;
|
||||
|
||||
IF @IDCertificates IS NULL RETURN;
|
||||
|
||||
-- ── CTE: calcola performance per ogni serie, TOP 350 per-serie ─────
|
||||
-- ── CTE: TOP 350 per-serie, poi reinverte in ASC ───────────────────
|
||||
WITH AllSeriesRaw AS
|
||||
(
|
||||
-- ── Serie CTF (IDUnderlyings = 0) ────────────────────────────────
|
||||
-- Performance = PX_LAST_EOD / Nominal * 100
|
||||
SELECT
|
||||
0 AS IDUnderlyings,
|
||||
-- (stessa logica di cedlab_Chart_DailyCTF)
|
||||
SELECT TOP 350
|
||||
0 AS IDUnderlyings,
|
||||
pc.Px_date,
|
||||
CONVERT(DECIMAL(18, 4),
|
||||
pc.PX_LAST_EOD / NULLIF(@Nominal, 0) * 100
|
||||
) AS Performance,
|
||||
) AS Performance,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY 0
|
||||
ORDER BY pc.Px_date DESC
|
||||
) AS rn
|
||||
) AS rn
|
||||
FROM dbo.Prices pc
|
||||
WHERE pc.CertificatesID = @IDCertificates
|
||||
AND pc.PX_LAST_EOD IS NOT NULL
|
||||
@@ -56,11 +67,12 @@ BEGIN
|
||||
|
||||
UNION ALL
|
||||
|
||||
-- ── Serie UL (una per ogni sottostante) ──────────────────────────
|
||||
-- Stessa logica di cedlab_Chart_DailyUL1, estesa a tutti gli UL.
|
||||
-- ── Serie UL (tutti i sottostanti del certificato) ────────────────
|
||||
-- Performance = (Px_close o Px_closeadj) / Strike * 100
|
||||
-- Identica a cedlab_Chart_DailyUL1 ma per TUTTI gli UL in un colpo.
|
||||
-- Date allineate al CTF (INNER JOIN su Px_date).
|
||||
SELECT
|
||||
pu.UnderlyingsID AS IDUnderlyings,
|
||||
SELECT TOP 350
|
||||
pu.UnderlyingsID AS IDUnderlyings,
|
||||
pu.Px_date,
|
||||
CONVERT(DECIMAL(18, 4),
|
||||
CASE
|
||||
@@ -68,29 +80,28 @@ BEGIN
|
||||
ELSE pu.Px_closeadj
|
||||
END
|
||||
/ NULLIF(u.Strike, 0) * 100
|
||||
) AS Performance,
|
||||
) AS Performance,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY pu.UnderlyingsID
|
||||
ORDER BY pu.Px_date DESC
|
||||
) AS rn
|
||||
) AS rn
|
||||
FROM dbo.Prices pu
|
||||
LEFT JOIN dbo.Underlyings u
|
||||
ON u.IDUnderlyings = pu.UnderlyingsID
|
||||
-- Allinea date UL al CTF (solo date presenti nel certificato)
|
||||
-- Allinea date UL al CTF (solo date con prezzo CTF valido)
|
||||
INNER JOIN dbo.Prices pc
|
||||
ON pu.Px_date = pc.Px_date
|
||||
AND pc.CertificatesID = @IDCertificates
|
||||
AND pc.PX_LAST_EOD IS NOT NULL
|
||||
AND pc.PX_LAST_EOD <> 0
|
||||
WHERE u.IDCertificates = @IDCertificates -- <<< verify: colonna FK in Underlyings
|
||||
WHERE u.IDCertificates = @IDCertificates
|
||||
AND (
|
||||
pu.px_low IS NOT NULL OR pu.Px_high IS NOT NULL OR
|
||||
pu.Px_open IS NOT NULL OR pu.Px_close IS NOT NULL
|
||||
)
|
||||
AND pu.px_date >= u.StartDate -- <<< verify: colonna StartDate in Underlyings
|
||||
AND pu.px_date >= u.StartDate -- <<< verify: colonna StartDate in Underlyings
|
||||
)
|
||||
|
||||
-- ── Filtra TOP 350 per serie e reinverte in ordine ASC ───────────────
|
||||
SELECT
|
||||
IDUnderlyings,
|
||||
Px_date,
|
||||
|
||||
Reference in New Issue
Block a user