0

A common (i assume?) type of query:

I have two tables ('products', 'productsales'). Each record in 'productsales' represents a sale (so it has 'date', 'quantity', 'product_id'). How do i efficiently make a query like the following:

Retrieve the product names of all products that were sold more than X times between date Y and date Z.

(X is the quantity sold not the number of transactions)

1
  • More clarity on what you mean by "X times" needed, number of transactions or quantity sold? Feb 1, 2009 at 19:00

4 Answers 4

1
 SELECT p.[name]
 FROM products p
 WHERE p.product_id in (SELECT s.product_id
     FROM productsales s
     WHERE s.[date] between @dateStart and @dateEnd
     GROUP BY s.product_id
     HAVING Sum(s.quantity) > @X )
3
  • I presume you'll have to use SUM instead of COUNT, since the Sales table has a qty field ... :) Feb 1, 2009 at 18:56
  • Fredrik, The question asks for the number of times sold, not the number of items. Feb 1, 2009 at 18:59
  • The question is perhaps vague on this it asks for times but includes the detail that a qty field is available. I've ask Mota for more clarity Feb 1, 2009 at 19:02
1

The above query is not entirely correct ...

SELECT Name FROM Products
WHERE ProductId IN
( SELECT ProductId 
  FROM ProductSales
  WHERE ProductSales.Date BETWEEN  Y AND Z 
  GROUP BY ProductId
  HAVING SUM(ProductSales.Qty) > x
)
5
  • Note most answers are view by votes so 'above' is arbitary. Also it depends on how you interpret the questions use of the phrase "sold more than X times". You could be right it might refer to qty sold but it could also refer to the number of specific transactions. Feb 1, 2009 at 18:59
  • Note that the "above query" is going to change in this thread over time, making your "above query" reference incorrect... Feb 1, 2009 at 19:01
  • I was referring to the query written by Mehrdad Afshari; which couldn't even be interpreted since it has incorrect syntax. Feb 1, 2009 at 19:07
  • I would seem your original interpretation of the question was correct +1. Feb 1, 2009 at 19:21
  • Fredrik, I have tried the query on SQL Server 2008 and there is no "incorrect syntax" in it. Feb 1, 2009 at 19:37
0

Actually, I think you should learn the group by statement

The simplest statement would be:

SELECT ps.product_id, COUNT(*) 
           FROM product_sales ps 
           WHERE ps.product_id = id AND ps.Date BETWEEN @Y AND @Z
           GROUP_BY product_id;
0
SELECT p.[name]
FROM
products p
INNER JOIN
productsales ps
ON 
p.id = ps.product_id
WHERE
ps.date BETWEEN @Y AND @Z
GROUP BY
p.[name] 
--,p.id if it is likely that you have two products with the same name
HAVING
SUM(ps.quantity) > @X

Tested successfully with MS AdventureWorks DB, using the following-

DECLARE @Y DATETIME, @Z DATETIME, @X INT    

SET @Y = '2004-06-09'
SET @Z = '2004-08-01'
SET @X = 100

    SELECT p.[Name]
    FROM
    Production.Product p
    INNER JOIN
    Sales.SalesOrderDetail ps
    ON 
    p.ProductID = ps.ProductID
    WHERE
    ps.ModifiedDate BETWEEN @Y AND @Z
    GROUP BY p.Name
    HAVING
    SUM(ps.OrderQty) > @X
2
  • True, I have made that assumption
    – Russ Cam
    Feb 1, 2009 at 19:23
  • I have updated by including commented out p.id, in the event that two products have the same name. I don't think that I've ever come across this though
    – Russ Cam
    Feb 1, 2009 at 19:33

Your Answer

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