Click here to Skip to main content
15,920,614 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
MY Sql Query

SQL
select CONVERT(CHAR(4), dateofentry, 100) + CONVERT(CHAR(4), dateofentry, 120) as 'monthyear',
		SUM(amount) as 'Amount1',0 as 'Amount2' 
from MYtable 
where accountid=4 
group by CONVERT(CHAR(4), dateofentry, 100) + CONVERT(CHAR(4), dateofentry, 120)
union
select	CONVERT(CHAR(4), dateofentry, 100) + CONVERT(CHAR(4), dateofentry, 120) as 'monthyear',
		0 as 'Amount1',
		SUM(amount) as 'Amount2'
from MYtable
where accountid=3 
group by CONVERT(CHAR(4), dateofentry, 100) + CONVERT(CHAR(4), dateofentry, 120)


but when i execute query i am getting data in this way..
SQL
MonthYear     Amount1     Amount2
Jan 2015      0           100
Jan 2015      100         0
Feb 2015      200         0
Feb 2015      0           300
Mar 2015      400         0
Mar 2015      0           500


But i want Data in this format

SQL
MonthYear     Amount1     Amount2
Jan 2015      100         100
Feb 2015      200         300
Mar 2015      400         500


Thx in advance...

What I have tried:

SQL
select CONVERT(CHAR(4), dateofentry, 100) + CONVERT(CHAR(4), dateofentry, 120) as 'monthyear',
		SUM(amount) as 'Amount1',0 as 'Amount2' 
from MYtable 
where accountid=4 
group by CONVERT(CHAR(4), dateofentry, 100) + CONVERT(CHAR(4), dateofentry, 120)
union
select	CONVERT(CHAR(4), dateofentry, 100) + CONVERT(CHAR(4), dateofentry, 120) as 'monthyear',
		0 as 'Amount1',
		SUM(amount) as 'Amount2'
from MYtable
where accountid=3 
group by CONVERT(CHAR(4), dateofentry, 100) + CONVERT(CHAR(4), dateofentry, 120)
Posted
Updated 22-Jun-16 1:53am
v2
Comments
Kornfeld Eliyahu Peter 22-Jun-16 7:18am    
UNION works as excepted, it is just the wrong tool...Some JOIN and/or CTE would be better...
Ravi Sargam 22-Jun-16 7:21am    
how can i use my query with CTE? i am new to this.
Kornfeld Eliyahu Peter 22-Jun-16 7:22am    
If you are new start with JOIN...
Ravi Sargam 22-Jun-16 7:23am    
new to CTC not JOINs...

You don't need UNION's! You have to use CASE WHEN ... END.

So, the simplest way is:

SQL
SELECT CONVERT(CHAR(4), dateofentry, 100) + CONVERT(CHAR(4), dateofentry, 120) as 'monthyear',
		SUM(CASE WHEN accountid=4 THEN amount ELSE 0 END) as 'Amount1',
		SUM(CASE WHEN accountid=3 THEN amount ELSE 0 END) as 'Amount2' 
FROM MYtable 
GROUP BY CONVERT(CHAR(4), dateofentry, 100) + CONVERT(CHAR(4), dateofentry, 120)
 
Share this answer
 
v3
Comments
ZurdoDev 22-Jun-16 8:01am    
Good catch. I didn't go the extra mile to see why the union was there. +5.
Maciej Los 22-Jun-16 8:02am    
Thank you, Ryan.
Ravi Sargam 22-Jun-16 8:26am    
This one is short and simpl thank you.. maciej Los
Maciej Los 22-Jun-16 8:37am    
You're very welcome.
CHill60 22-Jun-16 8:50am    
5'd
As Peter said, UNION is doing exactly what it is supposed to do. An easy way around this is to wrap what you have inside another SELECT statement. Something like:

SQL
SELECT x.monthyear, SUM(x.amount1) AS amount1, SUM(x.amount2) as amount2
FROM (
  select CONVERT(CHAR(4), dateofentry, 100) + CONVERT(CHAR(4), dateofentry, 120) as   'monthyear',
  SUM(amount) as 'Amount1',0 as 'Amount2' 
  from MYtable 
  where accountid=4 
  group by CONVERT(CHAR(4), dateofentry, 100) + CONVERT(CHAR(4), dateofentry, 120)
  union
  select	CONVERT(CHAR(4), dateofentry, 100) + CONVERT(CHAR(4), dateofentry, 120)   as 'monthyear',
  0 as 'Amount1',
  SUM(amount) as 'Amount2'
  from MYtable
  where accountid=3 
  group by CONVERT(CHAR(4), dateofentry, 100) + CONVERT(CHAR(4), dateofentry, 120)
) x
GROUP BY x.monthyear
 
Share this answer
 
Comments
Ravi Sargam 22-Jun-16 7:27am    
Thx @RyanDev it's working very well..... thank you again.....
ZurdoDev 22-Jun-16 7:36am    
You're welcome.
ZurdoDev 22-Jun-16 8:10am    
Solution 2 by Maciej Los is a much better answer because you shouldn't be unioning anyway and Solution 2 will be much quicker as well. I didn't look closely at why you were unioning.
Maciej Los 22-Jun-16 7:54am    
A4, because there's is simplest way to achieve that -> using single SELECT statement + CASE WHEN ... END. Please, see my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900