Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with select queries 1

Status
Not open for further replies.

morfasie

IS-IT--Management
Mar 28, 2004
85
ZA
I have a query like this:
SELECT sum( Premium ) AS soweto
FROM production
WHERE `Agent Code`
LIKE '%GB01%'
UNION
SELECT sum( `Premium` ) AS `NW meretele`
FROM production
WHERE `Agent Code`
LIKE '%WW02%'

however it returns the results in one column, I want to return in two columns with the one column being 'Soweto' and the next column being 'NW meretele'

thanks
 
Thats the purpose of unions my friends...don't know how it didn't give you an error in the first place!

But if you want the above to work you should do something like:

SELECT sum(P.Premium)AS soweto, sum(T.Premium)AS `NW meretele`
FROM production P, production T
WHERE `P.Agent Code` LIKE '%GB01%' and T.Agent Code`
LIKE '%WW02%'

or something similar.....

You could check out joins!


Nick
 
Hi thanks for the reply, how would I use it with a left join? do you knowe of any usefull sites about joins in sql??
 
How come you want to use a left join?

(You don't know the difference between a union and a join, but you know you want a left join...)
 
Try this
[blue][tt]
SELECT sum( Premium ) AS soweto, 0 As `NW meretele`
FROM production
WHERE `Agent Code`
LIKE '%GB01%'
UNION
SELECT 0 , sum(Premium)
FROM production
WHERE `Agent Code`
LIKE '%WW02%'
[/tt][/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top