fix: rewrite cedlab_Chart_AllSeriesV2 with correct schema from cedlab_Chart_UL1 (CertificatesUnderlyings.Strike, StartDate=MIN(Prices.Px_date), deleted/sospeso filters)

This commit is contained in:
2026-05-27 17:17:32 +02:00
parent e4728cf79e
commit 679f9e4528

View File

@@ -6,51 +6,46 @@ GO
-- Restituisce tutte le serie (CTF + tutti 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. -- Usata da ChartDataServiceV2 per il grafico V2.
-- --
-- Logica UL: identica a cedlab_Chart_DailyUL1, estesa a tutti i -- Logica basata su:
-- sottostanti del certificato tramite INNER JOIN su dbo.Underlyings. -- - cedlab_Chart_DailyUL1: stessa formula performance UL, stessi filtri
-- - cedlab_Chart_UL1: Strike da CertificatesUnderlyings, StartDate = MIN(Prices.Px_date)
-- --
-- Output (ordinato IDUnderlyings ASC, Px_date ASC): -- Output (IDUnderlyings ASC, Px_date ASC):
-- IDUnderlyings INT -- 0 = CTF, altrimenti UnderlyingsID -- IDUnderlyings INT -- 0 = CTF, altrimenti UnderlyingsID
-- Px_date DATE -- Px_date DATE
-- Performance DECIMAL -- % su strike/nominal -- Performance DECIMAL -- % su strike/nominal
--
-- 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] CREATE OR ALTER PROCEDURE [dbo].[cedlab_Chart_AllSeriesV2]
@isin NVARCHAR(12) @isin VARCHAR(15)
AS AS
BEGIN BEGIN
SET NOCOUNT ON; SET NOCOUNT ON;
-- Recupera IDCertificates
DECLARE @IDCertificates INT; DECLARE @IDCertificates INT;
DECLARE @Nominal DECIMAL(18, 6); DECLARE @Nominal DECIMAL(18, 6);
DECLARE @StartDate DATE;
SELECT TOP 1 SELECT TOP 1
@IDCertificates = p.CertificatesID @IDCertificates = c.IDCertificates,
FROM dbo.Prices p @Nominal = c.Nominal_amount
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 FROM dbo.Certificates c
WHERE c.IDCertificates = @IDCertificates; WHERE c.ISIN = @isin;
IF @IDCertificates IS NULL RETURN; IF @IDCertificates IS NULL RETURN;
-- ── CTE: TOP 350 per-serie, poi reinverte in ASC ─────────────────── -- StartDate = prima data di prezzi EOD del certificato
-- (identica a cedlab_Chart_UL1: MIN(p.Px_date) con join su Prices)
SELECT @StartDate = MIN(p.Px_date)
FROM dbo.Prices p
WHERE p.CertificatesID = @IDCertificates;
-- ── CTE: TOP 350 per-serie tramite ROW_NUMBER DESC, poi reinverte ASC ──
WITH AllSeriesRaw AS WITH AllSeriesRaw AS
( (
-- ── Serie CTF (IDUnderlyings = 0) ──────────────────────────────── -- ── Serie CTF (IDUnderlyings = 0) ────────────────────────────────
-- Performance = PX_LAST_EOD / Nominal * 100 -- Performance = PX_LAST_EOD / Nominal_amount * 100
-- (stessa logica di cedlab_Chart_DailyCTF) SELECT
SELECT TOP 350
0 AS IDUnderlyings, 0 AS IDUnderlyings,
pc.Px_date, pc.Px_date,
CONVERT(DECIMAL(18, 4), CONVERT(DECIMAL(18, 4),
@@ -68,10 +63,10 @@ BEGIN
UNION ALL UNION ALL
-- ── Serie UL (tutti i sottostanti del certificato) ──────────────── -- ── Serie UL (tutti i sottostanti del certificato) ────────────────
-- Performance = (Px_close o Px_closeadj) / Strike * 100 -- Identica a cedlab_Chart_DailyUL1 per ogni UL attivo.
-- Identica a cedlab_Chart_DailyUL1 ma per TUTTI gli UL in un colpo. -- Strike da CertificatesUnderlyings (come in cedlab_Chart_UL1).
-- Date allineate al CTF (INNER JOIN su Px_date). -- Date allineate al CTF (INNER JOIN su Prices per CertificatesID).
SELECT TOP 350 SELECT
pu.UnderlyingsID AS IDUnderlyings, pu.UnderlyingsID AS IDUnderlyings,
pu.Px_date, pu.Px_date,
CONVERT(DECIMAL(18, 4), CONVERT(DECIMAL(18, 4),
@@ -79,7 +74,7 @@ BEGIN
WHEN ISNULL(u.AdjustedPrices, 0) = 0 THEN pu.Px_close WHEN ISNULL(u.AdjustedPrices, 0) = 0 THEN pu.Px_close
ELSE pu.Px_closeadj ELSE pu.Px_closeadj
END END
/ NULLIF(u.Strike, 0) * 100 / NULLIF(cu.Strike, 0) * 100
) AS Performance, ) AS Performance,
ROW_NUMBER() OVER ( ROW_NUMBER() OVER (
PARTITION BY pu.UnderlyingsID PARTITION BY pu.UnderlyingsID
@@ -88,18 +83,23 @@ BEGIN
FROM dbo.Prices pu FROM dbo.Prices pu
LEFT JOIN dbo.Underlyings u LEFT JOIN dbo.Underlyings u
ON u.IDUnderlyings = pu.UnderlyingsID ON u.IDUnderlyings = pu.UnderlyingsID
INNER JOIN dbo.CertificatesUnderlyings cu
ON cu.UnderlyingsID = pu.UnderlyingsID
AND cu.CertificatesID = @IDCertificates
-- Allinea date UL al CTF (solo date con prezzo CTF valido) -- Allinea date UL al CTF (solo date con prezzo CTF valido)
INNER JOIN dbo.Prices pc INNER JOIN dbo.Prices pc
ON pu.Px_date = pc.Px_date ON pu.Px_date = pc.Px_date
AND pc.CertificatesID = @IDCertificates AND pc.CertificatesID = @IDCertificates
AND pc.PX_LAST_EOD IS NOT NULL AND pc.PX_LAST_EOD IS NOT NULL
AND pc.PX_LAST_EOD <> 0 AND pc.PX_LAST_EOD <> 0
WHERE u.IDCertificates = @IDCertificates WHERE cu.deleted = 0
AND u.deleted = 0
AND u.sospeso = 0
AND ( AND (
pu.px_low IS NOT NULL OR pu.Px_high IS NOT NULL OR 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 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 >= @StartDate
) )
SELECT SELECT