select sum(lh_fq) FROM LH_ZT where lh_lb='一组' and "date"=convert(varchar(10),getdate() - 1,120) union all select sum(lh_ss) FROM LH_ZT where lh_lb='一组' and "date"=convert(varchar(10),getdate() - 1,120) union all select sum(LH_ZT.lh_fq) FROM LH_ZT where lh_lb='二组' and "date"=convert(varchar(10),getdate() - 1,120) union all select sum(lh_ss) FROM LH_ZT where lh_lb='二组' and date=convert(varchar(10),getdate() - 1,120) union all select sum(LH_ZT.lh_fq) FROM LH_ZT where lh_lb='后续' and "date"=convert(varchar(10),getdate() - 1,120) union all select sum(lh_ss) FROM LH_ZT where lh_lb='后续' and "date"=convert(varchar(10),getdate() - 1,120) union all select sum(LH_ZT.lh_fq) FROM LH_ZT where lh_lb='批量' and "date"=convert(varchar(10),getdate() - 1,120) union all select sum(lh_ss) FROM LH_ZT where lh_lb='批量' and "date"=convert(varchar(10),getdate() - 1,120) union all select sum(LH_ZT.lh_fq) FROM LH_ZT where lh_lb='技术调整' and "date"=convert(varchar(10),getdate() - 1,120) union all select sum(LH_ZT.lh_fq) FROM LH_ZT where lh_lb!='批量' and "date"=convert(varchar(10),getdate() - 1,120) union all select sum(lh_ss) FROM LH_ZT where lh_lb!='批量' and "date"=convert(varchar(10),getdate() - 1,120)这是用union all写的查询,在显示结果时只显示一列数据,我想达到的效果是在一张表中能通过一条语句将lh_fq和lh_ss依据不同条件分列显示出来,求大神提供办法。
------解决方案--------------------
select case when lh_lb='一组' and "date"=convert(varchar(10),getdate() - 1,120) then sum(lh_fq) end,
case when lh_lb='一组' and "date"=convert(varchar(10),getdate() - 1,120) then sum(lh_ss) end
from tb
提供一个思路吧
------解决方案--------------------
SELECT lh_lb,
SUM(lh_fq),
SUM(lh_ss)
FROM LH_ZT
WHERE "date"=convert(varchar(10),getdate() - 1,120)
AND lh_lb IN ('一组' , '二组', ...)
GROUP BY lh_lb;