简体   繁体   English

SQL Server DW-使用日期维度和时间维度查询数据

[英]SQL Server DW - Query data using Date dimension and Time Dimension

Before in the source systems I could do something like below 在源系统中,我可以做下面的事情

SELECT COUNT(*)
FROM FlightDeparture FD
WHERE FD.[Response_ts] BETWEEN '2013-01-01 45:01:38.000' AND '2013-01-02 15:04:20.000'

NOW we have a DW where there is a separate date dimension ( Dim_Date ) and time dimension ( Dim_time ), I tried below query but I am not getting same counts 现在我们有了一个DW,其中有一个单独的日期维度( Dim_Date )和时间维度( Dim_time ),我在下面的查询中尝试了一下,但没有得到相同的计数

SELECT COUNT(*)
FROM FACT_FlightDeparture FD
INNER JOIN DIM_DATE DD ON FD.Resp_DATE_SK = DD.Date_Sk
INNER JOIN DIM_TIME TT ON FD.Resp_TIME_SK = TT.TIME_Sk
AND (DD.Cal_Date >= '2013-01-01' AND TT.Time_Of_Day >= '14:01:38.000')
AND (DD.Cal_Date <= '2013-01-02' AND TT.Time_Of_Day <= '14:04:20.000')

Please help. 请帮忙。 I have reduced the issue to how I am doing the TIME portion 我已将问题减少到我如何做“时间”部分

Thanks in advance 提前致谢

The AND statement is preventing the two predicates from hardly ever returning true. AND语句阻止了这两个谓词几乎不会返回true。 Due to the Time_Of_Day difference, there is actually just under 3 minutes per day that the condition could return true. 由于Time_Of_Day的差异,实际上每天可能只有不到3分钟的时间条件会返回true。

If my thinking is correct, you need all times on 1/1 greater than or equal to 14:01:38 and all times on 1/2 less than or equal to 14:04:20. 如果我的想法是正确的,那么您需要所有时间都大于或等于14:01:38的1/1,并且所有时间都小于或等于14:04:20的1/2。

SELECT COUNT(*)
FROM FACT_FlightDeparture FD
INNER JOIN DIM_DATE DD ON FD.Resp_DATE_SK = DD.Date_Sk
INNER JOIN DIM_TIME TT ON FD.Resp_TIME_SK = TT.TIME_Sk
WHERE (
         (DD.Cal_Date = '2013-01-01' AND TT.Time_Of_Day >= '14:01:38.000')
      OR (DD.Cal_Date = '2013-01-02' AND TT.Time_Of_Day <= '14:04:20.000')
)

If the fact table is big and you plan to use this query pattern often, I'd put both of these columns into a single nonclustered index. 如果事实表很大,并且您打算经常使用此查询模式,那么我会将这两列都放在一个非聚集索引中。

CREATE NONCLUSTERED INDEX IX_FACT_FlightDeparture_DateTime 
ON FACT_FlightDeparture (Resp_DATE_SK,Resp_TIME_SK);

Also, don't forget your partitioning, compression, and other options on the index if applicable. 此外,请不要忘记对索引进行分区,压缩和其他选择(如果适用)。

I don't have a answer for this question. 我没有这个问题的答案。 Can you check if exists more rows in DIM_TIME than in FlightDeparture ? 您可以检查DIM_TIME中是否存在比FlightDeparture多的行? Let´s say original table FlightDeparture has only two records 假设原始表格FlightDeparture只有两个记录

'14:01:38.000','14:04:20:000'

and DIM_TIME has another one, even duplicate DIM_TIME还有另一个,甚至重复

'14:01:38.000','14:04:19:000','14:04:20:000'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM