我有一些看起来像这样的数据:
+----+-----------+--------+---------+--------+
| id | timestamp | amount | product | method |
+----+-----------+--------+---------+--------+
| 1 | 6/5/2018 | 4 | apple | cash |
| 2 | 6/5/2018 | 7 | apple | cash |
| 3 | 6/6/2018 | 3 | orange | card |
| 4 | 6/6/2018 | 9 | orange | cash |
| 5 | 6/7/2018 | 4 | orange | card |
| 6 | 6/7/2018 | 8 | apple | card |
+----+-----------+--------+---------+--------+
每天有数十万笔交易。
我想根据这些数据生成图表,这些图表可以是每天,每周,每月或数月。我当时想我应该编写一些c#代码来循环遍历每天,产品,方法的组合,然后运行查询以获取总计并产生如下数据:
+------+-------+-----+------------+-----------+-------+
| year | month | day | dimProduct | dimMethod | total |
+------+-------+-----+------------+-----------+-------+
| 2018 | 6 | 5 | apple | cash | 11 |
| 2018 | 6 | 5 | apple | card | 0 |
| 2018 | 6 | 6 | apple | cash | 0 |
| 2018 | 6 | 6 | apple | card | 0 |
| 2018 | 6 | 7 | apple | cash | 0 |
| 2018 | 6 | 7 | apple | card | 8 |
| 2018 | 6 | 5 | orange | cash | 0 |
| 2018 | 6 | 5 | orange | card | 0 |
| 2018 | 6 | 6 | orange | cash | 9 |
| 2018 | 6 | 6 | orange | card | 3 |
| 2018 | 6 | 7 | orange | cash | 0 |
| 2018 | 6 | 7 | orange | card | 4 |
+------+-------+-----+------------+-----------+-------+
但是后来我觉得...有一种比编写代码的方法更好的方法,该代码可以运行数百万个查询,并且需要花费数天的时间才能运行,然后随着新数据的传入而难以更新。
有什么建议吗?
答案 0 :(得分:0)
由于您需要生成不存在的数据,因此您需要包括数据库引擎,因为答案将取决于供应商。
对于您已有的数据,您需要从日期列中提取日期部分,将它们分组并包括sum(amount)
作为总数:
select
year(timestamp) as year,
month(timestamp) as month,
day(timestamp) as day,
product as "dimProduct",
method as "dimMethod",
sum(amount) as total
from yourtable
group by year(timestamp), month(timestamp), day(timestamp), product, method
答案 1 :(得分:0)
您可以添加DimDate而不是分解日期元素。比用汇总数据创建事实表。