当前位置: 代码迷 >> Sql Server >> 根据A表的条件COUNT(a),根据B表的条件COUNT(b),且两个条件无交集,怎么写
  详细解决方案

根据A表的条件COUNT(a),根据B表的条件COUNT(b),且两个条件无交集,怎么写

热度:46   发布时间:2016-04-27 13:20:00.0
根据A表的条件COUNT(a),根据B表的条件COUNT(b),且两个条件无交集,如何写?
SELECT 
COUNT(CASE WHEN ds.XKDM='010000' AND ds.LWPSZJ='是' THEN 1 ELSE NULL END) AS LWPSZJCount,
COUNT(CASE WHEN lw.XKDM='010000' AND lw.YJSFLM='1' THEN 1 ELSE NULL END) AS LWSSCount,
COUNT(CASE WHEN lw.XKDM='010000' AND lw.YJSFLM='2' THEN 1 ELSE NULL END) AS LWBSCount
FROM T_DS ds, T_LW lw
inner join T_YJSFL yjsfl on lw.YJSFLM=yjsfl.YJSFLM

结果是错的,可是不知道该怎么写了,求高手

------解决方案--------------------
SQL code
select * from  (select *,COUNT(CASE WHEN ds.XKDM='010000' AND ds.LWPSZJ='是' THEN 1 ELSE NULL END) AS LWPSZJCount) t1 , (select *,COUNT(CASE WHEN lw.XKDM='010000' AND lw.YJSFLM='1' THEN 1 ELSE NULL END) AS LWSSCount,COUNT(CASE WHEN lw.XKDM='010000' AND lw.YJSFLM='2' THEN 1 ELSE NULL END) AS LWBSCount) t2 where t1.YJSFLM=t2.YJSFLM
  相关解决方案