Groups | Blog | Home
all groups > sql server (alternate) > january 2006 >

sql server (alternate) : SQL Query


pipe.jack NO[at]SPAM gmail.com
1/12/2006 1:51:10 PM
Hello guys,

I'm not an sql expert but I need to pull out some data from SQL
database. I hope you can help me here.
I need to find out how many time a product was ordered. Some product
have the same ProductName but different Option and for me that's a
different product.

Here is the database:

- TABLE OrderDetails -
OrderDetailID
OrderID
ProductName
Options

- TABLE Orders -
OrderID
CustomerID
KeyAccess

The output should look something like this:
NoOrders ProductName ProductOptions KeyAccess
12 Blue car $500 C
7 Blue car $700
C
3 Yellow car $1000 C
1 Yellow car $5000 C
..........................
C = customers


Thanks for any help
Jack
Jens
1/12/2006 2:10:23 PM
Hi Jack,

this is sure untested but should go something like this:

Select COUNT(*), producName,ProductOptions, KeyAccess
[quoted text, click to view]
Inner join OrderDetails
ON Order.OrderId = OrderDetails.orderId
Group by producName,ProductOptions, KeyAccess

HTH, Jens Suessmeyer.
pipe.jack NO[at]SPAM gmail.com
1/12/2006 4:01:25 PM
Excellent, thanks!

Another one for you:
How many orders included a product. If the quantity of the product in a
single order is more then 1 then it should still count as 1 since I'm
interested in the number of orders.

Thanks again for all and any help.
Jens
1/12/2006 4:23:02 PM
Hi again,

this should be something like:

SELECT COUNT(*) From Orders O
WHERE EXISTS
(
SELECT * FROM Orderdetails OD
WHERE O.OrderID = OD.OrderId
)

HTH, Jens Suessmeyer.
Hugo Kornelis
1/12/2006 11:07:40 PM
[quoted text, click to view]

Hi Jack,

Try if this works:

SELECT COUNT(*) AS NoOrders,
od.ProductName, od.Options, o.KeyAccess
FROM OrderDetails AS od
INNER JOIN Orders AS o
ON o.OrderID = od.OrderID
GROUP BY od.ProductName, od.Options, o.KeyAccess

(untested - see www.aspfaq.com/5006 if you prefer a tested reply)

--
pipe.jack NO[at]SPAM gmail.com
1/13/2006 6:36:54 AM
Jens,

Thanks again but this query returns only one output. I need to break it
down by product:
Qty ProductName Options
23 Yellow Car $500
..........etc......

Jack
Erland Sommarskog
1/13/2006 10:43:31 PM
(pipe.jack@gmail.com) writes:
[quoted text, click to view]

SELCET COUNT(DISTINCT OrderID), ProductName
FROM OrderDetails
GROUP BY ProductName


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Hugo Kornelis
1/13/2006 11:38:30 PM
[quoted text, click to view]

Hi Jack,

SELECT ProductName, Options, COUNT(*)
FROM OrderDetails
GROUP BY ProductName, Options

--
AddThis Social Bookmark Button