在 PostgreSQL 中创建摘要 table
Creating a summary table in PostgreSQL
我被要求使用 PostgreSQL 从 table 创建聚合数据。
我不知道是否可以直接从 PostgreSQL 执行此操作,因为我总是使用 SQL 服务器编写查询。为此,我通常将查询结果复制到 excel,然后对它们进行透视。
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 | |
+------------+----------------+----------------+-----------+-------------+
如果有任何方法可以直接从 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
我被要求使用 PostgreSQL 从 table 创建聚合数据。
我不知道是否可以直接从 PostgreSQL 执行此操作,因为我总是使用 SQL 服务器编写查询。为此,我通常将查询结果复制到 excel,然后对它们进行透视。
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 | |
+------------+----------------+----------------+-----------+-------------+
如果有任何方法可以直接从 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