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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UNION puzzle

Status
Not open for further replies.

esdee

Programmer
Apr 12, 2001
143
US
i have
select a, b, c from t1 -- q1
union
select w, f, v from t2 --q2

in the result set q1 come below result set of q2
(w, f, v
q, b, c)

how come ?
 
The "published" difference between UNION and UNION ALL is that the later will include duplicate data if the same row of data exists in both queries.
You cannot include ORDER BY clauses in the union sets.
-Karl
 
I beg to differ as far as using ORDER BY...I believe you can...I use it to generate lists where the first item is a blank...

ex:
Code:
SELECT	C.CLIENTGROUPID AS GROUPID,
	C.CLIENTGROUP AS 'GROUP'
FROM	TBLCLIENTGROUP C
UNION
SELECT	0 AS GROUPID,
	'<< Select >>' AS 'GROUP'
ORDER
BY	'GROUP' asc
 
I agree with chekai. I do order by's in some of my stored procs that have unions. You can only do an order by after the last union, not after each one.

Tim
 
I love this forum. You learn new things and you relearn old things the right way. :)
-Karl
 
ahem..well, yeah
i know about union all and order by with unions, but
can anybody answer MY question? :)
of what i've read by now it seems that union automatically orders the rows, is that correct ?
 
Why not tag a static field onto the end of each q1 and q2 and order by that.
Code:
SELECT    C.CLIENTGROUPID AS GROUPID,
    C.CLIENTGROUP AS 'GROUP','0' as gOrder
FROM    TBLCLIENTGROUP C
UNION
SELECT    0 AS GROUPID,
    '<< Select >>' AS 'GROUP', '1' as gOrder
ORDER
BY    gOrder,'GROUP' asc
 
thats an interesting idea, jkelly, thanx
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top