Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

Current Order Date and Average Count of Orders for Three Prior Days Expand / Collapse
Author
Message
Posted Friday, October 5, 2007 8:14 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, November 14, 2012 11:39 AM
Points: 160, 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
Post #407648
Posted Friday, October 5, 2007 9:21 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 4:39 PM
Points: 4,576, Visits: 8,342
Total number of orders for the next 3 days is 7: 4 + 3 + 0.

Can you explain your logic more precisely?
Post #407656
Posted Friday, October 5, 2007 9:25 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, November 14, 2012 11:39 AM
Points: 160, Visits: 476
I can try. Can you explain what "ext" means precisely?
Post #407657
Posted Friday, October 5, 2007 9:37 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 4:39 PM
Points: 4,576, Visits: 8,342
I fixed the typo.
Post #407658
Posted Friday, October 5, 2007 10:56 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, November 14, 2012 11:39 AM
Points: 160, 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
Post #407662
Posted Friday, October 5, 2007 11:41 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 4:39 PM
Points: 4,576, Visits: 8,342
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.
Post #407668
Posted Saturday, October 6, 2007 12:14 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, November 14, 2012 11:39 AM
Points: 160, 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
Post #407672
Posted Saturday, October 6, 2007 1:35 AM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 4:39 PM
Points: 4,576, Visits: 8,342
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"?
Post #407673
Posted Saturday, October 6, 2007 12:19 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, July 8, 2014 8:36 AM
Points: 1,565, Visits: 2,378
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.
Post #407708
Posted Saturday, October 6, 2007 1:30 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, July 8, 2014 8:36 AM
Points: 1,565, Visits: 2,378
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.
Post #407713
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse