House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home /  Groups /  ColdFusion Talk (CF-Talk)

union and sorting

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Hi all,
George Abraham
06/09/04 11:58 A
Do this
Samuel Neff
06/09/04 12:24 P
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
George Abraham
06/09/2004 11:58 AM

Hi all, SQL Server question. I have a query like so: ******* SELECT * FROM Table1 WHERE Table1Item  = 'something' UNION SELECT * FROM Table2 WHERE Table2Item = 'something' ******** SQL Server does not allow me to put in individual 'ORDER BY' statements on the individual selects. ******* SELECT * FROM Table1 WHERE Table1Item  = 'something' ORDER By SomeTableColumn UNION SELECT * FROM Table2 WHERE Table2Item = 'something' ORDER By SomeTableColumn ******** Instead I can only put an 'ORDER BY' statement at the end of all the unions. I can't have that. I need to be able to show the results from the first select first, then the results from the second select. Any soultions? Thanks, George

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Barney Boisvert
06/09/2004 12:16 PM

I don't know about SQL Server, but if you put the select and ordr by clause in parentheses, MySQL will let you do what you want: ******* (SELECT * FROM Table1 WHERE Table1Item  = 'something' ORDER By SomeTableColumn ) UNION (SELECT * FROM Table2 WHERE Table2Item = 'something' ORDER By SomeTableColumn ) ******** If that doesn't work, add a constant field to your queries like this, which will let you use the global ORDER BY ******* SELECT 0 AS orderer, * FROM Table1 WHERE Table1Item  = 'something' UNION SELECT 1 AS orderer, * FROM Table2 WHERE Table2Item = 'something' ORDER By orderer, SomeTableColumn ******** Cheers, barneyb ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Samuel Neff
06/09/2004 12:24 PM

Do this SELECT *, 1 as SortOrder FROM Table1 UNION SELECT *, 2 as SortOrder FROM Table2 ORDER BY SortOrder and it will put query 1 records before query 2 records, which is what I think you're after from the text. HTH, Sam Hi all, SQL Server question. I have a query like so: ******* SELECT * FROM Table1 WHERE Table1Item  = 'something' UNION SELECT * FROM Table2 WHERE Table2Item = 'something' ******** SQL Server does not allow me to put in individual 'ORDER BY' statements on the individual selects. ******* SELECT * FROM Table1 WHERE Table1Item  = 'something' ORDER By SomeTableColumn UNION SELECT * FROM Table2 WHERE Table2Item = 'something' ORDER By SomeTableColumn ******** Instead I can only put an 'ORDER BY' statement at the end of all the unions. I can't have that. I need to be able to show the results from the first select first, then the results from the second select. Any soultions? Thanks, George

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
George Abraham
06/09/2004 12:37 PM

Thanks! I think both Barney's solution and yours are identical. George At 12:17 PM 6/9/2004, Samuel Neff wrote: ----- Excess quoted text cut - see Original Post for more -----


<< Previous Thread Today's Threads Next Thread >>

Search cf-talk

February 08, 2012

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
       1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29