在PostgreSQL中创建摘要表
我被要求使用PostgreSQL从表中创建聚合数据 我不知道是否可以直接从PostgreSQL执行此操作,因为我总是使用SQL Server编写查询。为此,我通常将查询结果复制到excel中,然后对其进行透视在PostgreSQL中创建摘要表,postgresql,summary,Postgresql,Summary,我被要求使用PostgreSQL从表中创建聚合数据 我不知道是否可以直接从PostgreSQL执行此操作,因为我总是使用SQL Server编写查询。为此,我通常将查询结果复制到excel中,然后对其进行透视 SELECT date(order_date) , count(customer_id) as total_customer , product_category , sum(quantity) as total_qty , sum(total_price) as total_pri
SELECT
date(order_date)
, count(customer_id) as total_customer
, product_category
, sum(quantity) as total_qty
, sum(total_price) as total_price
FROM public."CURRENT_WP_SALES"
WHERE order_status = 'sale'
GROUP BY date(order_date), product_category
ORDER BY date(order_date), product_category asc
我得到的结果如下:
+============+================+================+===========+=============+
| date | total_customer | product | total_qty | total_price |
+============+================+================+===========+=============+
| 2018-12-20 | 2 | frozen food | 2 | 500 |
+------------+----------------+----------------+-----------+-------------+
| 2018-12-20 | 4 | instant noodle | 5 | 300 |
+------------+----------------+----------------+-----------+-------------+
| 2018-12-20 | 4 | meds | 1 | 50 |
+------------+----------------+----------------+-----------+-------------+
| 2018-12-20 | 6 | candy | 10 | 200 |
+------------+----------------+----------------+-----------+-------------+
+============+================+================+===========+=============+
| date | total_customer | product | total_qty | total_price |
+============+================+================+===========+=============+
| | | frozen food | 2 | |
+ + +----------------+-----------+ +
| | | instant noodle | 5 | |
+ 2018-12-20 + 16 +----------------+-----------+ 1050 +
| | | meds | 1 | |
+ + +----------------+-----------+ +
| | | candy | 10 | |
+------------+----------------+----------------+-----------+-------------+
预期结果如下:
+============+================+================+===========+=============+
| date | total_customer | product | total_qty | total_price |
+============+================+================+===========+=============+
| 2018-12-20 | 2 | frozen food | 2 | 500 |
+------------+----------------+----------------+-----------+-------------+
| 2018-12-20 | 4 | instant noodle | 5 | 300 |
+------------+----------------+----------------+-----------+-------------+
| 2018-12-20 | 4 | meds | 1 | 50 |
+------------+----------------+----------------+-----------+-------------+
| 2018-12-20 | 6 | candy | 10 | 200 |
+------------+----------------+----------------+-----------+-------------+
+============+================+================+===========+=============+
| date | total_customer | product | total_qty | total_price |
+============+================+================+===========+=============+
| | | frozen food | 2 | |
+ + +----------------+-----------+ +
| | | instant noodle | 5 | |
+ 2018-12-20 + 16 +----------------+-----------+ 1050 +
| | | meds | 1 | |
+ + +----------------+-----------+ +
| | | candy | 10 | |
+------------+----------------+----------------+-----------+-------------+
如果有任何方法可以直接从PostgreSQL执行此操作,请让我知道 没有一个DBMS会完全允许您提出的要求,但有一些接近的解决方案:
SELECT
date
, SUM(total_customer)
, array_agg(product_category ORDER BY product_category)
, array_agg(total_qty ORDER BY product_category)
, SUM(total_price)
FROM (
SELECT
date(order_date)
, count(customer_id) as total_customer
, product_category
, sum(quantity) as total_qty
, sum(total_price) as total_price
FROM public."CURRENT_WP_SALES"
WHERE order_status = 'sale'
GROUP BY date(order_date), product_category
) T
GROUP BY date
ORDER BY date
谢谢,我会问我的用户他更喜欢使用哪一个。