# count and divide query

Hi,
I have a query looking like this:
SELECT        dbo.shopCart.product, COUNT(dbo.shopCart.productID) AS Antal
FROM            dbo.[Order] INNER JOIN
dbo.shopCart ON dbo.[Order].orderID = dbo.shopCart.orderID INNER JOIN
dbo.[Order] AS o ON dbo.[Order].orderID = o.orderID
WHERE        (dbo.[Order].customerID IS NOT NULL)
GROUP BY dbo.shopCart.product, dbo.shopCart.productID
ORDER BY antal DESC
--------------------------------------------------
That conts how many products that has been sold per productID. I would like to add a field that counts the percentage rate of the row productIID as compared to the whole sale (all productIDs).

I would like my result to look like this:
Product               Antal           %
A product           1000           10%             (the total sale is 10 0000 products)

Is there a way to achieve this?

Thanks for help!

Peter
###### Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
SELECT        dbo.shopCart.product, COUNT(dbo.shopCart.productID) AS Antal,
ROUND(COUNT(dbo.shopCart.productID) / (select COUNT(dbo.shopCart.productID) from dbo.shopCart) * 100), 0) Percentage
FROM            dbo.[Order] INNER JOIN
dbo.shopCart ON dbo.[Order].orderID = dbo.shopCart.orderID INNER JOIN
dbo.[Order] AS o ON dbo.[Order].orderID = o.orderID
WHERE        (dbo.[Order].customerID IS NOT NULL)
GROUP BY dbo.shopCart.product, dbo.shopCart.productID
ORDER BY antal DESC
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Data EngineerCommented:
If you dont get correct percentage with Tigin's solution, then convert the counts into money.
SELECT        dbo.shopCart.product, COUNT(dbo.shopCart.productID) AS Antal,
ROUND(CAST(COUNT(dbo.shopCart.productID) AS MONEY) / CAST((select COUNT(dbo.shopCart.productID) from dbo.shopCart) AS MONEY) * 100,0) AS Percentage
FROM            dbo.[Order] INNER JOIN
dbo.shopCart ON dbo.[Order].orderID = dbo.shopCart.orderID INNER JOIN
dbo.[Order] AS o ON dbo.[Order].orderID = o.orderID
WHERE        (dbo.[Order].customerID IS NOT NULL)
GROUP BY dbo.shopCart.product, dbo.shopCart.productID
ORDER BY antal DESC

0
IT ManagerAuthor Commented:
Thanks for help!

Peter
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.