SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Using DATEADD to determine Del Dates


Using DATEADD to determine Del Dates

Author
Message
tt-615680
tt-615680
SSC Eights!
SSC Eights! (877 reputation)SSC Eights! (877 reputation)SSC Eights! (877 reputation)SSC Eights! (877 reputation)SSC Eights! (877 reputation)SSC Eights! (877 reputation)SSC Eights! (877 reputation)SSC Eights! (877 reputation)

Group: General Forum Members
Points: 877 Visits: 1236
Hi All,

I am trying to determine when deliveries have occurred but the results that I am getting seem to be all the same for each week. Here is part of my script.

COUNT(CASE WHEN DATEADD(WK,1,Delivery_date) = 1 THEN 1 ELSE 0 END) AS [Delwithin1weeks]
COUNT(CASE WHEN DATEADD(WK,2,Delivery_date) = 1 THEN 1 ELSE 0 END) AS [Delwithin2weeks]
COUNT(CASE WHEN DATEADD(WK,3,Delivery_date) = 1 THEN 1 ELSE 0 END) AS [Delwithin3weeks]

Would someone let me know how else I can write the script please.

Thank you
vishal#SQL
vishal#SQL
SSC Veteran
SSC Veteran (244 reputation)SSC Veteran (244 reputation)SSC Veteran (244 reputation)SSC Veteran (244 reputation)SSC Veteran (244 reputation)SSC Veteran (244 reputation)SSC Veteran (244 reputation)SSC Veteran (244 reputation)

Group: General Forum Members
Points: 244 Visits: 198
Hi,
Do you want the count of deliveries where deliverydate within one week, two weeks and three weeks?

w00t
tt-615680
tt-615680
SSC Eights!
SSC Eights! (877 reputation)SSC Eights! (877 reputation)SSC Eights! (877 reputation)SSC Eights! (877 reputation)SSC Eights! (877 reputation)SSC Eights! (877 reputation)SSC Eights! (877 reputation)SSC Eights! (877 reputation)

Group: General Forum Members
Points: 877 Visits: 1236
vishal#SQL (2/10/2011)
Hi,
Do you want the count of deliveries where deliverydate within one week, two weeks and three weeks?


Thank you for the reply. Yes I do need the count of deliveries where deliverydate within one week, two weeks and three weeks.

Thank you
vishal#SQL
vishal#SQL
SSC Veteran
SSC Veteran (244 reputation)SSC Veteran (244 reputation)SSC Veteran (244 reputation)SSC Veteran (244 reputation)SSC Veteran (244 reputation)SSC Veteran (244 reputation)SSC Veteran (244 reputation)SSC Veteran (244 reputation)

Group: General Forum Members
Points: 244 Visits: 198
Try This

SELECT SUM(CASE WHEN delivery_date BETWEEN DATEADD(WK,-1,getDAte()) AND getDAte() THEN 1 ELSE 0 END) AS [Delwithin1weeks]
, SUM(CASE WHEN delivery_date BETWEEN DATEADD(WK,-2,getDAte()) AND getDAte() THEN 1 ELSE 0 END) AS [Delwithin2weeks]
, SUM(CASE WHEN delivery_date BETWEEN DATEADD(WK,-3,getDAte()) AND getDAte() THEN 1 ELSE 0 END) AS [Delwithin2weeks]

You need to USE SUM function instead of COUNT.

w00t
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search