And here's one with both a WHERE clause and a HAVING clause:
"Which agents booked more than $3,000 worth of business in December 2012?"
SELECT Agents.AgtFirstName, Agents.AgtLastName,
SUM(Engagements.ContractPrice) AS TotalBooked
FROM Agents
INNER JOIN Engagements
ON Agents.AgentID = Engagements.AgentID
WHERE Engagements.StartDate
BETWEEN '2012-12-01' AND '2012-12-31'
GROUP BY Agents.AgtFirstName, Agents.AgtLastName
HAVING SUM(Engagements.ContractPrice) > 3000;
Note that the query first uses a WHERE to select the contracts of interest, then uses HAVING to find the ones that sum to more than 3000.
John Viescas, Author
Effective SQL
SQL Queries for Mere Mortals
Microsoft Access 2010 Inside Out
Microsoft Access 2007 Inside Out
Microsoft Access 2003 Inside Out
Building Microsoft Access Applications
(Paris, France)
On Mar 23, 2017, at 8:31 PM, John Viescas JohnV@msn.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:
Bill-
You need to read SQL Queries for Mere Mortals! 😃 Use a HAVING clause to filter aggregates.
From Chapter 14:
"List for each customer and order date the customer's full name and the total cost of
items ordered that is greater than $1,000."
SELECT Customers.CustFirstName || ' ' ||
Customers.CustLastName AS CustFullName,
Orders.OrderDate,
SUM(Order_Details.QuotedPrice *
Order_Details.QuantityOrdered) AS TotalCost
FROM (Customers
INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID)
INNER JOIN Order_Details
ON Orders.OrderNumber =
Order_Details.OrderNumber
GROUP BY Customers.CustFirstName,
Customers.CustLastName, Orders.OrderDate
HAVING SUM(Order_Details.QuotedPrice *
Order_Details.QuantityOrdered) > 1000;
Note that the HAVING clause further filters the result based on an aggregate. (ANSI SQL Syntax)
John Viescas, Author
Effective SQL
SQL Queries for Mere Mortals
Microsoft Access 2010 Inside Out
Microsoft Access 2007 Inside Out
Microsoft Access 2003 Inside Out
Building Microsoft Access Applications
(Paris, France)
On Mar 23, 2017, at 8:22 PM, 'Bill Mosca' wrmosca@comcast.net [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:
__._,_.___
Posted by: John Viescas <johnv@msn.com>
Reply via web post | • | Reply to sender | • | Reply to group | • | Start a New Topic | • | Messages in this topic (15) |
Have you tried the highest rated email app?
With 4.5 stars in iTunes, the Yahoo Mail app is the highest rated email app on the market. What are you waiting for? Now you can access all your inboxes (Gmail, Outlook, AOL and more) in one place. Never delete an email again with 1000GB of free cloud storage.
.
__,_._,___
Tidak ada komentar:
Posting Komentar