选择查询中的多个选择

时间:2014-12-25 13:12:23

标签: sql

DECLARE @L25 int = (select po_det_branch_cd, count(po_det_quant) 
                    from po_details where po_det_date = '2014-12-25' 
                    and po_det_quant = 25 group by po_det_branch_cd);
DECLARE @L50 int = (select po_det_branch_cd, count(po_det_quant) 
                    from po_details where po_det_date = '2014-12-25' 
                    and po_det_quant = 50 group by po_det_branch_cd);
DECLARE @L100 int = (select po_det_branch_cd, count(po_det_quant) 
                    from po_details where po_det_date = '2014-12-25' 
                    and po_det_quant = 100 group by po_det_branch_cd);
DECLARE @L150 int = (select po_det_branch_cd, count(po_det_quant) 
                     from po_details where po_det_date = '2014-12-25' 
                     and po_det_quant = 150 group by po_det_branch_cd);

select @L25, @L50, @L100, @L150 from po_details
group by po_det_branch_cd

1 个答案:

答案 0 :(得分:0)

尝试这样

DECLARE @L25 int  
DECLARE @L50 int 
DECLARE @L100 int
DECLARE @L150 int

SELECT @L25 = SUM(CASE WHEN po_det_quant = 25 THEN 1 ELSE 0 END),
       @L50 = SUM(CASE WHEN po_det_quant = 50 THEN 1 ELSE 0 END),
       @L100 = SUM(CASE WHEN po_det_quant = 100 THEN 1 ELSE 0 END),
       @L150 = SUM(CASE WHEN po_det_quant = 150 THEN 1 ELSE 0 END)
FROM po_details 
WHERE po_det_date = '2014-12-25' 
GROUP BY po_det_branch_cd