每个日期只获得一个记录

时间:2018-05-28 02:41:44

标签: sql syntax count

我有一个问题,

我有这样的表:

|------|------------|---------|
| [Id] |   [Date]   | [InOut] |
|------|------------|---------|
|  10  | 2018-05-01 |    IN   |
|  10  | 2018-05-01 |    IN   |
|  10  | 2018-05-02 |    IN   |
|  10  | 2018-05-03 |    IN   |
|------|------------|---------|

我想要的是每个日期计算[InOut] 1条记录,这意味着返回3而不是4

我已使用此查询:SELECT DISTINCT COUNT([Date]), [Id] FROM AttLog GROUP BY [Date], [Id]

但它不起作用,我只得到1

请帮我正确查询,谢谢你提前

2 个答案:

答案 0 :(得分:0)

SELECT [Date], [Id], count([InOut]) from AttLog GROUP BY [Date], [Id]

答案 1 :(得分:0)

这可能是你想要的:

SELECT ID, COUNT(DISTINCT [Date])
FROM AttLog
GROUP BY [Id];

我无法确定InOut是否为过滤条件或汇总条件,但您可能希望将其添加到GROUP BYSELECT

SELECT ID, INOUT, COUNT(DISTINCT [Date])
FROM AttLog
GROUP BY Id, INOUT;