2017-02-28 5 views
0

Können Sie mir bitte helfen Sie mir dabei? Ich habe eine Abfrage geschrieben DocumentNo, Whscode und NetAmmount jedes Dokument zu erhalten, wie unten gezeigt:
Summe von Summe in SQL-Server

SELECT t1.whscode, 
CASE WHEN t0.[DiscPrcnt]>0 then ((sum(t1.LineTotal) -isnull(t0.dpmamnt,0))- ((sum(t1.LineTotal)-isnull(t0.dpmamnt,0)) * t0.[DiscPrcnt]/100)) +t0.VatSum 
ELSE ((sum(t1.LineTotal)-isnull(t0.dpmamnt,0))) END 
as 'NetAmount',t0.docnum 
from [dbo].[OINV] T0 (NOLOCK) INNER JOIN [dbo].[INV1] T1 (NOLOCK) ON T0. [DocEntry] = T1.[DocEntry] 
where t0.docdate between '02-09-17' and '02-10-17' and 
t1.WhsCode='kidst'and t0.CANCELED!='Y' and t1.targettype!=13 
group by T0.[CardName],t0.[taxdate],t0.[docduedate], T0.[DocStatus], t0.[doctotal]-t0.[vatsum], 
t1.whscode,t0.DocNum,t0.usersign,t0.DiscPrcnt,t0.dpmamnt,t0.vatsum 
having (sum(t1.LineTotal)-isnull(t0.dpmamnt,0))>0 


Ausgang:

|whscode| NetAmount | docnum| 
    |KIDST | 2147.419293 |3411592| 
    |KIDST | 19.000011 |3411670| 
    |KIDST | 23.380000 |3411314| 
    |KIDST | 50.000000 |3412061| 
    |KIDST | 268.720000 |3412000| 
    |KIDST | 69.930000 |3412289| 

Nun möchte Ich mag die Ausgabe als Whscode erhalten und Summe von NetAmount wie:

|Whscode| NetAmount | 
    KIDST----2578.449 

Wenn ich tun sum (Fall bis Ende) wirft einen Fehler von Kann eine Aggregatfunktion für einen Ausdruck, der ein Aggregat oder eine Unterabfrage enthält, nicht ausführen.
Bitte kann mir jemand dabei helfen.

+0

Entfernen Sie das Feld ‚DOCNUM‘ aus der Abfrage (von beiden wählen und Gruppe) –

+0

Wenn ich auch seine zeigt denselben Fehler zu entfernen. SELECT t1.whscode, Summe (FALL WENN t0. [DiscPrcnt]> 0 dann ((Summe (t1.LineTotal) -isnull (t0.dpmamnt, 0)) - ((Summe (t1.LineTotal) -isnull (t0 .dpmamnt, 0)) * t0. [DiscPrcnt]/100)) + t0.VatSum ELSE ((Summe (t1.LineTotal) -isnull (t0.dpmamnt, 0))) END) als 'NetAmount' von [dbo]. [OINV] T0 (NOLOCK) INNERER VERBINDUNG [dbo]. [INV1] T1 (NOLOCK) EIN T0. [DocEntry] = T1. [DocEntry] wo t0.docdate zwischen '02 -09-17 'und '02 -10-17' und t1.WhsCode = 'KidsT' und t0.CANCELED! = 'Y' und t1.targettype! = 13 Gruppe von t1.whscode –

Antwort

1

Bitte versuchen Sie dies:

;with cte as (
SELECT t1.whscode, 
CASE WHEN t0.[DiscPrcnt]>0 then ((sum(t1.LineTotal) -isnull(t0.dpmamnt,0))- ((sum(t1.LineTotal)-isnull(t0.dpmamnt,0)) * t0.[DiscPrcnt]/100)) +t0.VatSum 
ELSE ((sum(t1.LineTotal)-isnull(t0.dpmamnt,0))) END 
as 'NetAmount',t0.docnum 
from [dbo].[OINV] T0 (NOLOCK) INNER JOIN [dbo].[INV1] T1 (NOLOCK) ON T0. [DocEntry] = T1.[DocEntry] 
where t0.docdate between '02-09-17' and '02-10-17' and 
t1.WhsCode='kidst'and t0.CANCELED!='Y' and t1.targettype!=13 
group by T0.[CardName],t0.[taxdate],t0.[docduedate], T0.[DocStatus], t0.[doctotal]-t0.[vatsum], 
t1.whscode,t0.DocNum,t0.usersign,t0.DiscPrcnt,t0.dpmamnt,t0.vatsum 
having (sum(t1.LineTotal)-isnull(t0.dpmamnt,0))>0 
) 
select whscode,sum(Netamount) as NetAmount 
from cte 
group by whscode 
+0

gleichen Fehler. Eine Aggregatfunktion für einen Ausdruck, der ein Aggregat oder eine Unterabfrage enthält, kann nicht ausgeführt werden. –

+0

können Sie versuchen, es in 'Temptable' einzufügen? dann bekomme ich die Summe von 'NetAmount' –

+0

Ja ich habe es gleich versucht, immer noch kein Glück. Derselbe Fehler zeigt –