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


Current Order Date and Average Count of Orders for Three Prior Days


Current Order Date and Average Count of Orders for Three Prior Days

Author
Message
David-250683
David-250683
Mr or Mrs. 500
Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)

Group: General Forum Members
Points: 524 Visits: 476
Want to get

1) the OrderDate and

2) the daily average of total Orders for that day and two previous days where there are orders.



As an example, I use the Shipping.Orders Table from the Northwind database from SQL Server 2005.



USE Northwind

GO



SELECT OrderDate, COUNT(OrderDate)

FROM Shipping.Orders

WHERE OrderDate Between '04-30-1998' AND '05-06-1998'

GROUP BY OrderDate

ORDER BY 1 DESC



With this basic query you can see there are 4 orders on 5/6, 4 orders on 5/5 and 3 orders on 5/4.

Total orders for the three day period is 11. Average of 3.67.

Total orders for the next three day period starting from the next OrderDate is 10. Average of 3.33



So I would want the output to look like

OrderDate AverageOrderCount

5/6 3.67

5/5 3.33



and so on...



SELECT COUNT(OrderDate)

FROM Shipping.Orders

WHERE OrderDate Between '04-30-1998' AND '05-06-1998'

ORDER BY 1 DESC
Sergiy
Sergiy
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18321 Visits: 12296
Total number of orders for the next 3 days is 7: 4 + 3 + 0.



Can you explain your logic more precisely?
David-250683
David-250683
Mr or Mrs. 500
Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)

Group: General Forum Members
Points: 524 Visits: 476
I can try. Can you explain what "ext" means precisely?
Sergiy
Sergiy
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18321 Visits: 12296
I fixed the typo.
David-250683
David-250683
Mr or Mrs. 500
Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)

Group: General Forum Members
Points: 524 Visits: 476
Here's one solution but it is quite lengthy. I'm sure there is a more efficient answer. So please show me your wisdom!

I used a different column though - RequiredDate instead of OrderDate.



Here's the query to check the result set against. And below that - my inefficient, but working solution.



--Check Table

USE Northwind

GO



SELECT RequiredDate, COUNT(RequiredDate)

FROM Shipping.Orders

WHERE RequiredDate Between '1998-05-01' AND '1998-06-11'

GROUP BY RequiredDate

ORDER BY 1 DESC



------- Inefficient Solution --------

DECLARE @counter int,

@counter2 int,

@date smalldatetime,

@scope int

SELECT @counter = 1,

@counter2 = 1



DECLARE @Time TABLE (

ID int IDENTITY (1,1),

timex smalldatetime

)



--Gather dates to report on

-- Note: not all dates have a RequiredDate

INSERT @Time

(timex)

SELECT DISTINCT RequiredDate

FROM Shipping.Orders

WHERE RequiredDate BETWEEN '1998-05-01' AND '1998-06-11'

ORDER BY 1 DESC



-- Select 1st Date

SELECT @date = (SELECT timex FROM @Time WHERE ID = 1)



-- Create Results table

DECLARE @Results TABLE (

t_RequiredDate smalldatetime,

t_count int

)



SELECT @scope = SCOPE_IDENTITY()



-- Set @scope to number of RequiredDates in @Time

WHILE @counter < @scope

BEGIN



INSERT @Results

SELECT timex,

(SELECT COUNT(RequiredDate)

FROM Shipping.Orders

WHERE RequiredDate IN (SELECT distinct TOP 3 RequiredDate -- Only want count of last three RequiredDates

FROM Shipping.Orders

WHERE RequiredDate

BETWEEN DATEADD(day, -10, @date) AND @date order by 1 desc) --Goes back 10 because not all dates have values

)

FROM @Time

WHERE ID = @counter2



SELECT @counter2 = @counter2 + 1

SELECT @date = (SELECT timex FROM @Time WHERE ID = @counter2)



SELECT @counter = @counter + 1

END



-- Retrieve Results

SELECT t_RequiredDate,

LTRIM(STR(CAST(t_count as float)/ 3, 4,2)) --obtain average

FROM @Results

ORDER BY 1 DESC
Sergiy
Sergiy
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18321 Visits: 12296
Still it's not clear what are trying to achieve.



As for me the solution you posted is logically wrong.

But I cannot be sure because I don't have an idea what's the task you need to solve.
David-250683
David-250683
Mr or Mrs. 500
Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)Mr or Mrs. 500 (524 reputation)

Group: General Forum Members
Points: 524 Visits: 476
Here's a shorter version, but I know there is still room for improvement.



DECLARE @date smalldatetime,

@counter int

SELECT @date = '1998-06-11',

@counter = 1



DECLARE @Results TABLE (

t_RequiredDate smalldatetime,

t_count int

)



-- Select @counter to desired result set size

WHILE @counter < 10

BEGIN



WHILE (SELECT DISTINCT RequiredDate FROM Shipping.Orders WHERE RequiredDate = @date) IS NULL

BEGIN

SELECT @date = DATEADD(day, -1, @date)

END



INSERT @Results

SELECT RequiredDate,

(SELECT COUNT(RequiredDate)

FROM Shipping.Orders

WHERE RequiredDate IN (SELECT distinct TOP 3 RequiredDate -- Only want count of last three RequiredDates

FROM Shipping.Orders

WHERE RequiredDate

BETWEEN DATEADD(day, -10, @date) AND @date order by 1 desc) --Goes back 10 because not all dates have values

)

FROM Shipping.Orders

WHERE RequiredDate = @date



SELECT @date = DATEADD(day, -1, @date)



SELECT @counter = @counter + 1

END



SELECT DISTINCT t_RequiredDate,

LTRIM(STR(CAST(t_count as float)/ 3, 4,2)) --obtain average

FROM @Results

ORDER BY 1 DESC
Sergiy
Sergiy
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18321 Visits: 12296
David,

that was not much help.



You cannot build any right query unless you know what you are querying for.



I still see the same logical error (according to my understanding) in the approach, but I cannot tell for sure if it's an error in fact.



Can you tell IN PLAIN ENGLISH WHAT ARE YOU LOOKING FOR?



What does it really mean: the day and 2 earlier days?

Which days to be taken as "earlier days"?
Greg Snidow
Greg Snidow
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3235 Visits: 2494
It kind of seems to me David is looking for a rolling three day average. If today is the third day of the month, say 10/3, then he needs the average for 10/1+10/2+10/3. Tomorrow will be the fourth day of the month so he will need the average for 10/2+10/3+10/4, and so on.

Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
Greg Snidow
Greg Snidow
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3235 Visits: 2494
David I do not have the sample databases, so I improvised. I think this may be what you are looking to do? I am fairly sure this is *not* the way you want to do this because it creates a triangular join, but it works.



--Create test table and populate with data



IF OBJECT_ID('tblOrders','u') IS NOT NULL

DROP TABLE tblorders

CREATE TABLE tblOrders

(

ID INT IDENTITY(1,1),

ORDER_DT SMALLDATETIME,

)

INSERT INTO tblOrders (ORDER_DT)

SELECT '10/5/2007' UNION ALL

SELECT '10/5/2007' UNION ALL

SELECT '10/5/2007' UNION ALL

SELECT '10/5/2007' UNION ALL

SELECT '10/4/2007' UNION ALL

SELECT '10/4/2007' UNION ALL

SELECT '10/4/2007' UNION ALL

SELECT '10/3/2007' UNION ALL

SELECT '10/2/2007' UNION ALL

SELECT '10/2/2007' UNION ALL

SELECT '10/2/2007' UNION ALL

SELECT '10/1/2007' UNION ALL

SELECT '10/3/2007' UNION ALL

SELECT '10/6/2007' UNION ALL

SELECT '10/6/2007' UNION ALL

SELECT '10/6/2007' UNION ALL

SELECT '10/6/2007' UNION ALL

SELECT '10/6/2007' UNION ALL

SELECT '10/6/2007' UNION ALL

SELECT '10/6/2007' UNION ALL

SELECT '10/3/2007' UNION ALL

SELECT '10/2/2007' UNION ALL

SELECT '10/1/2007' UNION ALL

SELECT '10/6/2007'



--Create temp table to hold total # orders per day



IF OBJECT_ID('TempDB..#Temp','u') IS NOT NULL

DROP TABLE #Temp

CREATE TABLE #Temp

(

ID INT IDENTITY(1,1),

ORDER_DT SMALLDATETIME,

DT_Total FLOAT,

)

--Populate temp table

INSERT INTO #Temp

SELECT

ORDER_DT,

DT_Total = COUNT(order_dt)

FROM tblOrders

GROUP BY order_dt

ORDER BY order_dt DESC



--Find 3 day average

SELECT

t1.ORDER_DT,

t1.DT_Total,

(

SELECT

ROUND(SUM(t2.dt_total)/3,1)

FROM #temp t2

WHERE t2.order_dt = t1.order_dt

OR t2.order_dt = t1.order_dt - 1

OR t2.order_dt = t1.order_dt -2

) AS Total

FROM #Temp t1

Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
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