Weekly report through whole year

  • Hi there

    I have 2 tables and sample data:

    --------------------------------------------------------------------------------------------------------------------------------------------------------

    CREATE TABLE [dbo].[tblProduct](

    [ProductID] [int] IDENTITY(1,1) NOT NULL,

    [Name] [varchar](50) NOT NULL,

    CONSTRAINT [PK_tblProduct] PRIMARY KEY CLUSTERED

    (

    [ProductID] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    --------------------------------------------------------------------------------------------------------------------------------------------------------

    CREATE TABLE [dbo].[tblOrder](

    [OrderID] [int] IDENTITY(1,1) NOT NULL,

    [ProductID] [int] NOT NULL,

    [OrderDate] [datetime] NOT NULL,

    CONSTRAINT [PK_tblOrder] PRIMARY KEY CLUSTERED

    (

    [OrderID] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    --------------------------------------------------------------------------------------------------------------------------------------------------------

    INSERT INTO tblProduct (Name) VALUES('Coffee')

    INSERT INTO tblProduct (Name) VALUES('Tea')

    INSERT INTO tblProduct (Name) VALUES('Lager')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (1, '2013-01-01 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (1, '2013-01-08 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (2, '2013-01-08 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (3, '2013-01-09 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (1, '2013-01-10 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (2, '2013-01-10 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (3, '2013-01-11 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (1, '2013-01-11 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (2, '2013-01-15 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (1, '2013-01-18 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (1, '2013-01-30 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (2, '2013-05-01 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (3, '2013-07-15 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (1, '2013-09-23 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (1, '2013-10-01 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (1, '2013-10-08 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (2, '2013-10-11 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (1, '2013-11-01 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (3, '2013-11-06 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (1, '2013-11-08 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (3, '2013-12-10 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (1, '2013-12-25 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (2, '2013-12-30 09:00:00')

    INSERT INTO tblOrder (ProductID, OrderDate) VALUES (1, '2013-12-30 09:00:00')

    --------------------------------------------------------------------------------------------------------------------------------------------------------

    I need to generate report which shows number of orders for each ProductID for each week in year 2013. Ideally would be to generate 54 weeks as pivot table so week1, week2 would be columns and 3 rows for 3 products and order count for each product each week.

    Product

  • -- Query 1 have a gander at the data

    SELECT

    o.*,

    p.Name,

    x.CurrWeek

    FROM tblOrder o

    LEFT JOIN tblProduct p ON p.ProductID = o.ProductID

    CROSS APPLY (SELECT CurrWeek = DATEPART(week,o.OrderDate)) x

    WHERE o.OrderDate >= '2013-01-01 00:00:00.000'

    AND o.OrderDate < '2014-01-01 00:00:00.000'

    -- Query 2 calculate the week number and try aggregate

    SELECT

    p.Name,

    [rows] = COUNT(*),

    x.CurrWeek

    FROM tblOrder o

    LEFT JOIN tblProduct p ON p.ProductID = o.ProductID

    CROSS APPLY (SELECT CurrWeek = DATEPART(week,OrderDate)) x

    WHERE o.OrderDate >= '2013-01-01 00:00:00.000'

    AND o.OrderDate < '2014-01-01 00:00:00.000'

    GROUP BY p.Name, x.CurrWeek

    -- Query 3 solution: set up crosstab, compare results to Query 2

    SELECT

    p.Name,

    week01 = SUM(CASE WHEN x.CurrWeek = 1 THEN 1 END),

    week02 = SUM(CASE WHEN x.CurrWeek = 2 THEN 1 END),

    week03 = SUM(CASE WHEN x.CurrWeek = 3 THEN 1 END),

    week04 = SUM(CASE WHEN x.CurrWeek = 4 THEN 1 END),

    week53 = SUM(CASE WHEN x.CurrWeek = 53 THEN 1 END),

    week54 = SUM(CASE WHEN x.CurrWeek = 54 THEN 1 END)

    FROM tblOrder o

    LEFT JOIN tblProduct p ON p.ProductID = o.ProductID

    CROSS APPLY (SELECT CurrWeek = DATEPART(week,o.OrderDate)) x

    WHERE o.OrderDate >= '2013-01-01 00:00:00.000'

    AND o.OrderDate < '2014-01-01 00:00:00.000'

    GROUP BY p.Name

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Brilliant!!

    That's what i needed.

    Thanks a lot!

  • You're welcome. Thanks loads for posting a sample data script - it doesn't half make a difference 🙂

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • What about if I want same scenario between 1.4.2013 and 14.42013? So each day within this date range is displayed as column?

  • roman 19626 (4/17/2013)


    What about if I want same scenario between 1.4.2013 and 14.42013? So each day within this date range is displayed as column?

    Can you show what you mean by this, using a script?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Hi

    Exactly same tables and data. But instead of having 'week1', 'week2' etc as columns I would like to have dates for example '01.04.2013', '02.04.2013', etc. I need to be able selected date range so If I select between 01.04.2013 to 30.04.2013 I will have 30 columns starting with column '01.04.2013' and finishing with '30.04.2013'. I need same output but instead of weekly report I need daily report within date range.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply