日期:2014-05-17 浏览次数:20614 次
select ZYH, KZYS as YS, len(SFBZBH) as LB, (select sum(SJFY) from SFD_2 where SFLB='1501' and DJ>=1500 )as HZJE from SFD_2 where convert(char(8),RQ,112)>='20120701' group by FSKS, len(SFBZBH) ,KZYS,ZYH
------解决方案--------------------
SELECT ZYH , KZYS YS , LEN(SFBZBH) LB , SUM(SJFY) HZJE, sum(CASE WHEN SFLB='1501' and DJ>=1500 THEN sjfy ELSE 0 END ) AS 新求和 FROM SFD_2 WHERE CONVERT(CHAR(8), RQ, 112) >= '20120701' GROUP BY FSKS , LEN(SFBZBH) , KZYS , ZYH
------解决方案--------------------
嵌套子查询即可
------解决方案--------------------
select ZYH, KZYS as YS, len(SFBZBH) as LB, SUM(SJFY) HZJE, (select sum(SJFY) from SFD_2 where SFLB='1501' and DJ>=1500 )as 新求和 from SFD_2 where convert(char(8),RQ,112)>='20120701' group by FSKS, len(SFBZBH) ,KZYS,ZYH
------解决方案--------------------
select ZYH,KZYS YS,len(SFBZBH) LB,sum(case when SFLB='1501' and DJ>=1500 then SJFY else 0 end) HZJE from SFD_2 where convert(char(8),RQ,112)>='20120701' group by FSKS, len(SFBZBH) ,KZYS,ZYH
------解决方案--------------------
新求和也要出现在group by中,忘记加了
------解决方案--------------------
--try select ZYH, KZYS as YS, len(SFBZBH) as LB, SUM(SJFY) HZJE, SUM(select SJFY from SFD_2 where SFLB='1501' and DJ>=1500 )as 新求和 from SFD_2 where convert(char(8),RQ,112)>='20120701' group by FSKS, len(SFBZBH) ,KZYS,ZYH
------解决方案--------------------
select ZYH, KZYS as YS, len(SFBZBH) as LB, SUM(SJFY) HZJE, SUM(select SJFY from SFD_2 where SFLB='1501' and DJ>=1500 AND KZYS = A.KZYS)as 新求和 from SFD_2 AS A where convert(char(8),RQ,112)>='20120701' group by FSKS, len(SFBZBH) ,KZYS,ZYH
------解决方案--------------------
二樓的不就是對的嘛
------解决方案--------------------