Hogyan is generáljunk olyan dátum kereszttáblát amely segítségével nagyon sokféle felösszegzési műveletet támogathatunk?
Hát SqlServer-en így:
SET LANGUAGE Hungarian
DECLARE @StartDate DATE = '2016.01.01';
DECLARE @EndDate DATE = '2017.12.31';
WITH KLDate (Datum) AS (SELECT @StartDate UNION ALL SELECT DATEADD(d, 1, Datum) FROM KLDate WHEREDATEADD(d, 1, Datum) <= @EndDate)
--INSERT @dim_date_table
SELECT Datum AS [DatumID]
,CAST(CAST(YEAR(Datum) AS VARCHAR) + '. ' + CAST(UPPER(SUBSTRING(DATENAME( MONTH, Datum), 1,1)) + SUBSTRING(DATENAME(MONTH, Datum), 2, LEN(DATENAME(MONTH, Datum))) AS VARCHAR) + ' ' + CAST(DAY(Datum) ASVARCHAR) + '.' AS VARCHAR(50)) AS [DatumNev]
,DATEPART(YEAR, Datum) * 100 + DATEPART(WEEK, Datum) AS [HetID]
,DATENAME(dw,Datum) as [HetNap]
,DATEPART(dw,Datum) as [HetNapSsz]
,CAST(CAST(DATEPART(WEEK, Datum) AS VARCHAR(2))+ '. hét' AS VARCHAR(7)) AS [HetNev]
,YEAR(Datum) * 100 + MONTH(Datum) AS [HonapID]
,CAST(CAST(YEAR(Datum) AS VARCHAR) + '. ' + CAST(UPPER(SUBSTRING(DATENAME( MONTH, Datum), 1,1)) + SUBSTRING(DATENAME(MONTH, Datum), 2, LEN(DATENAME(MONTH, Datum))) AS VARCHAR(20)) AS VARCHAR(20)) AS[HonapNev]
,YEAR(Datum) * 10 + DATEPART(QUARTER, Datum) AS [NegyedevID]
,CAST(CAST(YEAR(Datum) AS VARCHAR) + '. ' + CASE DATEPART(QUARTER, Datum) WHEN 1 THEN 'I' WHEN2 THEN 'II' WHEN 3 THEN 'III' ELSE 'IV' END + '. negyedév' AS VARCHAR(20)) AS [NegyedevNev]
,YEAR(Datum) * 10 + CASE WHEN DATEPART(QUARTER, Datum) < 3 THEN 1 ELSE 2 END AS [FelevID]
,CAST(CAST(YEAR(Datum) AS VARCHAR) + '. ' + CASE WHEN DATEPART(QUARTER, Datum) < 3 THEN 'I'ELSE 'II' END + '. félév' AS VARCHAR(20)) AS [FelevNev]
,YEAR(Datum) AS [EvID]
,CAST(CAST(YEAR(Datum) AS VARCHAR) AS VARCHAR(10)) AS [EvNev]
,DATEPART(DAY, Datum) AS [EvfuggetlenNapID]
,CAST(DATEPART(DAY, Datum) AS VARCHAR(2)) AS [EvfuggetlenNapNev]
,DATEPART(wk, Datum) AS [EvfuggetlenHetID]
,CAST(DATEPART(wk, Datum) AS VARCHAR(2)) + '. hét' AS [EvfuggetlenHetNev]
,MONTH(Datum) AS [EvfuggetlenHonapID]
,CAST(UPPER(SUBSTRING(DATENAME (MONTH, Datum), 1, 1)) + SUBSTRING(DATENAME(MONTH, Datum), 2,LEN(DATENAME(MONTH, Datum))) AS VARCHAR(20)) AS [EvfuggetlenHonapNev]
,DATEPART(QUARTER, Datum) AS [EvfuggetlenNegyedevID]
,CAST(CASE DATEPART(QUARTER, Datum) WHEN 1 THEN 'I' WHEN 2 THEN 'II' WHEN 3 THEN 'III' ELSE'IV' END + '. negyedév' AS VARCHAR(13)) AS [EvfuggetlenNegyedevNev]
,CASE WHEN DATEPART(QUARTER, Datum) < 3 THEN 1 ELSE 2 END AS [EvfuggetlenFelevID]
,CAST(CASE WHEN DATEPART(QUARTER, Datum) < 3 THEN 'I' ELSE 'II' END + '. félév' AS VARCHAR(9))AS [EvfuggetlenFelevNev]
,CASE WHEN MONTH(Datum) = MONTH(DATEADD(DAY, 1, Datum)) THEN 'N' ELSE 'Y' END AS[HonapUtolsoNapja]
,CASE ---- Ünnepek 2017
WHEN Datum = '2017-03-15' then 'Ünnep'
WHEN Datum = '2017-04-14' then 'Ünnep'
WHEN Datum = '2017-04-17' then 'Ünnep'
WHEN Datum = '2017-05-01' then 'Ünnep'
WHEN Datum = '2017-06-05' then 'Ünnep'
WHEN Datum = '2017-10-23' then 'Ünnep'
WHEN Datum = '2017-11-01' then 'Ünnep'
WHEN Datum = '2017-12-25' then 'Ünnep'
WHEN Datum = '2017-12-26' then 'Ünnep'
---- Ünnepek 2016
WHEN Datum = '2016-01-01' then 'Ünnep'
WHEN Datum = '2016-03-14' then 'Ünnep_'
WHEN Datum = '2016-03-15' then 'Ünnep'
WHEN Datum = '2016-03-28' then 'Ünnep'
WHEN Datum = '2016-05-16' then 'Ünnep'
WHEN Datum = '2016-08-20' then 'Ünnep'
WHEN Datum = '2016-10-31' then 'Ünnep_'
WHEN Datum = '2016-11-01' then 'Ünnep'
WHEN Datum = '2016-12-25' then 'Ünnep'
WHEN Datum = '2016-12-26' then 'Ünnep'
---- Ledolgozás
WHEN Datum = '2016-03-05' then 'Ledolgozás'
WHEN Datum = '2016-10-15' then 'Ledolgozás'
---- Normál hétvégék
WHEN DATEPART(dw,Datum) > 5 then 'Hétvége'
ELSE 'Munkanap'
end as [BankiMunkanapFlag]
,'Date : [' + CAST(@StartDate AS VARCHAR) + ' - ' + CAST(@EndDate AS VARCHAR) + '] ' AS[IdosorNev]
FROM KLDate OPTION (MAXRECURSION 0)
Éves, negyedéves, havi, heti, munkanap összegzés, hétvége ünnepek kezelése, ....
Megjegyzések
Megjegyzés küldése