Hello Tek-Tips,
I have struggled far too long on my attempt to summarize data in my Access Database. I wish to code a SQL statement that will total all of my customer's revenues and group them by CustomerName. Here is the structure of my database:
Events Table: (one Event to many EventDetail items)
MeetingRoomPrice + Grat7 + Grat11 + SalesTax
EventDetail Table:
Quantity * Price
Output:
Customer1 12,100.25
Customer2 8,412.35
Here is a sample of a Union Select statement which returns the correct results, however on two seperate lines per customer.
SELECT Customers.CustomerName, IIf(ISNULL(Sum(EventDetail.Quantity*EventDetail.Price)),0,Sum((EventDetail.Quantity*EventDetail.Price))) AS CustomerTotal
FROM (Customers INNER JOIN Events ON Events.CustomerID=Customers.CustomerID) LEFT JOIN EventDetail ON EventDetail.EventID=Events.EventID
WHERE Events.EventDate Between #1/1/2001# And #12/31/2004# And Events.Canceled=0
GROUP BY Customers.CustomerName
ORDER BY Customers.CustomerName
UNION SELECT Customers.CustomerName, sum(Events.MeetingRoomPrice+Events.Grat11+Events.Grat7+Events.SalesTax) AS CustomerTotal
FROM Events INNER JOIN Customers ON Customers.CustomerID=Events.CustomerID
WHERE Events.EventDate Between #1/1/2001# And #12/31/2004# And Events.Canceled=0
GROUP BY Customers.CustomerName
ORDER BY Customers.CustomerName;
I have tried many variations of joins, and the sum of the Events table is duplicated by the amount of entries in the EventDetail table.
Any help is greatly appreciated.
Tom (maxflitom)
I have struggled far too long on my attempt to summarize data in my Access Database. I wish to code a SQL statement that will total all of my customer's revenues and group them by CustomerName. Here is the structure of my database:
Events Table: (one Event to many EventDetail items)
MeetingRoomPrice + Grat7 + Grat11 + SalesTax
EventDetail Table:
Quantity * Price
Output:
Customer1 12,100.25
Customer2 8,412.35
Here is a sample of a Union Select statement which returns the correct results, however on two seperate lines per customer.
SELECT Customers.CustomerName, IIf(ISNULL(Sum(EventDetail.Quantity*EventDetail.Price)),0,Sum((EventDetail.Quantity*EventDetail.Price))) AS CustomerTotal
FROM (Customers INNER JOIN Events ON Events.CustomerID=Customers.CustomerID) LEFT JOIN EventDetail ON EventDetail.EventID=Events.EventID
WHERE Events.EventDate Between #1/1/2001# And #12/31/2004# And Events.Canceled=0
GROUP BY Customers.CustomerName
ORDER BY Customers.CustomerName
UNION SELECT Customers.CustomerName, sum(Events.MeetingRoomPrice+Events.Grat11+Events.Grat7+Events.SalesTax) AS CustomerTotal
FROM Events INNER JOIN Customers ON Customers.CustomerID=Events.CustomerID
WHERE Events.EventDate Between #1/1/2001# And #12/31/2004# And Events.Canceled=0
GROUP BY Customers.CustomerName
ORDER BY Customers.CustomerName;
I have tried many variations of joins, and the sum of the Events table is duplicated by the amount of entries in the EventDetail table.
Any help is greatly appreciated.
Tom (maxflitom)