SQL - GROUP和WHERE ERROR

时间:2017-07-01 16:30:45

标签: sql sql-server database

WRONG OUTPUT

第1周至第52周 计算每周的所有报告 计算每周的所有目标

[EDITED]

我从基础和划痕开始。

CREATE TABLE Records
    ([Dates] DATE, [Week] INTEGER, [dayOfWeek] varchar(10), [report] INTEGER)
;

INSERT INTO Records
    ([Dates], [Week], [dayOfWeek], [report] )
VALUES
    ('02/01/2017', '1', 'Monday', '1'),
    ('03/01/2017', '1', 'Tuesday', '2'),
    ('04/01/2017', '1', 'Wednesday', '2'),
    ('05/01/2017', '1', 'Thursday', '5'),
    ('06/01/2017', '1', 'Friday', '1')
;

QUERY

SELECT Week, dayOfWeek, COUNT(report) AS TotalReports
FROM Records
GROUP BY Records.Week, Records.dayOfWeek

1 个答案:

答案 0 :(得分:1)

我相信你想要SUM()而不是COUNT()。 COUNT()将返回行数,SUM()将添加列中包含的值。

SELECT Week, dayOfWeek, SUM(report) AS TotalReports
FROM Records
GROUP BY Week, dayOfWeek