USE [FirstSolutionDB] GO -- ============================================================ -- SP: cedlab_Chart_UnderlyingOnly -- Restituisce lo storico prezzi di UN SOLO sottostante, senza -- alcun contesto certificato (nessuno strike/barriera/trigger). -- Usata da UnderlyingChartDataService per il grafico standalone -- sottostante. -- -- @Years: ampiezza storico in anni (default 5). -- -- Output (Px_date ASC, ultimi @Years anni): -- Nome VARCHAR -- "{Name} ({Ticker_bbg})", uguale su ogni riga -- Px_date DATE -- Px DECIMAL -- Px_closeadj se AdjustedPrices=1, altrimenti -- Px_close. Nessun fallback incrociato tra i due -- campi: evita il gradino artificiale quando -- Px_closeadj non รจ backfillato prima di uno split -- ============================================================ CREATE OR ALTER PROCEDURE [dbo].[cedlab_Chart_UnderlyingOnly] @IDUnderlyings INT, @Years INT = 5 AS BEGIN SET NOCOUNT ON; DECLARE @AdjustedPrices BIT; SELECT @AdjustedPrices = ISNULL(AdjustedPrices, 0) FROM dbo.Underlyings WHERE IDUnderlyings = @IDUnderlyings AND deleted = 0 AND sospeso = 0; IF @AdjustedPrices IS NULL RETURN; -- non trovato / cancellato / sospeso DECLARE @FromDate DATE = DATEADD(YEAR, -@Years, CAST(GETDATE() AS DATE)); SELECT CONCAT(u.Name, ' (', u.Ticker_bbg, ')') AS Nome, p.Px_date, CASE WHEN @AdjustedPrices = 0 THEN p.Px_close ELSE p.Px_closeadj END AS Px FROM dbo.Prices p CROSS JOIN (SELECT TOP 1 * FROM dbo.Underlyings WHERE IDUnderlyings = @IDUnderlyings) u WHERE p.UnderlyingsID = @IDUnderlyings AND p.Px_date >= @FromDate AND (CASE WHEN @AdjustedPrices = 0 THEN p.Px_close ELSE p.Px_closeadj END) IS NOT NULL ORDER BY p.Px_date ASC; END GO