【发布时间】:2020-08-17 22:46:45
【问题描述】:
我无法让sum() 函数在我的数据库中工作。我正在查询表purchases 的store_id(从表stores 检索)、购买的month(+year)(存储为timestamp ,未指定时区)和sum total 月销售收入。问题是没有发生聚合,因此每笔销售的总额都被报告为给定商店的每月销售总额。我想我没有正确使用“sum()”,但我还没有找到解决方案。建议和意见表示赞赏。
SELECT --month of purchase, total sales
purchases.store_id AS store_id,
TO_CHAR(timestamp, 'Month'||'YYYY') AS month,
sum(total) AS total_sales
FROM purchases
GROUP BY store_id, timestamp, purchases.total
ORDER BY month, store_id;
【问题讨论】:
标签: sql database postgresql datetime sum