Interbase SQL重制了一个Select查询

时间:2016-12-16 09:22:20

标签: sql select sum case interbase

我需要你的帮助。

我有一个Interbase SQL Select查询:

  

选择aps.fullname,aps.productvcode,aps.alccode,aps.capacity,Cast(Round(总和(wp.capacity> 0然后wp.quantity * wp.capacity / 10的情况)   其他wp.quantity * wp.capacity结束),4)as DECIMAL(18,4))as WBTotal from   AP aps在wp.alccode = aps.alccode中加入Waybill_positions wp   wp.​​alccode ='0001821000001389010'group by aps.fullname,   aps.productvcode,aps.alccode,aps.capacity
  工会全部   选择aps.fullname,aps.productvcode,aps.alccode,aps.capacity,   施放(圆形(总和(wp.capacity> 0然后wp.quantity * wp.capacity / 10的情况)   其他wp.quantity * wp.capacity结束),4)as DECIMAL(18,4))as WBTotal from   AP aps在wp.alccode = aps.alccode上加入Waybill_out_positions wp   其中wp.alccode ='0001821000001389010'group by aps.fullname,   aps.productvcode,aps.alccode,aps.capacity

它给了我这个:enter image description here

但我希望第二行的值“156,9750”“WBTOTAL”旁边右侧的单独列,标题为 “WBOTOTAL”

怎么做?

1 个答案:

答案 0 :(得分:1)

您可以使用CTE,如下所示

with CTE1 as
(
    Select aps.fullname, aps.productvcode, aps.alccode, aps.capacity, Cast(Round(Sum(case when wp.capacity>0 then wp.quantity * wp.capacity/10 else wp.quantity * wp.capacity end),4) as DECIMAL(18,4)) as WBTotal from AP aps left join Waybill_positions wp on wp.alccode=aps.alccode where wp.alccode='0001821000001389010' group by aps.fullname, aps.productvcode, aps.alccode, aps.capacity
),
CTE2 as
(
    Select aps.fullname, aps.productvcode, aps.alccode, aps.capacity, Cast(Round(Sum(case when wp.capacity>0 then wp.quantity * wp.capacity/10 else wp.quantity * wp.capacity end),4) as DECIMAL(18,4)) as WBTotal from AP aps left join Waybill_out_positions wp on wp.alccode=aps.alccode where wp.alccode='0001821000001389010' group by aps.fullname, aps.productvcode, aps.alccode, aps.capacity
)
Select CTE1.fullname, CTE1.productvcode, CTE1.alccode, CTE1.capacity, CTE1.WBTotal, CTE2.WBTotal as WBOTOTAL
From  CTE1 
join  CTE2
on CTE1.productvcode = CTE2.productvcode
and CTE1.alccode = CTE2.alccode