读书人

怎样改写这个SQL?该如何处理

发布时间: 2012-01-21 21:31:43 作者: rapoo

怎样改写这个SQL?

select sum(金额)as '出库金额 ' from
(
select isnull(sum(( c * k * ( h / 1000) * 7.85) * sl * dj),0.00)as 金额 from t_kc where type= '板材 'and status= '0 '
union all
select isnull(sum(zl * c * sl * dj),0.00) from t_kc where type= '型材 'and status= '0 '
union all
select isnull(sum(sl * dj),0.00) from t_kc where type= '标准件 'and status= '0 '
union all
select isnull(sum(sl * dj),0.00) from t_kc where type= '消耗材料 'and status= '0 '

) a


go

select sum(金额)as '退库金额 ' from
(
select isnull(sum(( c * k * ( h / 1000) * 7.85) * sl * dj),0.00)as 金额 from t_kc where type= '板材 'and status= '2 '
union all
select isnull(sum(zl * c * sl * dj),0.00) from t_kc where type= '型材 'and status= '2 '
union all
select isnull(sum(sl * dj),0.00) from t_kc where type= '标准件 'and status= '2 '
union all
select isnull(sum(sl * dj),0.00) from t_kc where type= '消耗材料 'and status= '2 '

) b

------------------------------------

以上语句执行没有错误,但是执行结果是1列,2行记录,就是这样:
出库金额
1000
退库金额
400
--------------------------------------------
我想得到这样的结果
出库金额 退库金额
1000 4000
就是放到一个表内,一行2列.

该怎样做呢??谢谢大家.



[解决办法]

select sum(CASE status WHEN '0 ' THEN 金额 ELSE 0 END) as '出库金额 ',
sum(CASE status WHEN '2 ' THEN 金额 ELSE 1 END) as '退库金额 ' from
(

select isnull(sum(( c * k * ( h / 1000) * 7.85) * sl * dj),0.00)as 金额 from t_kc where type= '板材 'and status IN ( '0 ', '2 ')
union all
select isnull(sum(zl * c * sl * dj),0.00) from t_kc where type= '型材 'and status IN ( '0 ', '2 ')
union all
select isnull(sum(sl * dj),0.00) from t_kc where type= '标准件 'and status IN ( '0 ', '2 ')
union all
select isnull(sum(sl * dj),0.00) from t_kc where type= '消耗材料 'and status IN ( '0 ', '2 ')

) a

[解决办法]
--try

select [出库金额]=
(
select sum(金额)as '出库金额 ' from
(
select isnull(sum(( c * k * ( h / 1000) * 7.85) * sl * dj),0.00)as 金额 from t_kc where type= '板材 'and status= '0 '
union all
select isnull(sum(zl * c * sl * dj),0.00) from t_kc where type= '型材 'and status= '0 '
union all
select isnull(sum(sl * dj),0.00) from t_kc where type= '标准件 'and status= '0 '


union all
select isnull(sum(sl * dj),0.00) from t_kc where type= '消耗材料 'and status= '0 '
) a
),
[退库金额]=
(
select sum(金额)as '退库金额 ' from
(
select isnull(sum(( c * k * ( h / 1000) * 7.85) * sl * dj),0.00)as 金额 from t_kc where type= '板材 'and status= '2 '
union all
select isnull(sum(zl * c * sl * dj),0.00) from t_kc where type= '型材 'and status= '2 '
union all
select isnull(sum(sl * dj),0.00) from t_kc where type= '标准件 'and status= '2 '
union all
select isnull(sum(sl * dj),0.00) from t_kc where type= '消耗材料 'and status= '2 '
) b
)
into #T


[解决办法]

select 出库金额=(select sum(金额)as '出库金额 ' from
(
select isnull(sum(( c * k * ( h / 1000) * 7.85) * sl * dj),0.00)as 金额 from t_kc where type= '板材 'and status= '0 '
union all
select isnull(sum(zl * c * sl * dj),0.00) from t_kc where type= '型材 'and status= '0 '
union all
select isnull(sum(sl * dj),0.00) from t_kc where type= '标准件 'and status= '0 '
union all
select isnull(sum(sl * dj),0.00) from t_kc where type= '消耗材料 'and status= '0 '

) a),退库金额=(select sum(金额)as '退库金额 ' from
(
select isnull(sum(( c * k * ( h / 1000) * 7.85) * sl * dj),0.00)as 金额 from t_kc where type= '板材 'and status= '2 '
union all
select isnull(sum(zl * c * sl * dj),0.00) from t_kc where type= '型材 'and status= '2 '
union all
select isnull(sum(sl * dj),0.00) from t_kc where type= '标准件 'and status= '2 '
union all
select isnull(sum(sl * dj),0.00) from t_kc where type= '消耗材料 'and status= '2 '

) b)

读书人网 >SQL Server

热点推荐