2016-12-16 1 views
0

Ich brauche Sie zu helfen.Interbase SQL remake a Wählen Sie die Abfrage

Ich habe eine Interbase SQL-Select-Abfrage:

Select aps.fullname, aps.productvcode, aps.alccode, aps.capacity, Guss (Round (Sum (Fall, wenn wp.capacity> 0 dann wp .Quantity wp.capacity */10 * anderes wp.quantity wp.capacity Ende), 4) als DECIMAL (18,4)), wie von WBTotal AP aps links Waybill_positions verbinden wp auf wp.alccode = aps.alccode wo wp.alccode = '0001821000001389010' Gruppe von aps.fullname, aps.productvcode, aps.alccode, aps.capacity
union all
Select aps.fullname aps.productvcode, aps.alccode, aps.capacity, Besetzung (Round (Summe (Fall bei wp.capacity> 0 dann wp.quantity * wp.capacity/10 sonst wp.quantity * wp.capacity end), 4) als DECIMAL (18,4)) als WBTotal von AP aps links beitreten Waybill_out_positions wp auf wp.alccode = aps.alccode wo wp.alccode = '0001821000001389010' Gruppe von aps.fullname, aps.productvcode, aps.alccode , aps.capacity

Und es gibt mir dies: enter image description here

Aber ich möchte den Wert "156,9750" aus der zweiten Reihe sein eine separate Spalte rechts neben „WBTOTAL“ und hat den Titel „WBOTOTAL“

Wie es zu tun?

Antwort

1

können Sie CTE verwenden, wie unten

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 
+0

Dank Arbeitete ... .. Kann ich nur die gleiche Art und Weise andere Tabellen hinzufügen? – SovereignSun

+1

@SovereignSun, sicher, dass Sie mehrere CTE in einer Abfrage haben können. Sie können mehr Referenz in diesem Link haben https://msdn.microsoft.com/en-us/library/ms175972.aspx –

+0

Vielen Dank! – SovereignSun