Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

a good day to all of you. I am a newbie in SQL queries and I would like to ask a question in an existing query I have.
SQL
SELECT 
 SUM (SPEND) SPEND1,
 FROM 
 TIME
 WHERE LTM = 1


Now I need to modify this query to add another column called SPEND2 but this time, it's where clause shouldn't be LTM=1, it should be
L4M = 1<br />

I can't do LTM=1 and L4M = 1 because the where clause will apply to both SPEND1 and SPEND2.
Spend1 where should be LTM=1 and Spend2 should be
L4M=1<br />

they should be in the same query.

I would like to do this :

SQL
Select
Sum (Spend) as Spend1 where LTM = 1
Sum (Spend) as Spend2 where L4M = 1


I want to thank you for your advance help and do let me know if my question is confusing and I will gladly and immidiately clarify.
Posted
Updated 10-Jul-14 0:46am
v2

1 solution

You were already very close ... try this
SQL
Select 'Spend1' as SpendType, Sum(Spend) as Spend where LTM = 1
UNION
Select 'Spend2', Sum(Spend) where L4M = 1

This will give you two rows of results:

SpendType         Spend
-----------------------
Spend1            9999
Spend2            9999
 
Share this answer
 
Comments
serigraphie 11-Jul-14 3:34am    
CHill60,

Okay let me try it. I will let you know if it worked.

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