group by with negative snd positive amont

User avatar
sal21
PlatinumLounger
Posts: 4364
Joined: 26 Apr 2010, 17:36

group by with negative snd positive amont

Post by sal21 »

I have 2 field:
Cod and Aumount
I can have:
Aaa 100, 00
Aaa -56,00
Aaa 88,00

I neef to group by Cod and retrivd two separate summs of Aumont similar:
Cod plus minus
Aaa 188, 00 -56, 00
How to in sql?

Note:
Aumont is Double Cod is Text
Last edited by sal21 on 21 Nov 2013, 10:44, edited 1 time in total.

User avatar
HansV
Administrator
Posts: 78558
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: group by with ndgstive snd poditive aumont

Post by HansV »

SELECT Cod, Sum(IIf([Amount]>0,[Amount],0)) AS Plus, Sum(IIf([Amount]<0,[Amount],0)) AS Minus
FROM MyTable
GROUP BY Cod
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4364
Joined: 26 Apr 2010, 17:36

Re: group by with ndgstive snd poditive aumont

Post by sal21 »

HansV wrote:SELECT Cod, Sum(IIf([Amount]>0,[Amount],0)) AS Plus, Sum(IIf([Amount]<0,[Amount],0)) AS Minus
FROM MyTable
GROUP BY Cod
As usual tkx.
Sorry but i need a param from the two result:
... whre the summs minus is >= 5000, 00 and summs of plus >= 5000,00
Tkx

User avatar
HansV
Administrator
Posts: 78558
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: group by with ndgstive snd poditive aumont

Post by HansV »

I don't know if this will work, but try

SELECT Cod, Sum(IIf([Amount]>0,[Amount],0)) AS Plus, Sum(IIf([Amount]<0,[Amount],0)) AS Minus
FROM MyTable
GROUP BY Cod
HAVING Sum(IIf([Amount]>0,[Amount],0))>=5000 AND Sum(IIf([Amount]<0,[Amount],0))<=-5000
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4364
Joined: 26 Apr 2010, 17:36

Re: group by with ndgstive snd poditive aumont

Post by sal21 »

HansV wrote:I don't know if this will work, but try

SELECT Cod, Sum(IIf([Amount]>0,[Amount],0)) AS Plus, Sum(IIf([Amount]<0,[Amount],0)) AS Minus
FROM MyTable
GROUP BY Cod
HAVING Sum(IIf([Amount]>0,[Amount],0))>=5000 AND Sum(IIf([Amount]<0,[Amount],0))<=-5000

Hans the query work perfect...

but the query return the two result if condition is respected from the two param. >=5000 and <=-5000.

in my case i have:

10.020,00
1.400,47

i need:
11.420,47 -00,00

User avatar
HansV
Administrator
Posts: 78558
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: group by with negative snd positive amont

Post by HansV »

Does this do what you want?

Code: Select all

SELECT A.Cod, Val(Nz(B.Plus,0)) AS Plus, Val(Nz(C.Minus,0)) AS Minus
FROM 
    ((SELECT DISTINCT Cod FROM MyTable) AS A 
    LEFT JOIN 
    (SELECT Cod, Sum(IIf([Amount]>0,[Amount],0)) AS Plus
        FROM MyTable
        GROUP BY Cod
        HAVING Sum(IIf([Amount]>0,[Amount],0))>=5000) AS B
    ON A.Cod = B.Cod)
    LEFT JOIN
    (SELECT Cod, Sum(IIf([Amount]<0,[Amount],0)) AS Minus
        FROM MyTable
        GROUP BY Cod
        HAVING Sum(IIf([Amount]<0,[Amount],0))<=-5000) AS C
    ON A.Cod = C.Cod
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4364
Joined: 26 Apr 2010, 17:36

Re: group by with negative snd positive amont

Post by sal21 »

HansV wrote:Does this do what you want?

Code: Select all

SELECT A.Cod, Val(Nz(B.Plus,0)) AS Plus, Val(Nz(C.Minus,0)) AS Minus
FROM 
    ((SELECT DISTINCT Cod FROM MyTable) AS A 
    LEFT JOIN 
    (SELECT Cod, Sum(IIf([Amount]>0,[Amount],0)) AS Plus
        FROM MyTable
        GROUP BY Cod
        HAVING Sum(IIf([Amount]>0,[Amount],0))>=5000) AS B
    ON A.Cod = B.Cod)
    LEFT JOIN
    (SELECT Cod, Sum(IIf([Amount]<0,[Amount],0)) AS Minus
        FROM MyTable
        GROUP BY Cod
        HAVING Sum(IIf([Amount]<0,[Amount],0))<=-5000) AS C
    ON A.Cod = C.Cod
i cannot test now...

if i would to trnsform the number -125.004,45 to 125.004,45, i need to use a simple:

val(mytable.amont)

or?

User avatar
HansV
Administrator
Posts: 78558
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: group by with negative snd positive amont

Post by HansV »

You can use

Code: Select all

SELECT A.Cod, Val(Nz(B.Plus,0)) AS Plus, Val(Nz(C.Minus,0)) AS Minus
FROM 
    ((SELECT DISTINCT Cod FROM MyTable) AS A 
    LEFT JOIN 
    (SELECT Cod, Sum(IIf([Amount]>0,[Amount],0)) AS Plus
        FROM MyTable
        GROUP BY Cod
        HAVING Sum(IIf([Amount]>0,[Amount],0))>=5000) AS B
    ON A.Cod = B.Cod)
    LEFT JOIN
    (SELECT Cod, -Sum(IIf([Amount]<0,[Amount],0)) AS Minus
        FROM MyTable
        GROUP BY Cod
        HAVING -Sum(IIf([Amount]<0,[Amount],0))>=5000) AS C
    ON A.Cod = C.Cod
Best wishes,
Hans