SQL Server - 在子查询中使用GROUP BY的COUNT

时间:2016-03-15 14:23:54

标签: sql sql-server

我一直在努力解决这个问题!本质上,我一直在尝试在子查询中使用COUNT和GROUP BY,错误返回多个值和整个错误主机。

所以,我有下表:

start_date  | ID_val     |  DIR   | tsk | status|
-------------+------------+--------+-----+--------+
25-03-2015  |   001      |   U    | 28  |   S    |
27-03-2016  |   003      |   D    | 56  |   S    |
25-03-2015  |   004      |   D    | 56  |   S    |
25-03-2015  |   001      |   U    | 28  |   S    |
16-02-2016  |   002      |   D    | 56  |   S    |
25-03-2015  |   001      |   U    | 28  |   S    |
16-02-2016  |   002      |   D    | 56  |   S    |
16-02-2016  |   005      |   NULL | 03  |   S    |
25-03-2015  |   001      |   U    | 17  |   S    |
16-02-2016  |   002      |   D    | 81  |   S    |

理想情况下,我需要计算ID_val的唯一值的次数,例如U和28或D和56.并且只有那些组合。

例如,我希望在可能的情况下返回以下结果:

start_date   | ID_val     |  no of times  | status |
-------------+------------+---------------+--------+
25-03-2015  |   001      |      3        |    S   |
27-03-2016  |   003      |      1        |    S   |
25-03-2015  |   004      |      1        |    S   |
25-03-2015  |   002      |      3        |    S   |

我已经成功地获得了自己的时间,但没有与其他值(子查询?)分开表。

非常感谢任何建议!

4 个答案:

答案 0 :(得分:2)

这是一个基本的条件聚合:

select id_val,
       sum(case when (dir = 'U' and tsk = 28) or (dir = 'D' and tsk = 56)
                then 1 else 0
           end) as NumTimes
from t
group by id_val;

我遗漏了其他专栏,因为您的问题主要集中在id_valdirtsk。其他专栏似乎没必要。

答案 1 :(得分:1)

COUNTGROUP BY一起使用。

<强>查询

select start_date, ID_val, count(ID_Val) as [no. of times], [status]
from your_table_name
where (tsk = 28 and DIR = 'U') or (tsk = 56 and DIR = 'D')
group by start_date, ID_val, [status]

答案 2 :(得分:1)

您希望每个ID_val有一个结果,因此您需要按ID_val进行分组。

您想要最短的开始日期:min(start_date)

您想要任何状态(因为它始终是相同的):例如min(status)max(status)

您想要计算匹配项:count(case when <match> then 1 end)

select
  min(start_date) as start_date,
  id_val,
  count(case when (dir = 'U' and tsk = 28) or (dir = 'D' and tsk = 56) then 1 end) 
    as no_of_times,
  min(status) as status
from mytable
group by id_val;

答案 3 :(得分:1)

到目前为止,所有答案都假设您将提前知道值对,如果这些更改或被添加,则需要修改。这个解决方案没有任何假设。

表格创建

CREATE TABLE IDCounts
(
      start_date date
    , ID_val     char(3)
    , DIR        nchar(1)
    , tsk        int
    , status     nchar(1)
)

INSERT IDCounts
VALUES
     ('2015-03-25','001','U'  , 28,'S')
    ,('2016-03-27','003','D'  , 56,'S')
    ,('2015-03-25','004','D'  , 56,'S')
    ,('2015-03-25','001','U'  , 28,'S')
    ,('2016-03-16','002','D'  , 56,'S')
    ,('2015-03-25','001','U'  , 28,'S')
    ,('2016-02-16','002','D'  , 56,'S')
    ,('2016-02-16','005', NULL, 03,'S')
    ,('2015-03-25','001','U'  , 17,'S')
    ,('2016-02-16','002','D'  , 81,'S');

<强>代码

SELECT Distinct i1.start_date, i1.ID_Val, i2.NumOfTimes, i1.status
from IDCounts i1
    JOIN 
    (
        select start_date, ID_val, isnull(DIR,N'')+cast(tsk as nvarchar) ValuePair, count(DIR+cast(tsk as nvarchar)) as NumOfTimes
        from IDCounts
        GROUP BY start_date, ID_val, isnull(DIR,N'')+cast(tsk as nvarchar)
    ) i2 on i2.start_date=i1.start_date
        and i2.ID_val    =i1.ID_val
        and i2.ValuePair =isnull(i1.DIR,N'')+cast(i1.tsk as nvarchar)
order by i1.ID_val, i1.start_date;