如何汇总交易数据以进行报告? (SQL Server)

问题描述 投票:0回答:2

我有一些看起来像这样的数据:

+----+-----------+--------+---------+--------+
| 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 |
+------+-------+-----+------------+-----------+-------+

但后来我想......除了编写运行数百万个查询的代码并花费数天才能运行时,必须有更好的方法,然后随着新数据的进入而难以更新。

有什么建议?

sql-server reporting data-warehouse aggregation business-intelligence
2个回答
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

0
投票

您可以添加DimDate而不是分解日期元素。比使用聚合数据创建Fact表。

© www.soinside.com 2019 - 2024. All rights reserved.