ORDER BY with 2 statements

Results 1 to 3 of 3

Thread: ORDER BY with 2 statements

  1. #1
    Reggie Guest

    Default ORDER BY with 2 statements

    Is there an easy way to sort the record set output of 2 separate SQL statements? For example if SQL_Query1 returns 3 things like "apple", "carrot", and "elephant" then SQL_Query2 returns "banana", "donut", and "fridge". Is there any way to get it to print out "apple", "banana", "carrot", "donut", "elephant", "fridge"?<BR><BR>I hate writing sorting algorithms, so I was hoping there was something in SQL that could take care of this for me.

  2. #2
    Join Date
    Dec 1969

    Default RE: ORDER BY with 2 statements

    If you&#039re using SQL server you can create a VIEW that is a UNION of the two queries. Then issue a SELECT statement FROM the VIEW specifing an ORDER BY Clause EG:<BR><BR>/* CREATE THE UNION VIEW */<BR>CREATE VIEW TestUnion<BR>AS<BR>SELECT ProdName FROM Table1 <BR>UNION <BR>SELECT ProdName FROM Table2<BR>/* END CREATE THE UNION VIEW */<BR><BR>***ASP Code<BR><BR>&#060;%<BR>sSql = "SELECT * FROM TestUnion ORDER BY ProdName"<BR>Set rs = MyConn.Execute(sSQl)<BR>%&#062;<BR><BR><BR><BR><BR >

  3. #3
    Oh yeah, thanks! Guest

    Default RE: ORDER BY with 2 statements

    Now I seem to remember something about UNION and VIEW from the online tutorial I found when I was learning about SQL. At the time I was only concerned with the basics and didn&#039t understand why the heck I&#039d ever need anything besides the basic 4 commands.<BR><BR>Thanks much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts