TSQL - 获取数据仓库维度中日期之间的值
TSQL - Get value between dates in data warehouse dimension
我有一个数据仓库的 beginnngs,其中包含(除其他 tables)
- date table 有 200 年的日期可以加入,所以不需要
建立一个“动态”日期 table
- dbo.Dim_Items(见下文)
ItemNumber
QOH
EffectiveDate
ExpirationDate
IsCurrentRecord
372620
24
2021-12-11 05:34:09.000
9999-12-31 00:00:00.000
1
372620
11
2021-12-09 05:34:11.000
2021-12-11 05:34:09.000
0
372620
9
2021-12-07 05:34:20.000
2021-12-09 05:34:11.000
0
我想找出从 2021 年 12 月 7 日到今天(为简洁起见,假设 2021 年 12 月 13 日)之间每一天的 QOH,这样我的结果如下所示
Date
ItemNumber
QOH
2021-12-07
372620
9
2021-12-08
372620
9
2021-12-09
372620
11
2021-12-10
372620
11
2021-12-11
372620
24
2021-12-12
372620
24
2021-12-13
372620
24
我最接近的是下面的查询,但它没有按照我想要的方式工作(注意日期上的零与 Items 维度不一致)
DECLARE @START_DATE date,
@END_DATE date
SET @START_DATE = '20211207'
SET @END_DATE = GETDATE() -- '20211213'
SELECT CAL.TheDate,
I.ItemNumber,
I.QOH
FROM dbo.Dim_Calendar CAL
LEFT OUTER JOIN dbo.Dim_Items I ON CAL.TheDate >= CAST(I.EffectiveDate as date)
AND CAL.TheDate <= CAST(I.EffectiveDate as date)
AND I.ItemNumber = 372620
WHERE CAL.TheDate >= @START_DATE
AND CAL.TheDate <= @END_DATE
ORDER BY CAL.TheDate,
I.ItemNumber
Date
ItemNumber
QOH
2021-12-07
372620
9
2021-12-08
372620
0
2021-12-09
372620
11
2021-12-10
372620
0
2021-12-11
372620
24
2021-12-12
372620
0
2021-12-13
372620
0
我是整个数据仓库的新手,所以我不确定如何正确构建此查询。
DECLARE @START_DATE date,
@END_DATE date
SET @START_DATE = '20211207'
SET @END_DATE = GETDATE() -- '20211213'
SELECT
TheDate,
I.Itemnumber,
I.QOH
FROM dbo.Dim_Calendar CAL
LEFT JOIN dbo.Dim_Items I On CAL.TheDate BETWEEN CAST(I.EffectiveDate as DATE)
AND I.ExpirationDate
AND I.Itemnumber = 372620
WHERE CAL.TheDate >= @START_DATE
AND CAL.TheDate <= @END_DATE
ORDER BY CAL.TheDate,
I.ItemNumber
我有一个数据仓库的 beginnngs,其中包含(除其他 tables)
- date table 有 200 年的日期可以加入,所以不需要 建立一个“动态”日期 table
- dbo.Dim_Items(见下文)
ItemNumber | QOH | EffectiveDate | ExpirationDate | IsCurrentRecord |
---|---|---|---|---|
372620 | 24 | 2021-12-11 05:34:09.000 | 9999-12-31 00:00:00.000 | 1 |
372620 | 11 | 2021-12-09 05:34:11.000 | 2021-12-11 05:34:09.000 | 0 |
372620 | 9 | 2021-12-07 05:34:20.000 | 2021-12-09 05:34:11.000 | 0 |
我想找出从 2021 年 12 月 7 日到今天(为简洁起见,假设 2021 年 12 月 13 日)之间每一天的 QOH,这样我的结果如下所示
Date | ItemNumber | QOH |
---|---|---|
2021-12-07 | 372620 | 9 |
2021-12-08 | 372620 | 9 |
2021-12-09 | 372620 | 11 |
2021-12-10 | 372620 | 11 |
2021-12-11 | 372620 | 24 |
2021-12-12 | 372620 | 24 |
2021-12-13 | 372620 | 24 |
我最接近的是下面的查询,但它没有按照我想要的方式工作(注意日期上的零与 Items 维度不一致)
DECLARE @START_DATE date,
@END_DATE date
SET @START_DATE = '20211207'
SET @END_DATE = GETDATE() -- '20211213'
SELECT CAL.TheDate,
I.ItemNumber,
I.QOH
FROM dbo.Dim_Calendar CAL
LEFT OUTER JOIN dbo.Dim_Items I ON CAL.TheDate >= CAST(I.EffectiveDate as date)
AND CAL.TheDate <= CAST(I.EffectiveDate as date)
AND I.ItemNumber = 372620
WHERE CAL.TheDate >= @START_DATE
AND CAL.TheDate <= @END_DATE
ORDER BY CAL.TheDate,
I.ItemNumber
Date | ItemNumber | QOH |
---|---|---|
2021-12-07 | 372620 | 9 |
2021-12-08 | 372620 | 0 |
2021-12-09 | 372620 | 11 |
2021-12-10 | 372620 | 0 |
2021-12-11 | 372620 | 24 |
2021-12-12 | 372620 | 0 |
2021-12-13 | 372620 | 0 |
我是整个数据仓库的新手,所以我不确定如何正确构建此查询。
DECLARE @START_DATE date,
@END_DATE date
SET @START_DATE = '20211207'
SET @END_DATE = GETDATE() -- '20211213'
SELECT
TheDate,
I.Itemnumber,
I.QOH
FROM dbo.Dim_Calendar CAL
LEFT JOIN dbo.Dim_Items I On CAL.TheDate BETWEEN CAST(I.EffectiveDate as DATE)
AND I.ExpirationDate
AND I.Itemnumber = 372620
WHERE CAL.TheDate >= @START_DATE
AND CAL.TheDate <= @END_DATE
ORDER BY CAL.TheDate,
I.ItemNumber