1

I'm searching how to do this and can't find it.

Here's my query:

SELECT
    S_Product.Name AS "Name"
    SUM(S_Item.Quantity) AS "QtySold"
FROM s_product
INNER JOIN S_Item ON S_Product.ID = S_Item.Product_ID
WHERE "QtySold" > 500 
--(SUM(S_Item.Quantity)) cannot be used in a WHERE statement since it's a group function
GROUP BY S_Product.Name

Though this doesn't work I think you get my idea, how could I use the `"QtySold" value in the WHERE statement?

2 Answers 2

2

The answer is to put any testing of aggregations in to a HAVING clause:

SELECT
    S_Product.Name AS "Name"
    SUM(S_Item.Quantity) AS "QtySold"
FROM s_product
INNER JOIN S_Item ON S_Product.ID = S_Item.Product_ID
GROUP BY S_Product.Name
HAVING SUM(S_Item.Quantity) > 500 ;
2
  • Do you take a performance hit by putting an analytic function in the HAVING clause, or can the optimizer sort that out nicely?
    – McGlothlin
    Commented Jun 16, 2017 at 14:44
  • The HAVING clause is executed after the GROUP BY. So basically SQL Server will run the GROUP BY over the entire data set, return the results, then filter these results based on what is in your having clause before outputting them. Pretty much all the work will be done in your GROUP BY as it will be aggregating over a large data set. Usually only a small percentage of work will be done in the HAVING.
    – blobbles
    Commented Jun 18, 2017 at 21:24
0

Depending on what your SQL server can do, there are other ways to use a query result in a condition of another query, such as.

SELECT * FROM Orders WHERE CustomerID IN
    ( SELECT CustomerID FROM Customers WHERE ExpressDelivery = 1 )
;
SELECT SomeSubQuery.column1
FROM
    (
    SELECT SUM(Quantity) AS column1, Name AS Name
    FROM s_product GROUP BY Name
    ) AS SomeSubQuery  /* needs a name this time */
;
WITH SomeQuery (Quantities, Name) AS
( SELECT SUM(Quantity) AS Quantities, Name 
  FROM s_product GROUP BY Name )
SELECT Quantities, Name FROM SomeQuery WHERE Quantities > 500
;

The last 2, especially, may be particular to Microsoft SQL Server? (Update) Or may work generally.

I wrote a poem about my first example:

Expedite supply
When prime customers desire
Seasonal produce.

I want a server that can execute that. (but, would it want me?)

6
  • 1
    All 3 of your queries are standard SQL. Commented Jun 16, 2017 at 7:03
  • Okay, but en.wikipedia.org/wiki/Hierarchical_and_recursive_queries_in_SQL says MySQL isn't quite up with "Common Table Expressions" - the one that starts with "WITH SomeQuery (...) AS (...) ... Commented Jun 19, 2017 at 9:27
  • 1
    What does MySQL (or SQL Server) have to do with the question? Commented Jun 19, 2017 at 10:27
  • Apparently the CTE isn't available in MySQL (before version 8 which isn't released yet) - if that information is accurate. My experience is almost all with Microsoft SQL Server, and I know that some features of Microsoft's "Transact-SQL" are exclusive but I don't have a good sense of which. I'm not identifying what Bobby uses; the comment style is not a good clue since apparently that is standard since "SQL 2003"; CTE apparently is a standard provision since "SQL 1999" but not in all database products. But nearly all. So... if the server says "Incorrect syntax" then it may be not my fault? Commented Jun 20, 2017 at 11:34
  • 1
    I can understand why you might think CTEs were specific to SQL Server but what is so SQL Server-specific about derived tables (your second query)? And what is the first query about? What does it convey that the second query doesn't?
    – Andriy M
    Commented Jun 20, 2017 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.