Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I have the following SELECT statement to retrieve products from the database and show it on a chart
SQL
SELECT DISTINCT productName, SUM(quantity) AS Expr1, orderPeriod 
     FROM WP_analytic WHERE (orderPeriod >= GETDATE() - 30) GROUP BY productName, orderPeriod

I am able to retrieve the product and display however there will be duplicates of a particular product if the orderPeriod is different days. OrderPeriod data type is DateTime.

I want to make it such that for a period of 30 days, it will show accumulated quantity for a particular product within the period without any duplicates on the chart.

Any idea how to achieve this?

It shows duplicate products if the orderPeriod is two different days.
Posted
Updated 3-Aug-13 7:19am
v3

1 solution

You have duplicates because the orderPeriod field is in the SELECT statement.

Remove this field from both the SELECT and GROUP BY and you should get a total for each product without duplicates.
SQL
SELECT DISTINCT productName, SUM(quantity) AS Expr1
     FROM WP_analytic WHERE (orderPeriod >= GETDATE() - 30) GROUP BY productName
 
Share this answer
 
Comments
Jerrell77 3-Aug-13 13:29pm    
Hey man thanks! It worked:)
ridoy 3-Aug-13 13:29pm    
my 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900