Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7092703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:20:51+00:00 2026-05-28T08:20:51+00:00

I have a stored proc that produces a table like this: PeriodStart PeriodEnd TotalActive

  • 0

I have a stored proc that produces a table like this:

PeriodStart     PeriodEnd     TotalActive     TotalAdded     TotalRemoved
2011-03-01      2011-03-07    123             456            789
2011-03-08      2011-03-14    567             789            123
2011-03-15      2011-03-21    444             555            666

etc…

I need to display the results in a table that looks like this:

          03-01-2011     03-08-2011     03-15-2011
Active    123            567            444
Added     456            789            555
Removed   789            123            666

I’ve never had to use PIVOT before so I’m a little unsure of how to modify the below SQL in order to achieve the desired result. Here’s the sproc:

ALTER PROCEDURE [dbo].[TrendReport]
    @DateStart datetime,
    @DateEnd datetime,
    @Frequency varchar(5)
AS
BEGIN
    SELECT
      PeriodStart,
      PeriodEnd,
      SUM(TotalActive) As TotalActive,
      SUM(TotalAdded) As TotalAdded,
      SUM(TotalRemoved) As TotalRemoved
    FROM (
      SELECT
        PeriodStart = CASE @Frequency
          WHEN 'day'     THEN DATEADD(hour, 0, DATEDIFF(DAY, 0, [Date]))
          WHEN 'week'    THEN DATEADD(hour, 0, DATEDIFF(DAY, 0, DATEADD(DAY, 1 - DATEPART(WEEKDAY, [Date]), [Date])))
          WHEN 'month'   THEN DATEADD(hour, 0, DATEDIFF(DAY, 0, DATEADD(MONTH,   DATEDIFF(MONTH,   0, [Date]), 0)))
          WHEN 'quarter' THEN DATEADD(hour, 0, DATEDIFF(DAY, 0, DATEADD(QUARTER, DATEDIFF(QUARTER, 0, [Date]), 0)))
          WHEN 'year'    THEN DATEADD(hour, 0, DATEDIFF(DAY, 0, DATEADD(YEAR,    DATEDIFF(YEAR,    0, [Date]), 0)))
        END,
        PeriodEnd   = CASE @Frequency
          WHEN 'day'     THEN DATEADD(s, -1, DATEADD(day, 1, DATEDIFF(DAY, 0, [Date])))
          WHEN 'week'    THEN DATEADD(s, -1, DATEADD(hour, 0, DATEDIFF(DAY, -1, DATEADD(DAY, 7 - DATEPART(WEEKDAY, [Date]), [Date]))))
          WHEN 'month'   THEN DATEADD(s, -1, DATEADD(hour, 0, DATEDIFF(DAY, -1, DATEADD(DAY, -1, DATEADD(MONTH,   DATEDIFF(MONTH,   0, [Date]) + 1, 0)))))
          WHEN 'quarter' THEN DATEADD(s, -1, DATEADD(hour, 0, DATEDIFF(DAY, -1, DATEADD(DAY, -1, DATEADD(QUARTER, DATEDIFF(QUARTER, 0, [Date]) + 1, 0)))))
          WHEN 'year'    THEN DATEADD(s, -1, DATEADD(hour, 0, DATEDIFF(DAY, -1, DATEADD(DAY, -1, DATEADD(YEAR,    DATEDIFF(YEAR,    0, [Date]) + 1, 0)))))
        END,
        TotalActive,
        TotalAdded,
        TotalRemoved
      FROM TrendReport P
      WHERE [Date] BETWEEN @DateStart AND @DateEnd
    ) s
    GROUP BY
      PeriodStart,
      PeriodEnd
    ORDER BY PeriodStart

END
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:20:52+00:00Added an answer on May 28, 2026 at 8:20 am

    You actually need to first apply UNPIVOT to rotate the three quantity types from columns to rows and then apply PIVOT to rotate the period start date to columns and aggregate the quantities.

    Here’s the thing with using the PIVOT operator though, you need to know what columns you will have ahead of time (03-01-2011, 03-08-2011, 03-15-2011), it will need hard coded into the query (unless you want to get down and dirty with dynamic sql, which i would read up on before using in production)

    Here is the solution:

    WITH CTE AS (
        SELECT
          PeriodStart,
          PeriodEnd,
          SUM(TotalActive) As TotalActive,
          SUM(TotalAdded) As TotalAdded,
          SUM(TotalRemoved) As TotalRemoved
        FROM (
          SELECT
            PeriodStart = CASE @Frequency
              WHEN 'day'     THEN DATEADD(hour, 0, DATEDIFF(DAY, 0, [Date]))
              WHEN 'week'    THEN DATEADD(hour, 0, DATEDIFF(DAY, 0, DATEADD(DAY, 1 - DATEPART(WEEKDAY, [Date]), [Date])))
              WHEN 'month'   THEN DATEADD(hour, 0, DATEDIFF(DAY, 0, DATEADD(MONTH,   DATEDIFF(MONTH,   0, [Date]), 0)))
              WHEN 'quarter' THEN DATEADD(hour, 0, DATEDIFF(DAY, 0, DATEADD(QUARTER, DATEDIFF(QUARTER, 0, [Date]), 0)))
              WHEN 'year'    THEN DATEADD(hour, 0, DATEDIFF(DAY, 0, DATEADD(YEAR,    DATEDIFF(YEAR,    0, [Date]), 0)))
            END,
            PeriodEnd   = CASE @Frequency
              WHEN 'day'     THEN DATEADD(s, -1, DATEADD(day, 1, DATEDIFF(DAY, 0, [Date])))
              WHEN 'week'    THEN DATEADD(s, -1, DATEADD(hour, 0, DATEDIFF(DAY, -1, DATEADD(DAY, 7 - DATEPART(WEEKDAY, [Date]), [Date]))))
              WHEN 'month'   THEN DATEADD(s, -1, DATEADD(hour, 0, DATEDIFF(DAY, -1, DATEADD(DAY, -1, DATEADD(MONTH,   DATEDIFF(MONTH,   0, [Date]) + 1, 0)))))
              WHEN 'quarter' THEN DATEADD(s, -1, DATEADD(hour, 0, DATEDIFF(DAY, -1, DATEADD(DAY, -1, DATEADD(QUARTER, DATEDIFF(QUARTER, 0, [Date]) + 1, 0)))))
              WHEN 'year'    THEN DATEADD(s, -1, DATEADD(hour, 0, DATEDIFF(DAY, -1, DATEADD(DAY, -1, DATEADD(YEAR,    DATEDIFF(YEAR,    0, [Date]) + 1, 0)))))
            END,
            TotalActive,
            TotalAdded,
            TotalRemoved
          FROM TrendReport P
          WHERE [Date] BETWEEN @DateStart AND @DateEnd
        ) s
        GROUP BY
          PeriodStart,
          PeriodEnd
        ORDER BY PeriodStart
    
    ), U AS (
        SELECT qty_type, PeriodStart, qty
        FROM CTE
        UNPIVOT(qty FOR qty_type IN([TotalActive], [TotalAdded], [TotalRemoved]))Q
    )     
    SELECT * FROM U 
    PIVOT (SUM(Qty) FOR PeriodStart IN([03/01/2011], [03/08/2011], [03/15/2011])) AS P
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored proc that takes an input of xml value like this:
I have a Stored Proc that executes and outputs like this, Plant1 Top1 12
So I have this stored proc that will not get created when I run
I have a stored proc that takes one integer parameter. I am calling this
I have a stored proc that inserts XML into an underlying table. Unfortunately the
We have a stored proc that grabs that data from a table, let's call
I have a stored proc that returns 4 rows in a table. Each row
Say i have a stored proc that inserts some data into a table. If
I have a stored proc on sql server 2008 that excepts a int parameter.
I have a stored proc with the basic layout below that returns a sys_refcursor

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.