Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with query syntax

Status
Not open for further replies.

minli98

IS-IT--Management
Aug 30, 2005
178
US
Hi,

I need help writing a query statement. Say I have a sales table with two columns: SalesDate and ProductID. I want to calculate a variable x for each ProductID using the following function:

x(ProductId) = sum_for_all_sales_of_ProductId{
if ((datediff(d,SalesDate,getDate()) <= 100)
datediff(d,SalesDate,getDate)
else
0
}

In words, for each product, I want to calculate sum of the day difference between the sales dates and today. The part I don't know how to do is that when the datediff is greater than 100, I want to use zero rather than the day difference.

This is my attempt so far:
Code:
select ProductId, 
       dayIndex = sum(
           if (datediff(d,salesDate,getDate()) <= 100)
               datediff(d,salesDate,getDate())
           else
               0
           )
from sales
group by ProductId

Thank you in advance for your help.

Regards,
Min
 
Code:
select ProductId, 
       dayIndex = sum(
           Case When datediff(d,salesDate,getDate()) <= 100
                Then datediff(d,salesDate,getDate())
                else 0
                End
           )
from sales
group by ProductId

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top