从case语句中执行Count

时间:2018-02-27 15:56:06

标签: sql sql-server sql-server-2012 count case

我有以下查询:

SELECT DISTINCT cst_recno as 'MemID',
 evt_code as 'MeetingID',
 etp_code as 'Event Type',
 evt_start_date as 'Event Start Date',
case 
       when net_ivd_amount_cp=0 
       then 'Comp' 
       else 'Paid' 
end as 'P/C'
FROM 
ev_registrant  
join ev_event on evt_key=reg_evt_key
LEFT JOIN ev_event_type ON evt_etp_key = etp_key 
LEFT JOIN ev_event_location  ON evl_evt_key = evt_key 
LEFT JOIN ev_location  ON evl_loc_key = loc_key 
LEFT join ac_invoice_detail on reg_ivd_key=ivd_key
LEFT join vw_ac_invoice_detail on net_ivd_key=ivd_key
JOIN co_customer ON reg_cst_key=cst_key and cst_delete_flag=0 and reg_delete_flag=0 
WHERE reg_cancel_date is NULL AND reg_delete_flag = 0 and reg_attendance_flag=1
and evt_start_date >='7/1/2017' and evt_title = 'uli europe conference 2018'
order by cst_recno

给我以下输出:

MemID   MeetingID   Event Type  Event Start Date      P/C      
14191   33100418    Conference  2018-01-30 00:00:00   Paid  
16544   33100418    Conference  2018-01-30 00:00:00   Comp  
16592   33100418    Conference  2018-01-30 00:00:00   Paid
17415   33100418    Conference  2018-01-30 00:00:00   Comp  
18531   33100418    Conference  2018-01-30 00:00:00   Paid  
19922   33100418    Conference  2018-01-30 00:00:00   Comp  
21207   33100418    Conference  2018-01-30 00:00:00   Paid
22832   33100418    Conference  2018-01-30 00:00:00   Comp  
26346   33100418    Conference  2018-01-30 00:00:00   Comp  
27607   33100418    Conference  2018-01-30 00:00:00   Paid  
33674   33100418    Conference  2018-01-30 00:00:00   Paid

我想添加两列可以计算P / C列的不同值,并将该计数分配给整列,以便我所需的输出如下所示:

MemID   MeetingID   Event Type  Event Start Date      P/C    Paid  Comp
14191   33100418    Conference  2018-01-30 00:00:00   Paid   6     5
16544   33100418    Conference  2018-01-30 00:00:00   Comp   6     5
16592   33100418    Conference  2018-01-30 00:00:00   Paid   6     5
17415   33100418    Conference  2018-01-30 00:00:00   Comp   6     5
18531   33100418    Conference  2018-01-30 00:00:00   Paid   6     5
19922   33100418    Conference  2018-01-30 00:00:00   Comp   6     5
21207   33100418    Conference  2018-01-30 00:00:00   Paid   6     5
22832   33100418    Conference  2018-01-30 00:00:00   Comp   6     5
26346   33100418    Conference  2018-01-30 00:00:00   Comp   6     5
27607   33100418    Conference  2018-01-30 00:00:00   Paid   6     5
33674   33100418    Conference  2018-01-30 00:00:00   Paid   6     5

3 个答案:

答案 0 :(得分:1)

您可以使用窗口功能:

select . . .,
       sum(case when net_ivd_amount_cp = 0 then 1 else 0 end) over (partition by MeetingID) as comp,
       sum(case when net_ivd_amount_cp <> 0 or net_ivd_amount_cp is null then 1 else 0 end) over (partition by MeetingID) as paid

我怀疑select distinct。我不明白为什么这个查询需要这个。在某些情况下,窗口函数不会使用select distinct生成您期望的结果。

答案 1 :(得分:0)

我认为使用下面的cte可能会对您有所帮助:

;with cte (MemID,MeetingID,EventType,EventStartDate,PC) as
(
   SELECT DISTINCT cst_recno as 'MemID',
   evt_code as 'MeetingID',
   etp_code as 'EventType',
   evt_start_date as 'EventStartDate',
   case 
       when net_ivd_amount_cp=0 
       then 'Comp' 
       else 'Paid' 
   end as 'PC'
   FROM 
   ev_registrant  
   join ev_event on evt_key=reg_evt_key
   LEFT JOIN ev_event_type ON evt_etp_key = etp_key 
   LEFT JOIN ev_event_location  ON evl_evt_key = evt_key 
   LEFT JOIN ev_location  ON evl_loc_key = loc_key 
   LEFT join ac_invoice_detail on reg_ivd_key=ivd_key
   LEFT join vw_ac_invoice_detail on net_ivd_key=ivd_key
   JOIN co_customer ON reg_cst_key=cst_key and cst_delete_flag=0 and reg_delete_flag=0 
   WHERE reg_cancel_date is NULL AND reg_delete_flag = 0 and reg_attendance_flag=1
   and evt_start_date >='7/1/2017' and evt_title = 'uli europe conference 2018'
   order by cst_recno
)
select *,
       (select count(*) from cte where PC = 'Paid') as Paid,
       (select count(*) from cte where PC = 'Comp') as Comp
from cte

答案 2 :(得分:0)

只需使用子查询:

SELECT
  *
  ,(SELECT COUNT([P/C]) FROM tbl WHERE [P/C] = 'Paid') AS Paid
  ,(SELECT COUNT([P/C]) FROM tbl WHERE [P/C] = 'Comp') AS Comp
 FROM (
     SELECT DISTINCT cst_recno as 'MemID',
     evt_code as 'MeetingID',
     etp_code as 'Event Type',
     evt_start_date as 'Event Start Date',
    case 
           when net_ivd_amount_cp=0 
           then 'Comp' 
           else 'Paid' 
    end as 'P/C'
    FROM 
    ev_registrant  
    join ev_event on evt_key=reg_evt_key
    LEFT JOIN ev_event_type ON evt_etp_key = etp_key 
    LEFT JOIN ev_event_location  ON evl_evt_key = evt_key 
    LEFT JOIN ev_location  ON evl_loc_key = loc_key 
    LEFT join ac_invoice_detail on reg_ivd_key=ivd_key
    LEFT join vw_ac_invoice_detail on net_ivd_key=ivd_key
    JOIN co_customer ON reg_cst_key=cst_key and cst_delete_flag=0 and reg_delete_flag=0 
    WHERE reg_cancel_date is NULL AND reg_delete_flag = 0 and reg_attendance_flag=1
    and evt_start_date >='7/1/2017' and evt_title = 'uli europe conference 2018'
    ) tbl
order by MemID