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

I am not able to get count in weekwise i.e 7 days shipped orders to be one week, please check my query as follows:


SQL
Declare @CurrentDate nvarchar(50);
SET @CurrentDate='2012-07-25'

BEGIN 

;WITH DateList AS
(
SELECT  DATEADD(WEEK, 0, CONVERT(DATETIME, @CurrentDate)) AS CreateDateTime, 1 AS Cnter
UNION ALL
SELECT  DATEADD(WEEK, -1, CreateDateTime), DateList.Cnter + 1
FROM DateList
WHERE DateList.Cnter  < 5
)
SELECT  DateList.CreateDateTime AS ShipWeek, COALESCE(Temp.TotalCount, 0) AS TotalCount
FROM DateList
LEFT JOIN  (
SELECT  COUNT(Id) TotalCount
,DATEADD(WEEK, DATEDIFF(WEEK, '19000101',CreatedDateTime),'19000101') AS ShipWeek
FROM ShipmentDetail 
WHERE CreatedDateTime
BETWEEN DATEADD(DAY,-30,@CurrentDate) AND @CurrentDate
GROUP BY  DATEADD(WEEK, DATEDIFF(WEEK, '19000101',CreatedDateTime),'19000101') 
) Temp
ON CONVERT(VARCHAR(10), DateList.CreateDateTime, 112) = Temp.ShipWeek
END


Result Table is as follows:

ShipWeek TotalCount

2012-07-25 00:00:00.000 0
2012-07-18 00:00:00.000 0
2012-07-11 00:00:00.000 0
2012-07-04 00:00:00.000 0
2012-06-27 00:00:00.000 0
please give me the exact solution.

Thanks&Regards,

Raghu.
Posted
Updated 13-Aug-12 23:11pm
v2

1 solution

What happens in your query is that the join doesn't result in any matches.

In your sample the DateList shows wednesdays, where the temp subquery results in mondays. You can verify this by changing the @CurrentDate into a monday. For example:

SQL
SET @CurrentDate='2012-07-23'


What you need to do is, that you convert your DateList to mondays as well. Like this:

SQL
WITH DateList AS
(
SELECT  DATEADD(WEEK, 0, DATEADD(WEEK, DATEDIFF(WEEK, '19000101',@CurrentDate), '19000101')) AS CreateDateTime, 1 AS Cnter
UNION ALL
SELECT  DATEADD(WEEK, -1, CreateDateTime), DateList.Cnter + 1
FROM DateList
WHERE DateList.Cnter  < 5
)


I couldn't fully tests the solution, because I don't have data available. But this should work.


Aside from this solution there might be another thing you might look at. In the temp subquery you have the following where-clause
SQL
WHERE CreatedDateTime
BETWEEN DATEADD(DAY,-30,@CurrentDate) AND @CurrentDate

You select 30 days. But in your DateList you have 5 weeks, which is 35 days. This might result in incomplete data in the oldest week. But that actually depends on the objective you want to achieve with this query. Also when you want to use this query to get historical data, you might want to change the where clause into the following to ensure that the whole youngest week is covered (the join would filter the week you don't need).
SQL
WHERE CreatedDateTime
BETWEEN DATEADD(DAY,-35,@CurrentDate) AND DATEADD(DAY,7,@CurrentDate)
 
Share this answer
 

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