Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Union and select on 2 fields in the same table

Author  Topic 

Lesombrero
Starting Member

43 Posts

Posted - 2013-06-15 : 12:38:51
Hello Everybody,
Here is my problem:

Table Z_GLivre:
konto, Umsatz, Gegenkonto, SH
1200, 25.97, 4650, H
1200, 11.13, 4650, H
1200, 17.52, 4824, NULL
1200, 51.44, 4824, NULL
1755, 300.00, 4190, H
1755, 6.00, 4190, H
1755, 86.97, 4250, H
1755, 288.30, 4190, NULL
1755, 104.67, 4190, NULL

I expect an output like:
TheKonto TotalCredit TotalDebit
1200, 37.10, 68.96
1755, 392.97, 392.97
4190, 306.00, 392.97
4250, 86.97, 0.00
4650, 37.10, 0.00
4824, 0.00, 68,96

I tried this:
-- Part I
SELECT Konto as TheKonto,
SUM(Umsatz) as TotalCredit
FROM Z_GLivre
WHERE Konto > 0 AND SH = 'H'
GROUP by Konto
UNION
SELECT Gegenkonto as TheKonto,
SUM(Umsatz) as TotalCredit
FROM Z_GLivre
WHERE Gegenkonto > 0 AND SH = 'H'
GROUP by Gegenkonto,
-- Part II
SELECT Konto as TheKonto,
SUM(Umsatz) as TotalDebit
FROM Z_GLivre
WHERE Konto > 0 AND SH IS NULL
GROUP by Konto
UNION
SELECT Gegenkonto as TheKonto,
SUM(Umsatz) as TotalDebit
FROM Z_GLivre
WHERE Gegenkonto > 0 AND SH IS NULL
GROUP by Gegenkonto
ORDER by TheKonto

Part I is fine, Part II give an error (of course)

Any ideas?
Thanks in advance

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-15 : 12:57:13
Sorry your output doesnt match with posted sample data.

Where did you get 1210 etc value from?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Lesombrero
Starting Member

43 Posts

Posted - 2013-06-15 : 13:13:46
It was an example. There is arround 50 different Konto #
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-15 : 13:18:18
please post sample data and then give output from it. that will help us to understand your requirement clearly.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Lesombrero
Starting Member

43 Posts

Posted - 2013-06-15 : 13:21:46
Done!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-15 : 13:26:18
[code]
SELECT Konto,
SUM(CASE WHEN SH = 'H' THEN Umsatz ELSE 0 END) AS TotalCredit,
SUM(CASE WHEN SH IS NULL THEN Umsatz ELSE 0 END) AS TotalDebit
FROM Table
GROUP BY Konto
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Lesombrero
Starting Member

43 Posts

Posted - 2013-06-15 : 13:37:16
Why do I always want to make it so complicated when it can be simple !!!
Thank you so much visakh16
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-15 : 13:45:37
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Lesombrero
Starting Member

43 Posts

Posted - 2013-06-15 : 13:54:38
I actualy missed results in the output, who make the all sense of my first question! Could you please have a look?
Sorry about that.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-15 : 15:19:54
[code]
SELECT Konto,
SUM(CASE WHEN SH = 'H' THEN Umsatz ELSE 0 END) AS TotalCredit,
SUM(CASE WHEN SH IS NULL THEN Umsatz ELSE 0 END) AS TotalDebit
FROM Table
GROUP BY Konto
UNION ALL
SELECT Gegenkonto,
SUM(CASE WHEN SH = 'H' THEN Umsatz ELSE 0 END) AS TotalCredit,
SUM(CASE WHEN SH IS NULL THEN Umsatz ELSE 0 END) AS TotalDebit
FROM Table
GROUP BY Gegenkonto
[/code]


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Lesombrero
Starting Member

43 Posts

Posted - 2013-06-15 : 15:32:37
Thanks Visakh,
I tried this already, but I have Kontos coming out in double.
Ex.:
1200, 367105.62, 379174.44
1210, 124529.78, 126030.95
1360, 6000.00, 6000.00
1361, 2951.95, 19611.48
1361, 1270.07, 16975.44
1362, 3679.19, 3679.19
1570, 16098.98, 2554.06
1588, 1128.42, 0.00
1590, 24549.64, 13194.28
1590, 12269.55, 3857.20
Go to Top of Page

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-06-15 : 19:33:35
Try this:
[CODE]

SELECT Konto,
SUM(CASE WHEN SH = 'H' THEN Umsatz ELSE 0 END) AS TotalCredit,
SUM(CASE WHEN SH IS NULL THEN Umsatz ELSE 0 END) AS TotalDebit
FROM (
SELECT Konto, Umsatz, SH FROM Z_GLivre
UNION ALL
SELECT Gegenkonto, Umsatz, SH FROM Z_GLivre
) A
GROUP BY konto

[/CODE]
Go to Top of Page

Lesombrero
Starting Member

43 Posts

Posted - 2013-06-16 : 04:09:39
Thank you very much MuMu88. It works perfectly.

Now, I have an other one ;-)
Similar table, so we can use the same example, but I need to do the total differently.
The total won't be on:
Konto + Gegenkonto Where SH = 'H' As TotalCredit
Konto + Gegenkonto Where SH = '' As TotalDebit

BUT
Konto Where SH = 'H' + Gegenkonto Where SH IS NULL As TotalCredit
Konto Where SH IS NULL + Gegenkonto Where SH = 'H' As TotalDebit

Any ideas ?
Go to Top of Page

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-06-16 : 10:28:29
[CODE]


SELECT Konto,
SUM(CASE WHEN ((SH = 'H' AND K = 1) OR (SH IS NULL AND K =2)) THEN Umsatz ELSE 0 END) AS TotalCredit,
SUM(CASE WHEN ((SH = 'H' AND K = 2) OR (SH IS NULL AND K =1)) THEN Umsatz ELSE 0 END) AS TotalDebit
FROM (
SELECT 1 as K, Konto, Umsatz, SH FROM @Z_GLivre
UNION ALL
SELECT 2 as K, Gegenkonto, Umsatz, SH FROM @Z_GLivre
) A
GROUP BY konto

[/CODE]
Go to Top of Page

Lesombrero
Starting Member

43 Posts

Posted - 2013-06-29 : 09:34:37
Thanks a lot MuMu88. That is exactly what I needed! And sorry for not responding earlier (I was away...).
Go to Top of Page

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-06-29 : 11:23:58
Glad to help.

quote:
Originally posted by Lesombrero

Thanks a lot MuMu88. That is exactly what I needed! And sorry for not responding earlier (I was away...).

Go to Top of Page
   

- Advertisement -