SQL query help
Discussion
Now you will have to bear with me as I'm not very experienced with SQL at all...
Running MS SQL 6.5
I want to return the sum of two query sets as a single value - it seems using "UNION" doesn't work - here's what i've got
select sum(productval)
from producttable
where blah = blah
union
select sum(productval)
from producttable
where blah = blah2
using this seems to only return the second sum value, rather than a union of the two - can anyone help?
Running MS SQL 6.5
I want to return the sum of two query sets as a single value - it seems using "UNION" doesn't work - here's what i've got
select sum(productval)
from producttable
where blah = blah
union
select sum(productval)
from producttable
where blah = blah2
using this seems to only return the second sum value, rather than a union of the two - can anyone help?
Liszt said:
do you mean:
select sum(productval)
from producttable
where blah = blah
or blah = blah2
Sorry if I wasn't very clear, trying to keep it simple but in doing so I've confused the issue.
This is a simplified query but is the general gist of it:
select sum(productval)
from producttable
where productnumber = 17
union
select sum(productval)
from producttable
where productnumber != 17 and dateexpires > getdate()
Using the "or" expression you've suggested just seems to sit there and not return a result.
_dobbo_ said:
select sum(productval)
from producttable
where productnumber = 17
union
select sum(productval)
from producttable
where productnumber != 17 and dateexpires > getdate()
Using the "or" expression you've suggested just seems to sit there and not return a result.
How's about:
select sum (productval) from (select productval from producttable where (productnumber=17) or (productnumber!=17 and dateexpires>getdate()))
or the more succint version that Liszt suggested:
select sum (productval) from producttable where (productnumber=17) or (productnumber!=17 and dateexpires>getdate())
>> Edited by pdV6 on Friday 7th October 15:02
jimothy said:
What do you mean by get the results as 1 value? The SQL you describe implies you want a results set containing 2 values, one from each sum...
We need the result of the two queries combined and returned as a single value - we could just return the two and combine them in Cold fusion but we want it done in SQL
I know the union clause combines two result sets, hence wasn't likely to work!
PDV6, your second suggestion, using Liszt's syntax seems to have helped us to get there:
select sum (productval) from producttable where (productnumber=17) or (productnumber!=17 and dateexpires>getdate())
by using this, a bit of extra encapsulation with () in a couple of places has got this working!
Thanks for all the help!
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff



I think, _dobbo_, that you've misunderstood the exact meaning of the UNION clause.