开发者

TSQL: grouping customer orders by week

开发者 https://www.devze.com 2022-12-22 13:06 出处:网络
I have a table with a collection of orders. The fields are: customerName (text) DateOfOrder (datetime).

I have a table with a collection of orders. The fields are:

  • customerName (text)
  • DateOfOrder (datetime).

I would like to show total开发者_如何学运维s of orders per week per customer. I would like to have it arranged for the Friday of each week so that it looks like this:

all dates follow mm/dd/yyyy

"bobs pizza", 3/5/2010, 10
"the phone co",3/5/2010,5
"bobs pizza", 3/12/2010, 3
"the phone co",3/12/2010,11

Could somebody please show me how to do this?

Thanks


Make a field which you can group by more easily.

SELECT COUNT(OrderID), WeekStart
FROM 
(
 SELECT *, 
  dateadd(week, datediff(day,'20000107',yourDate) / 7, '20000107') AS WeekStart  
 FROM Orders
) o
GROUP BY WeekStart;

20000107 is a known Friday.


This isn't a complete order, but it will give you the week #. Just need to add grouping.

SELECT
Cast(DatePart(YEAR, dateadd(month, datediff(month, 0, DateOfOrder),0))
    AS nvarchar(20))
 + ' '
 + RIGHT('0' + Cast(DatePart(WK, DateOfOrder) AS nvarchar(20)), 2)
,CustomerName
FROM Orders


Just try to give this a go

DECLARE @Table TABLE(
        customerName VARCHAR(50),
        DateOfOrder DATETIME,
        Quantity FLOAT
        )

INSERT INTO @Table SELECT 'bobs pizza', '1/5/2010', 10      
INSERT INTO @Table SELECT 'bobs pizza', '3/5/2010', 10 
INSERT INTO @Table SELECT 'bobs pizza', '3/4/2010', 10 
INSERT INTO @Table SELECT 'the phone co','3/5/2010',5 
INSERT INTO @Table SELECT 'bobs pizza', '3/12/2010', 3 
INSERT INTO @Table SELECT 'the phone co','3/12/2010',11 
INSERT INTO @Table SELECT 'the phone co','3/13/2010',11 
INSERT INTO @Table SELECT 'the phone co','3/15/2010',11 


SELECT  DateFirday,
        DATENAME(dw, DateFirday),
        SUM(Quantity) Total
FROM    (
            SELECT  *,
                    DATEADD(week, DATEPART(WEEK, DateOfOrder) - 1, CAST('01 Jan' + CAST(DATEPART(year,DateOfOrder) AS VARCHAR(4)) AS DATETIME)) DateFirday
            FROM    @Table
        ) sub
GROUP BY DateFirday

Output

DateFirday                                             Total
----------------------- ------------------------------ ----------------------
2010-01-08 00:00:00.000 Friday                         10
2010-03-05 00:00:00.000 Friday                         25
2010-03-12 00:00:00.000 Friday                         25
2010-03-19 00:00:00.000 Friday                         11

If I missed something with the Friday dates, let me know so I can have a look.


This will work as long as there is no time component:

SELECT     DateOfOrder - DATEPART(dw, DateOfOrder) + 7 AS weekEndingDate, customerName, COUNT(*) AS totalOrders
FROM         Orders
GROUP BY DateOfOrder - DATEPART(dw, DateOfOrder) + 7, customerName

This assumes that DATEFIRST hasn't been changed. The subtraction will roll back to the previos Saturday, then + 7 gets to Friday.

If there is a time component, use one of the standard techniques to strip it off:

DATEADD(day, DATEDIFF(day, 0, DateOfOrder), 0) - DATEPART(dw, DateOfOrder) + 7

CAST(FLOOR(CAST(DateOfOrder as FLOAT)) as DATETIME) - DATEPART(dw, DateOfOrder) + 7

or, for SS2008

CAST(DateOfOrder AS date) - DATEPART(dw, DateOfOrder) + 7


I thought I would add this since I did not like the existing best answer as well as I could. It just subtracts the weekday and then adds back to the desired day. Adjust the 6 if you want a different day of the week (or if your DATEFIRST is set differently, from what I gather).

SELECT Friday, COUNT(CustomerName)
FROM 
(
 SELECT *, 
  CAST(DATEADD(d, 6 - DATEPART(dw,DateOfOrder), DateOfOrder) AS date) AS Friday  
 FROM Orders
) o
GROUP BY Friday;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号