|
Home > Archive > Microsoft SQL Server forum > June 2005 > Union Query
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| jack-b@humlog.com 2005-06-22, 8:24 pm |
| hi,
Can you union 2 queries with an IF statement between them?
e.g.
select a, b
from mtTable
where a = c
union
if ab = x
begin
select a, b
from mtTable
where a = c
end
Cheers,
Jack
| |
| David Portas 2005-06-23, 3:23 am |
| Not exactly. But you could put the extra condition in a WHERE clause. I'm
not clear what AB is in your example but any expression that can go after
your IF statement will be valid in the WHERE clause.
SELECT a, b
FROM mtTable
WHERE a = c
UNION
SELECT a, b
FROM mtTable
WHERE a = c
AND ab = x
--
David Portas
SQL Server MVP
--
| |
| jack-b@humlog.com 2005-06-23, 8:23 pm |
| Thanks - worked perfectly!
You don't know how I can keep the order in between queries? (order by)
Do I have to use a sub-query for each select statement?
| |
| David Portas 2005-06-24, 8:23 pm |
| SELECT a,b
FROM
(SELECT a, b, 1 AS ord
FROM mtTable
WHERE a = c
UNION
SELECT a, b, 2
FROM mtTable
WHERE a = c
AND ab = x) AS T
ORDER BY ord
--
David Portas
SQL Server MVP
--
| |
| jack-b@humlog.com 2005-06-27, 8:23 pm |
| Thanks David.
|
|
|
|
|