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
group by with negative snd positive amont
-
- PlatinumLounger
- Posts: 4364
- Joined: 26 Apr 2010, 17:36
group by with negative snd positive amont
Last edited by sal21 on 21 Nov 2013, 10:44, edited 1 time in total.
-
- Administrator
- Posts: 78558
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: group by with ndgstive snd poditive aumont
SELECT Cod, Sum(IIf([Amount]>0,[Amount],0)) AS Plus, Sum(IIf([Amount]<0,[Amount],0)) AS Minus
FROM MyTable
GROUP BY Cod
FROM MyTable
GROUP BY Cod
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4364
- Joined: 26 Apr 2010, 17:36
Re: group by with ndgstive snd poditive aumont
As usual tkx.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
Sorry but i need a param from the two result:
... whre the summs minus is >= 5000, 00 and summs of plus >= 5000,00
Tkx
-
- Administrator
- Posts: 78558
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: group by with ndgstive snd poditive aumont
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
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
Hans
-
- PlatinumLounger
- Posts: 4364
- Joined: 26 Apr 2010, 17:36
Re: group by with ndgstive snd poditive aumont
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
-
- Administrator
- Posts: 78558
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: group by with negative snd positive amont
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
Hans
-
- PlatinumLounger
- Posts: 4364
- Joined: 26 Apr 2010, 17:36
Re: group by with negative snd positive amont
i cannot test now...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
if i would to trnsform the number -125.004,45 to 125.004,45, i need to use a simple:
val(mytable.amont)
or?
-
- Administrator
- Posts: 78558
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: group by with negative snd positive amont
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
Hans