Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL查询和语法(经典ASP项目)_Sql_Sql Server_Group By_Asp Classic_Sum - Fatal编程技术网

SQL查询和语法(经典ASP项目)

SQL查询和语法(经典ASP项目),sql,sql-server,group-by,asp-classic,sum,Sql,Sql Server,Group By,Asp Classic,Sum,我有3张表格:月份,商店,报告(附图片样本) 我想编写一个SQL查询,它将对表报告中的记录(day\u amount)进行求和,但只对具有相同存储和相同月份的记录进行求和 我需要这样的东西: SELECT SUM(REPORTS.day_amount) AS GRAND_TOTAL FROM REPORTS WHERE MONTHS.month_id = REPORTS.month_id AND STORES.store_id = REPORTS.store_id 对吗?我必须按使用组?您

我有3张表格:月份商店报告(附图片样本)

我想编写一个SQL查询,它将对表
报告中的记录(
day\u amount
)进行求和,但只对具有相同存储和相同月份的记录进行求和

我需要这样的东西:

SELECT SUM(REPORTS.day_amount) AS GRAND_TOTAL
FROM REPORTS
WHERE MONTHS.month_id = REPORTS.month_id
  AND STORES.store_id = REPORTS.store_id

对吗?我必须按使用组?

您需要确保加入其他表(如果需要)

虽然,看起来你根本不需要其他的桌子,但你可以试试这样的东西

SELECT 
     REPORTS.month_id
    ,REPORTS.store_id
    ,SUM(REPORTS.day_amount) AS GRAND_TOTAL
FROM REPORTS
GROUP BY REPORTS.month_id, REPORTS.store_id

是的,没错。你不需要分组。您的查询很好。您没有引用SQL语句中的其他表。如果要使用连接语句,可能需要在此处查找并执行连接语句。
SELECT 
     REPORTS.month_id
    ,REPORTS.store_id
    ,SUM(REPORTS.day_amount) AS GRAND_TOTAL
FROM REPORTS
GROUP BY REPORTS.month_id, REPORTS.store_id