在SQL中聚合子查询中的计数

时间:2016-06-02 14:35:11

标签: sql sql-server

基本上我希望查询1给出查询2的结果。我需要使用查询1 b / c我有其他子查询添加我要么计算或获取最大日期。

我尝试按查询1中的ClientVisit.client_id进行分组但我得到此错误列'ClientVisit.clientvisit_id'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。 ClientVisit.clientvisit_id不在选择列表中。

查询1

SELECT ClientVisit.client_id ,
       ( SELECT COUNT( ZFORM_group_note_28.clientvisit_id )
         FROM ZFORM_group_note_28
         WHERE ZFORM_group_note_28.successful_day_752410 = 'Yes'
               AND ZFORM_group_note_28.clientvisit_id =        ClientVisit.clientvisit_id ) AS Successful_Day
FROM ClientVisit
WHERE ClientVisit.visittype_id = 16
      AND ClientVisit.program_id = 5
      AND ClientVisit.rev_timein >= @param1
      AND ClientVisit.rev_timeout < DATEADD( DAY ,
                                         1 ,
                                         @param2 )

查询2

SELECT ClientVisit.client_id ,
       COUNT( ZFORM_group_note_28.clientvisit_id ) AS Successful_Day
FROM ClientVisit
INNER JOIN ZFORM_group_note_28
ON ZFORM_group_note_28.clientvisit_id = ClientVisit.clientvisit_id
WHERE ClientVisit.visittype_id = 16
      AND ClientVisit.program_id = 5
      AND ZFORM_group_note_28.successful_day_752410 = 'Yes'
      AND ClientVisit.rev_timein >= @param1
      AND ClientVisit.rev_timeout < DATEADD( DAY ,
                                         1 ,
                                         @param2 )
GROUP BY ClientVisit.client_id

结果: 查询1

client_id   successful_day
1182    1
1182    1
1379    1
1379    1
1379    1
1379    1

查询2

client_id   successful_day
1182    2
1379    9
1523    2
1757    1

3 个答案:

答案 0 :(得分:0)

尝试:

SELECT ClientVisit.client_id ,
       COUNT( ClientVisit.clientvisit_id ) AS Successful_Day
FROM ClientVisit
INNER JOIN ZFORM_group_note_28
ON ZFORM_group_note_28.clientvisit_id = ClientVisit.clientvisit_id
WHERE ClientVisit.visittype_id = 16
      AND ClientVisit.program_id = 5
      AND ZFORM_group_note_28.successful_day_752410 = 'Yes'
      AND ClientVisit.rev_timein >= @param1
      AND ClientVisit.rev_timeout < DATEADD( DAY ,
                                         1 ,
                                         @param2 )
GROUP BY ClientVisit.client_id

我已更改的是COUNT上从ZFORM_group_note_28ClientVisit的表格名称。

答案 1 :(得分:0)

也许这会有所帮助。 (使用case语句修改了查询)

select ClientVisit.client_id, sum(Successful_Day) as Successful_Day,Sum(Unsuccessful_Day) as Unsuccessful_Day
from
(
 SELECT ClientVisit.client_id ,
 Case when ZFORM_group_note_28.successful_day_752410 = 'Yes' then  COUNT(ZFORM_group_note_28.clientvisit_id ) else 0 end AS Successful_Day,
 Case when ZFORM_group_note_28.successful_day_752410 = 'No' then  COUNT(ZFORM_group_note_28.clientvisit_id ) else 0 end AS Unsuccessful_Day 
 FROM ClientVisit
 INNER JOIN ZFORM_group_note_28
 ON ZFORM_group_note_28.clientvisit_id = ClientVisit.clientvisit_id
 WHERE ClientVisit.visittype_id = 16
  AND ClientVisit.program_id = 5
  AND ClientVisit.rev_timein >= @param1
  AND ClientVisit.rev_timeout < DATEADD( DAY ,  1 , param2 )
 GROUP BY ClientVisit.client_id
)Clients
Group by ClientVisit.client_id

答案 2 :(得分:0)

这是我结束的最终代码解决了所有问题:

Select 
ClientVisit.client_id, 
Count(Case when ZFORM_group_note_28.successful_day_752410 = 'Yes' then 1  else null end) AS Successful_Day,
Count(Case when ZFORM_group_note_28.successful_day_752410 = 'No' then 1 else null end) AS Unsuccessful_Day

From ClientVisit 
Inner Join ZFORM_group_note_28 On ZFORM_group_note_28.clientvisit_id = ClientVisit.clientvisit_id 

Where ClientVisit.visittype_id = 16
AND ClientVisit.program_id = 5
And ZFORM_group_note_28.successful_day_752410 is not null
AND ClientVisit.rev_timein >= @param1
AND ClientVisit.rev_timeout < DATEADD( DAY ,  1 , @param2 )

Group By ClientVisit.client_id  

Order by ClientVisit.client_id