SQL查询计数记录数

时间:2018-10-29 23:51:53

标签: sql reporting-services

嗨,我在下面的报表状态数据示例中显示记录

NAME     STATE
BOB      NSW
PETER    ACT
SAM      NSW
BOY      VIC

我需要统计每个状态的每个记录以显示在SSRS报告中。我正在尝试行号和分区依据。但是显示1,2,3,4。那就是应该的。任何其他建议,预期结果应为NSW = 2 VIC = 1和ACT = 1

2 个答案:

答案 0 :(得分:0)

你可以喜欢

CREATE TABLE T
    ([NAME] varchar(5), [STATE] varchar(3))
;

INSERT INTO T
    ([NAME], [STATE])
VALUES
    ('BOB', 'NSW'),
    ('PETER', 'ACT'),
    ('SAM', 'NSW'),
    ('BOY', 'VIC')
;

SELECT *, (SELECT COUNT(State) FROM T WHERE State = T1.State) StateCount
FROM T T1;

返回:

+-------+-------+------------+
| NAME  | STATE | StateCount |
+-------+-------+------------+
| BOB   | NSW   |          2 |
| PETER | ACT   |          1 |
| SAM   | NSW   |          2 |
| BOY   | VIC   |          1 |
+-------+-------+------------+

答案 1 :(得分:0)

类似

SELECT state, count(name) AS count
FROM yourtable
GROUP BY state
ORDER BY state