SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Union and select on 2 fields in the same table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Lesombrero
Starting Member

Germany
43 Posts

Posted - 06/15/2013 :  12:38:51  Show Profile  Reply with Quote
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

Edited by - Lesombrero on 06/15/2013 13:53:13

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 06/15/2013 :  12:57:13  Show Profile  Reply with Quote
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

Germany
43 Posts

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

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 06/15/2013 :  13:18:18  Show Profile  Reply with Quote
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

Germany
43 Posts

Posted - 06/15/2013 :  13:21:46  Show Profile  Reply with Quote
Done!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 06/15/2013 :  13:26:18  Show Profile  Reply with Quote

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


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

Lesombrero
Starting Member

Germany
43 Posts

Posted - 06/15/2013 :  13:37:16  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 06/15/2013 :  13:45:37  Show Profile  Reply with Quote
welcome

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

Lesombrero
Starting Member

Germany
43 Posts

Posted - 06/15/2013 :  13:54:38  Show Profile  Reply with Quote
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.

Edited by - Lesombrero on 06/15/2013 14:08:48
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 06/15/2013 :  15:19:54  Show Profile  Reply with Quote

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



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

Lesombrero
Starting Member

Germany
43 Posts

Posted - 06/15/2013 :  15:32:37  Show Profile  Reply with Quote
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

547 Posts

Posted - 06/15/2013 :  19:33:35  Show Profile  Reply with Quote
Try this:


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

Go to Top of Page

Lesombrero
Starting Member

Germany
43 Posts

Posted - 06/16/2013 :  04:09:39  Show Profile  Reply with Quote
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

547 Posts

Posted - 06/16/2013 :  10:28:29  Show Profile  Reply with Quote



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

Go to Top of Page

Lesombrero
Starting Member

Germany
43 Posts

Posted - 06/29/2013 :  09:34:37  Show Profile  Reply with Quote
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

547 Posts

Posted - 06/29/2013 :  11:23:58  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000