Interbase SQL 重做一个 Select 查询
Interbase SQL remake a Select query
我需要你的帮助。
我有一个 Interbase SQL Select 查询:
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
union all
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
它给了我这个:
但我希望第二行的值 "156,9750" 成为 "WBTOTAL"[= 右侧的单独一列28=] 并且标题为 "WBOTOTAL"
怎么做?
你可以像下面这样使用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
我需要你的帮助。
我有一个 Interbase SQL Select 查询:
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
union all
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
它给了我这个:
但我希望第二行的值 "156,9750" 成为 "WBTOTAL"[= 右侧的单独一列28=] 并且标题为 "WBOTOTAL"
怎么做?
你可以像下面这样使用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