91 lines
3.7 KiB
Transact-SQL
91 lines
3.7 KiB
Transact-SQL
-- ============================================================
|
|
-- SP: cedlab_Chart_UL1
|
|
-- Metadata dei sottostanti per il grafico V2.
|
|
-- Estende FSWeb_Chart_UL con:
|
|
-- IsWorstOf, PriceWorst, PriceWorstPerc,
|
|
-- NumPrezziCFT, NomeCFT,
|
|
-- BarrieraCouponPerc, BarrieraCapitalePerc,
|
|
-- TriggerAutocallPerc, AutocallValue
|
|
--
|
|
-- Ordinato: IsWorstOf DESC (worst-of nella prima riga)
|
|
--
|
|
-- NOTA: se questa SP esiste già nel DB con questo nome e restituisce
|
|
-- già tutte queste colonne, questo file è solo documentazione.
|
|
-- Eseguilo solo se la SP non esiste o mancano le colonne.
|
|
--
|
|
-- ADATTAMENTO RICHIESTO: stessi nomi tabella del file cedlab_Chart_AllSeriesV2.sql
|
|
-- ============================================================
|
|
|
|
USE [FirstSolutionDB]
|
|
GO
|
|
|
|
CREATE OR ALTER PROCEDURE [dbo].[cedlab_Chart_UL1]
|
|
@isin NVARCHAR(12)
|
|
AS
|
|
BEGIN
|
|
SET NOCOUNT ON;
|
|
|
|
-- Numero prezzi CTF disponibili (usato per il sub-titolo)
|
|
DECLARE @NumPrezziCFT INT;
|
|
DECLARE @IDCertificates INT;
|
|
DECLARE @Nominal DECIMAL(18,6);
|
|
DECLARE @NomeCFT NVARCHAR(200);
|
|
|
|
SELECT TOP 1
|
|
@IDCertificates = c.IDCertificates, -- <<< adatta
|
|
@Nominal = c.Nominal_amount, -- <<< adatta
|
|
@NomeCFT = c.Descrizione + ' - ' + c.ISIN -- <<< adatta
|
|
FROM [dbo].[Certificates] c -- <<< adatta
|
|
WHERE c.ISIN = @isin;
|
|
|
|
SELECT @NumPrezziCFT = COUNT(*)
|
|
FROM [dbo].[CFT_PriceHistory] -- <<< adatta
|
|
WHERE IDCertificates = @IDCertificates;
|
|
|
|
-- Prezzo attuale worst-of (prezzo corrente / strike * 100)
|
|
-- La logica di IsWorstOf dipende dalla tua definizione (es: peggior performer)
|
|
SELECT
|
|
u.IDCertificates,
|
|
u.IDUnderlyings,
|
|
u.StartDate, -- <<< adatta
|
|
u.Strike, -- <<< adatta
|
|
-- Percentuali barriere (es: BarrieraCoupon / Nominal * 100)
|
|
u.BarrieraCouponPerc, -- <<< adatta o calcola
|
|
u.BarrieraCoupon, -- <<< adatta (valore assoluto)
|
|
u.BarrieraCapitalePerc, -- <<< adatta o calcola
|
|
u.BarrieraCapitale, -- <<< adatta (valore assoluto)
|
|
u.Name AS Sottostante, -- <<< adatta nome colonna
|
|
u.IsWorstOf, -- <<< adatta: 1=worst-of, 0=altri
|
|
-- Prezzo attuale worst-of: 0 per non-worst-of
|
|
CASE WHEN u.IsWorstOf = 1
|
|
THEN ISNULL(last_px.Px_close, 0) -- <<< adatta: ultimo prezzo disponibile
|
|
ELSE 0
|
|
END AS PriceWorst,
|
|
CASE WHEN u.IsWorstOf = 1 AND u.Strike > 0
|
|
THEN ISNULL(last_px.Px_close, 0) / u.Strike * 100
|
|
ELSE 0
|
|
END AS PriceWorstPerc,
|
|
@NumPrezziCFT AS NumPrezziCFT,
|
|
@NomeCFT AS NomeCFT,
|
|
ISNULL(u.TriggerAutocallPerc, 0) AS TriggerAutocallPerc, -- <<< adatta
|
|
ISNULL(u.AutocallValue, 0) AS AutocallValue -- <<< adatta
|
|
FROM [dbo].[Underlyings] u -- <<< adatta nome tabella
|
|
-- Join per ottenere l'ultimo prezzo disponibile
|
|
LEFT JOIN (
|
|
SELECT ul.IDCertificates, ul.IDUnderlyings,
|
|
ul.Px_close,
|
|
ROW_NUMBER() OVER (
|
|
PARTITION BY ul.IDCertificates, ul.IDUnderlyings
|
|
ORDER BY ul.Px_date DESC
|
|
) AS rn
|
|
FROM [dbo].[UL_PriceHistory] ul -- <<< adatta nome tabella
|
|
) last_px
|
|
ON last_px.IDCertificates = u.IDCertificates
|
|
AND last_px.IDUnderlyings = u.IDUnderlyings
|
|
AND last_px.rn = 1
|
|
WHERE u.IDCertificates = @IDCertificates
|
|
ORDER BY u.IsWorstOf DESC; -- worst-of prima
|
|
|
|
END
|
|
GO
|