Solved

UNION query problem

Posted on 2007-03-29
3
167 Views
Last Modified: 2010-03-20
hi i have this question on the go:

http://www.experts-exchange.com/Programming/Languages/SQL_Syntax/Q_22474564.html

but i'm stuck, and have spent the last 2 days trying to get this query to work, i'm close but i'm not there.

i need to link 4 tables:

Suppliers
Deliveries (all deliveries made by a supplier)
Complaints (all complaints raised against supplier)
Periods

i want to output a query showing a by month summary of supplier performance

looking like:
Month         Deliveries      OnTime      AvgLeadTime       Complaints
Jan2007          86                 80                  12.1                     2
Feb2007           0                   0                                               0
Mar2007          10                  10                   8.0                     0

now this needs to be generated for all suppliers regardless of whether we had delivieries or not, i.e a LEFT JOIN on tSuppliers
and i need to UNION ALL the tDeliveries and tComplaints
and then do a BETWEEN on the Period table

if that makes any sense! its 8pm here and i'm weary of this!

please help

i'm using:

SELECT l.sAcc, tP.Period, Sum(l.Deliveries) AS SumOfDeliveries, Sum(l.[On Time]) AS [SumOfOn Time], Sum(l.LeadTime) AS SumOfLeadTime, Sum(l.Complaints) AS SumOfComplaints
FROM [SELECT qDel.sAcc, Format([DueDate],"yyyy-mm") AS Period
, Count(qDel.Prod) AS [Deliveries]
, Sum(qDel.OnTime) AS [On Time]
, Avg(qDel.LeadTime) AS [LeadTime]
, 0 as Complaints
FROM qDel
GROUP BY qDel.sAcc, Format([DueDate],"yyyy-mm")
UNION ALL
SELECT tblSC_Report.Account, Format([Date],"yyyy-mm") AS Period, 0 , 0 , 0, Count(tblSC_Report.ComplaintNo)
FROM tblSC_Report
GROUP BY tblSC_Report.Account, Format([Date],"yyyy-mm")
]. AS l, tP
WHERE (((l.Period) Between [tp].[pfrm] And [tp].[pto]))
GROUP BY l.sAcc, tP.Period
ORDER BY l.sAcc, tP.Period;

see the other post, for examples of what worked and didnt.

Thanks G

i'm using Access 2003 and my experience (nor brain power!) is that high
0
Comment
Question by:GrahamLaws
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 250 total points
ID: 18819040
is all you are now missing is month where there where no activity?

if so then you need a calendar table added to your query...

set up a table to contain all periods that are of interest to you ...

e.g.
reportperiod
periodDescription  char(7)  
Year smallint
month smallint
primary key (year,month)


then add it to your query as ...



 
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question