我有3个表: MONTHS , STORES , REPORTS (附图像示例)。
我想编写一个SQL查询,它将 SUM 来自表day_amount
的记录(REPORTS
),但只记录具有相同存储和同月的记录。
我需要这样的东西:
SELECT SUM(REPORTS.day_amount) AS GRAND_TOTAL
FROM REPORTS
WHERE MONTHS.month_id = REPORTS.month_id
AND STORES.store_id = REPORTS.store_id
这是对的吗?我必须使用GROUP BY?
答案 0 :(得分:1)
您需要确保您正在加入其他表(如果您需要这样做)。
A throw executed inside a parallel region must cause execution to resume within
the dynamic extent of the same structured block, and it must be caught by the
same thread that threw the exception.
虽然看起来你根本不需要其他桌子,但你可以试试这样的事情;
SELECT
MONTHS.month_id
,STORES.store_id
,SUM(REPORTS.day_amount) AS GRAND_TOTAL
FROM REPORTS
INNER JOIN MONTHS ON MONTHS.month_id = REPORTS.month_id
INNER JOIN STORES ON STORES.store_id = REPORTS.store_id
GROUP BY MONTHS.month_id, STORES.store_id