【问题标题】:relation between two tables fact两个表之间的关系事实
【发布时间】:2023-02-22 21:11:53
【问题描述】:

我们有两个事实表如下

事实1

DeviceKey Datekey TimeKey CustomersIn
1 20230101 101532 2
1 20230101 230945 1
2 20230101 092409 2
2 20230102 142915 3
3 20230102 120024 1
3 20230102 161935 2

事实2

DeviceKey EventDateKey EventTimeKey ErrorKey
1 20230101 092423 2
1 20230101 093412 12
1 20230101 213311 12
1 20230102 125503 2
1 20230103 081215 12
2 20230102 174523 12
2 20230102 180112 12
3 20230101 120412 2

显示 Fact 2 表的 EventDateKey 和 EventTimeKey,大于 Fact 1 表中每个设备的最大 Datekey 和 TimeKey。 结果如下:

结果

DeviceKey EventDateKey EventTimeKey ErrorKey
1 20230102 125503 2
1 20230103 081215 12
2 20230102 174523 12
2 20230102 180112 12

我如何得出这个结论? 通过在数据库或 SSAS 表格中创建视图?

【问题讨论】:

  • 源代码。请格式化
  • 设备 1将不匹配,因为 max(TimeKey) 为 230945 大于 Fact2 中的所有 EventTimeKey
  • 更正并编辑。

标签: sql-server ssas ssas-tabular


【解决方案1】:

使用group by 获取 max_datekey 和 max_timekey,然后使用内部连接获取所需数据

select f2.*
from Fact2 f2
inner join (
  select DeviceKey, max(Datekey) max_datekey, max(TimeKey) max_timekey
  from Fact1 f1
  group by DeviceKey
) as s on s.DeviceKey = f2.DeviceKey  and (EventDateKey >= max_datekey and EventTimeKey >= max_timekey)

【讨论】:

    猜你喜欢
    • 2018-09-03
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 2017-07-31
    相关资源
    最近更新 更多