Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
| Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 30th, 2004, 11:22 AM
Registered User
 
Join Date: Jul 2003
Location: Worland, WY, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to eoffshore
Default Pooling fields from two tables into one view

I have two tables that record different date/time events. Think of one table for repairs and another table for installs. Both table have a createDate and createTime as well as several other common fields..and some unique fields.

I want to create a view that combines the two tables so that I can have a date/time ordered timeline that combines the two table. There is no relationship between the records in the repair and the installs table so I suspect the view I want will be a full outer join. But how can I have one column in the view called createDate that is populated with the createdate data from both the repairs and the installs tables?

The Meek Shall Inherit the Gulag
  #2 (permalink)  
Old July 30th, 2004, 11:35 AM
Authorized User
 
Join Date: Apr 2004
Location: Boise, ID, USA.
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Default

A UNION would probably be easier here...

Code:
SELECT *
  FROM (SELECT <<column list>>, createDate, createTime
          FROM table1
        UNION ALL
        SELECT <<column list>>, createDate, createTime
          FROM table2)
 ORDER BY createDate, createTime;
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC
  #3 (permalink)  
Old August 2nd, 2004, 03:30 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Cardiff, , United Kingdom.
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Justin, why have you nested the UNION query in another query? The ORDER BY caluse is a perfectely valid statement in a UNION query, as long as it appears after all the queries have been UNIONed together. Therefore a simpler query would be:
Code:
   SELECT <<column list>>, createDate, createTime
     FROM repairs

UNION ALL

   SELECT <<column list>>, createDate, createTime
     FROM installs

 ORDER BY createDate, createTime


Regards
Owain Williams
  #4 (permalink)  
Old August 2nd, 2004, 03:52 AM
Authorized User
 
Join Date: Apr 2004
Location: Boise, ID, USA.
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In this case, I find the parenthesis to be helpful in reading the query. Without the parenthesis and outer SELECT statement, it might not be obvious to a reader whether the ORDER BY applied to the result of the UNION operation or whether it applied to the last SELECT statement in the UNION.

Under the covers, Oracle will treat both queries identically, so whichever formulation is easier to read ought to be chosen. To my eye, the outer SELECT makes the query more obvious, but that is purely a matter of taste.

Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC
  #5 (permalink)  
Old August 2nd, 2004, 05:42 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Cardiff, , United Kingdom.
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I see your point, I would like to add that I personally find the nested approach adds complexity to the statement, however I can see how others may find it easier to read.

ANSI SQL states that you can not order the individual queries in a UNION query, you can only ORDER the entire UNIONed result set, again I am assuming that the author knows this and using a nested query probably makes this clearer.

Finally, using the nested query approach, is there a possibility that this will create a higher overhead for the server? For example, the server might create the UNIONed query in a temporary table and then query this table to order and return the result set. Could this be possible or is the server cleaver enough to work out that the 2 queries are the same?

Regards
Owain Williams


Similar Threads
Thread Thread Starter Forum Replies Last Post
View of image fields AlexSepahpour SQL Server 2005 0 October 24th, 2007 06:06 PM
Compare two fields from different tables pallone SQL Server 2000 10 March 23rd, 2007 05:14 PM
having some fields of a grid view a drop list dsblack@montgomerycountyt ASP.NET 2.0 Professional 0 June 2nd, 2006 04:18 PM
Blank fields - queries with many tables dvarrin Crystal Reports 0 February 1st, 2005 05:43 AM
fields from tables to update damnnono_86 Access 3 November 5th, 2003 04:07 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.