Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 18th, 2012, 08:22 AM
paulpitchford paulpitchford is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 6 paulpitchford User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 28 m 12 sec
Reputation Power: 0
1 union query vs 3 other queries

Hi, I have a union query that pulls in all our order numbers from sales and orders. When we place an order with a supplier the reference we us is the order number that the customer placed with us.

We then have 3 other queries which detail deliveries, warehouse issues and factory returns:

They are all like this:
Order Number
Qty Issued

The union table just lists every single order number in the system.

What I would like to do is display all three queries in one query. I presume we need to use the union query in some way but I can't make it show all the records from all of the queries.

This is the SQL I have so far:

Code:
SELECT tst_Paul_UnionJoinOrdersDels.OrderNo, tst_Paul_FilmIssued.[SumOfQty Issued], tst_Paul_FilmDeliveries.SumOfDeldQtyKilos, tst_Paul_FilmReturns.[SumOfQty Issued]
FROM ((tst_Paul_UnionJoinOrdersDels LEFT JOIN tst_Paul_FilmIssued ON tst_Paul_UnionJoinOrdersDels.OrderNo = tst_Paul_FilmIssued.[Order No From]) LEFT JOIN tst_Paul_FilmDeliveries ON tst_Paul_UnionJoinOrdersDels.OrderNo = tst_Paul_FilmDeliveries.OrderNo) LEFT JOIN tst_Paul_FilmReturns ON tst_Paul_UnionJoinOrdersDels.OrderNo = tst_Paul_FilmReturns.[Order No From]
WHERE (((tst_Paul_FilmIssued.[SumOfQty Issued])>=0)) OR (((tst_Paul_FilmDeliveries.SumOfDeldQtyKilos)>=0)) OR (((tst_Paul_FilmReturns.[SumOfQty Issued])>=0));


This picks all the deliveries and returns up fine but for some reason it's not picking up all of the issues.

Could someone please point me in the right direction as I'm just going in circles at the moment and this is about as close as I can get it. Please feel free to ask for more information if this is not clear enough.

Thank you,

Paul.

Reply With Quote
  #2  
Old June 18th, 2012, 08:56 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,349 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 7 h 17 m 46 sec
Reputation Power: 390
As you have restrictions on the right hand table of a left join in the where clause, the query will behave as an inner join. Move those restrictions to an apropriate on clause instead.

Reply With Quote
  #3  
Old June 18th, 2012, 09:03 AM
paulpitchford paulpitchford is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 6 paulpitchford User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 28 m 12 sec
Reputation Power: 0
Thanks for the reply swampBoogie. I understand this:

Quote:
Originally Posted by swampBoogie
As you have restrictions on the right hand table of a left join in the where clause, the query will behave as an inner join.


But unfortunately I don't understand this:

Quote:
Originally Posted by swampBoogie
Move those restrictions to an apropriate on clause instead.


Could you elaborate on this?

Thank you.

Reply With Quote
  #4  
Old June 18th, 2012, 11:09 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,369 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 14 m 57 sec
Reputation Power: 4140
Quote:
Originally Posted by paulpitchford
Could you elaborate on this?
it's a true pity that microsoft access does such a shïtty job of formatting sql, not only running everything into one long continuous line, but tossing (all those (useless (ridiculous))) parentheses all over da place

let's first just simply reformat your query --
Code:
SELECT tst_Paul_UnionJoinOrdersDels.OrderNo
     , tst_Paul_FilmIssued.[SumOfQty Issued]
     , tst_Paul_FilmDeliveries.SumOfDeldQtyKilos
     , tst_Paul_FilmReturns.[SumOfQty Issued]
  FROM ((
       tst_Paul_UnionJoinOrdersDels 
LEFT 
  JOIN tst_Paul_FilmIssued 
    ON tst_Paul_UnionJoinOrdersDels.OrderNo = tst_Paul_FilmIssued.[Order No From]
       ) 
LEFT 
  JOIN tst_Paul_FilmDeliveries 
    ON tst_Paul_UnionJoinOrdersDels.OrderNo = tst_Paul_FilmDeliveries.OrderNo
       ) 
LEFT 
  JOIN tst_Paul_FilmReturns 
    ON tst_Paul_UnionJoinOrdersDels.OrderNo = tst_Paul_FilmReturns.[Order No From]
 WHERE (((tst_Paul_FilmIssued.[SumOfQty Issued])>=0)) 
    OR (((tst_Paul_FilmDeliveries.SumOfDeldQtyKilos)>=0)) 
    OR (((tst_Paul_FilmReturns.[SumOfQty Issued])>=0));

now it's easier to see the conditions after they've been moved to the ON clauses --
Code:
SELECT tst_Paul_UnionJoinOrdersDels.OrderNo
     , tst_Paul_FilmIssued.[SumOfQty Issued]
     , tst_Paul_FilmDeliveries.SumOfDeldQtyKilos
     , tst_Paul_FilmReturns.[SumOfQty Issued]
  FROM ((
       tst_Paul_UnionJoinOrdersDels 
LEFT 
  JOIN tst_Paul_FilmIssued 
    ON tst_Paul_UnionJoinOrdersDels.OrderNo = tst_Paul_FilmIssued.[Order No From]
   AND (((tst_Paul_FilmIssued.[SumOfQty Issued])>=0)) 
       ) 
LEFT 
  JOIN tst_Paul_FilmDeliveries 
    ON tst_Paul_UnionJoinOrdersDels.OrderNo = tst_Paul_FilmDeliveries.OrderNo
   AND (((tst_Paul_FilmDeliveries.SumOfDeldQtyKilos)>=0)) 
       ) 
LEFT 
  JOIN tst_Paul_FilmReturns 
    ON tst_Paul_UnionJoinOrdersDels.OrderNo = tst_Paul_FilmReturns.[Order No From]
   AND (((tst_Paul_FilmReturns.[SumOfQty Issued])>=0))

sadly, access is gonna bitch about the AND in the ON clauses ("join expression not supported" or similar), so here we are forced to add more parentheses

as a last step, i like to use table aliases ( thus reducing the total number of characters of code, making the forest easier to see amongst all the trees), as well as removing unnecessary parentheses and switching the join columns in the ON clause
Code:
SELECT o.OrderNo
     , i.[SumOfQty Issued]
     , d.SumOfDeldQtyKilos
     , r.[SumOfQty Issued]
  FROM ((
       tst_Paul_UnionJoinOrdersDels AS o
LEFT 
  JOIN tst_Paul_FilmIssued AS i
    ON ( i.[Order No From] = o.OrderNo 
     AND i.[SumOfQty Issued] >= 0 ) 
       ) 
LEFT 
  JOIN tst_Paul_FilmDeliveries AS d
    ON ( d.OrderNo = o.OrderNo
     AND d.SumOfDeldQtyKilos >=0 ) 
       ) 
LEFT 
  JOIN tst_Paul_FilmReturns AS r
    ON ( r.[Order No From] = o.OrderNo 
     AND r.[SumOfQty Issued] >=0 )

helps?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #5  
Old June 20th, 2012, 04:40 AM
paulpitchford paulpitchford is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 6 paulpitchford User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 28 m 12 sec
Reputation Power: 0
Hi,

Thanks for the detailed response. As it happens that didn't resolve my problem but I learnt enough from your post to figure it myself. Looking back maybe I didn't explain the question well enough.

Again thank you for such a concise response though. It really helped me.

Paul.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > 1 union query vs 3 other queries

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap