If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Union All

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-17-07, 12:28
Breako Breako is offline
Registered User
 
Join Date: Jan 2006
Posts: 119
Union All

Hi,
I was just wondering is any query that uses UNION ALL possible to achieve just using a join? i.e. is UNION ALL just handy syntax?
Thanks
Reply With Quote
  #2 (permalink)  
Old 08-17-07, 12:36
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Code:
SELECT email, Firstname, Lastname
FROM customers
UNION ALL
SELECT email, Firstname, Lastname
FROM employees
There you go - a list of all customers and employees details.
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 08-17-07, 12:59
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
yeah, but george, the question was, could you do that with a JOIN
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 08-17-07, 13:01
Breako Breako is offline
Registered User
 
Join Date: Jan 2006
Posts: 119
Quote:
Originally Posted by r937
yeah, but george, the question was, could you do that with a JOIN
Yes that was the quesion. thanks
Reply With Quote
  #5 (permalink)  
Old 08-17-07, 13:30
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
I think you can cover much - not sure about all.
Code:
SELECT ...
FROM   employee FULL OUTER JOIN customer ON ( 1 = 2 )
Then a few COALESCE function calls or CASE expressions in the SELECT-list to get the value from the correct table.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #6 (permalink)  
Old 08-17-07, 13:48
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
breako, by any chance are you using mysql version 3.23?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 08-17-07, 15:02
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
I posted an example of one which is faaaar easier using a union.
Proof by exception
__________________
George
Twitter | Blog
Reply With Quote
  #8 (permalink)  
Old 08-17-07, 15:04
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by georgev
Proof by exception
nice try, but quite a clever backpedal

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 08-18-07, 01:00
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
I can't think of any way to do this using just JOIN operations:
Code:
SELECT d0 + d1 + d2 + d3
   FROM       (SELECT 0 AS d0 UNION ALL SELECT  1 UNION ALL SELECT  2) AS z0
   CROSS JOIN (SELECT 0 AS d1 UNION ALL SELECT  3 UNION ALL SELECT  6) AS z1
   CROSS JOIN (SELECT 0 AS d2 UNION ALL SELECT  9 UNION ALL SELECT 18) AS z2
   CROSS JOIN (SELECT 0 AS d3 UNION ALL SELECT 27 UNION ALL SELECT 54) AS z3
   ORDER BY d0 + d1 + d2 + d3
-PatP
Reply With Quote
  #10 (permalink)  
Old 08-19-07, 17:41
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Quote:
Originally Posted by r937
nice try, but quite a clever backpedal

No, seriously!
I can't think of a better way of doing my example. I'm more than happy to be proved wrong though
__________________
George
Twitter | Blog
Reply With Quote
  #11 (permalink)  
Old 08-20-07, 08:33
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Quote:
Originally Posted by Pat Phelan
I can't think of any way to do this using just JOIN operations:
Code:
SELECT d0 + d1 + d2 + d3
   FROM       (SELECT 0 AS d0 UNION ALL SELECT  1 UNION ALL SELECT  2) AS z0
   CROSS JOIN (SELECT 0 AS d1 UNION ALL SELECT  3 UNION ALL SELECT  6) AS z1
   CROSS JOIN (SELECT 0 AS d2 UNION ALL SELECT  9 UNION ALL SELECT 18) AS z2
   CROSS JOIN (SELECT 0 AS d3 UNION ALL SELECT 27 UNION ALL SELECT 54) AS z3
   ORDER BY d0 + d1 + d2 + d3
-PatP
Aside from the point that this is no SQL (no FROM-clauses in the subselects, each of those subselects can be replaced with:
Code:
SELECT COALESCE(t1.d1, t2.d1) AS d1
FROM ( SELECT 0 AS d1 FROM sysibm.sysdummy1 ) AS t1
     FULL OUTER JOIN
     ( SELECT 1 AS d1 FROM sysibm.sysdummy1 ) AS t2 ON 1 = 2


D1
-----------
          1
          0
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #12 (permalink)  
Old 08-25-07, 18:34
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by stolze
SELECT COALESCE(t1.d1, t2.d1) AS d1
FROM ( SELECT 0 AS d1 FROM sysibm.sysdummy1 ) AS t1
FULL OUTER JOIN
( SELECT 1 AS d1 FROM sysibm.sysdummy1 ) AS t2 ON 1 = 2
Just being pedantic now (I know), but this isn't std SQL neither since non-DB2 systems usually don't have a 1x1 table called sysibm.sysdummy1.

The following is std SQL (I believe):
Code:
SELECT COALESCE(t1.d1, t2.d1) AS d1
FROM ( VALUES (0) AS d1 ) AS t1
     FULL OUTER JOIN
     ( VALUES (1) AS d1 ) AS t2
     ON 1 = 2
So, in summary, in answer to the original question, any UNION ALL could indeed be replaced by a (rather complex) FULL OUTER JOIN combined with COALESCEs, but I wouldn't call UNION ALL "just handy syntax": it's conceptually completely different from table joins.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #13 (permalink)  
Old 08-25-07, 20:35
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Quote:
Originally Posted by stolze
Aside from the point that this is no SQL (no FROM-clauses in the subselects, each of those subselects can be replaced with:
Code:
SELECT COALESCE(t1.d1, t2.d1) AS d1
FROM ( SELECT 0 AS d1 FROM sysibm.sysdummy1 ) AS t1
     FULL OUTER JOIN
     ( SELECT 1 AS d1 FROM sysibm.sysdummy1 ) AS t2 ON 1 = 2


D1
-----------
          1
          0
Coach me a bit here, I'm really missing something. Your code does in fact produce the result set that you posted, but how is that related to the code that I posted? I believe that my code was perfectly valid SQL, but even if the FROM clause was required by a specific SQL interpreter it could be added without harm, and I still don't know how to produce the same kind of results without a UNION.

-PatP
Reply With Quote
  #14 (permalink)  
Old 08-25-07, 21:31
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by Pat Phelan
I still don't know how to produce the same kind of results without a UNION.
Here it is:
Code:
SELECT d0 + d1 + d2 + d3
   FROM       (SELECT COALESCE(x1,x2,x3) AS d0
                FROM (SELECT 0 AS x1) FULL OUTER JOIN
                     (SELECT 1 AS x2) ON 1 = 2 FULL OUTER JOIN
                     (SELECT 2 AS x3) ON 1 = 2 ) AS z0
   CROSS JOIN (SELECT COALESCE(x1,x2,x3) AS d1
                FROM (SELECT 0 AS x1) FULL OUTER JOIN
                     (SELECT 3 AS x2) ON 1 = 2 FULL OUTER JOIN
                     (SELECT 6 AS x3) ON 1 = 2 ) AS z1
   CROSS JOIN (SELECT COALESCE(x1,x2,x3) AS d2
                FROM (SELECT 0 AS x1) FULL OUTER JOIN
                     (SELECT 9 AS x2) ON 1 = 2 FULL OUTER JOIN
                     (SELECT 18 AS x3) ON 1 = 2 ) AS z2
   CROSS JOIN (SELECT COALESCE(x1,x2,x3) AS d3
                FROM (SELECT 0 AS x1) FULL OUTER JOIN
                     (SELECT 27 AS x2) ON 1 = 2 FULL OUTER JOIN
                     (SELECT 54 AS x3) ON 1 = 2 ) AS z3
   ORDER BY d0 + d1 + d2 + d3
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #15 (permalink)  
Old 08-26-07, 11:59
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Quote:
Originally Posted by Peter.Vanroose
Just being pedantic now (I know), but this isn't std SQL neither since non-DB2 systems usually don't have a 1x1 table called sysibm.sysdummy1.
No, you're not pedantic - just right.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On