当前位置: 代码迷 >> Sql Server >> 有个有关问题 sql语句求帮助
  详细解决方案

有个有关问题 sql语句求帮助

热度:73   发布时间:2016-04-24 10:06:22.0
有个问题 sql语句求帮助
select * from (
 select QNQW,SZName,Pm,sum(JY) as JY,sum(ZCJ) as ZCJ,CJunit from (
select UnitId as FhUnitId, QrDate as Qfrq,QNQW, SZName,Pm,(ZCJ-YYZCJ)as JY,ZCJ,CJunit from(
select (case QNW when '其它' then newTypes else QNW end ) as QNQW,a.* from(
 select (case Types when 2 then '区内' when 3 then '区外' when 4 then '区外' when 5 then '其它' when 6 then '其它' else '不计' end) as QNW,* from Storage
)as a left join (
 select (case ycd when 3 then '区内' else '区外' end) as newTypes,* from Plan_Plubic 
)as b on a.MainId = b.Id 
) as c where QNQW !='不计' and YXState='有效' 
 ) as d group by Pm,QNQW,SZName,CJunit
) as e

这是部分源码 


这是效果图 
现在我要把QNQW这字段拆为QN QW 2个 对应显示的ZCJ的数据 该怎么做 sql2005的
 help!
------解决方案--------------------
我发现你希望直接能运行的代码而已

SELECT  SZName ,
        pm ,
        qn = CASE WHEN qnqw = '区内' THEN zcj
                  ELSE 0.00
             END ,
        qw = CASE WHEN qnqw = '区外' THEN zcj
                  ELSE 0.00
             END ,
        JY ,
        CJUnit ,
        qnqw
FROM    ( SELECT    *
          FROM      ( SELECT    QNQW ,
                                SZName ,
                                Pm ,
                                SUM(JY) AS JY ,
                                SUM(ZCJ) AS ZCJ ,
                                CJunit
                      FROM      ( SELECT    UnitId AS FhUnitId ,
                                            QrDate AS Qfrq ,
                                            QNQW ,
                                            SZName ,
                                            Pm ,
                                            ( ZCJ - YYZCJ ) AS JY ,
                                            ZCJ ,
                                            CJunit
  相关解决方案