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

  • SEARCH
  • Home
  • 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 7832135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:03:32+00:00 2026-06-02T12:03:32+00:00

I have a database with sample data represented by Table 1 below. How do

  • 0

I have a database with sample data represented by Table 1 below. How do I write an SQL query to display them in either Table 2 or Table 3 format?

Table 1                         Table 2                      
Date        |  Value            Year  |  Week  |  Total Value  |  % Change               
------------+-------            ------+-----+--|---------------|----------
19/12/2011  |  60               2012  |    1   |      295      |  656.41%               
20/12/2011  |  49               2012  |    0   |       39      |  -80.98%               
21/12/2011  |  42               2012  |   52   |      205      |  -41.76%               
22/12/2011  |  57               2011  |   51   |      352      |                 
23/12/2011  |  88 
24/12/2011  |  18               Table 3                    
25/12/2011  |  38               Year  |  Week  |  SUM1  |  Year  |  Week  |  SUM2  |  % Change
26/12/2011  |  16               ------+--------+--------+--------+--------+--------+-----------
27/12/2011  |  66               2012  |    1   |   295  |  2012  |    0   |   39   |  656.41%
28/12/2011  |  21               2012  |    0   |    39  |  2011  |   52   |  205   |  -80.98%
29/12/2011  |  79               2011  |   52   |   205  |  2011  |   51   |  352   |  -41.76%              
30/12/2011  |  7                2011  |   51   |   352  |    
31/12/2011  |  16
01/01/2012  |  39
02/01/2012  |  17
03/01/2012  |  86 
04/01/2012  |  55
05/01/2012  |  82
06/01/2012  |  0
07/01/2012  |  9
08/01/2012  |  46
  • 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-06-02T12:03:36+00:00Added an answer on June 2, 2026 at 12:03 pm

    My preference would be to run 1 query to aggregate Table 1 to the year/week level and then do the “% change” in another language, depending on your environment. However, if you truly needed a SQL-only solution, you could do something like this.

    create table t1 as
        select year(Date) as year, week(Date) as week, sum(Value) as totalvalue
        from table1
        group by year(Date) as year, week(Date) as week
        order by Date desc
    ;
    
    select a.year, a.month, a.totalvalue,
    (a.totalvalue-b.totalvalue)/b.totalvalue as pct_change
    from (
      select year, month, totalvalue, 
      case when week>1 then week-1 else 52 end as prevweek,
      case when week>1 then year else year-1 end as prevyear
      from t1
      ) a
        left outer join t1 b
        on a.prevweek=b.week and a.prevyear =b.year
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sample database like below, and I want to display only the
Suppose I have this database table (some sample code below) that stores the relationship
I have a simple database named customer with a single table data.I want to
I have a database 'Sample' with a table 'User' with columns 'FirstName', 'LastName', 'Category'.
I have a simple php page that displays data from a mysql database. I
I have installed a sample database, specifically Northwind from http://msdn.microsoft.com/en-us/library/8b6y4c7s.aspx . After installing the
Say you have a string 3.4564. A sample set in the database has these
I have a database with a simple flyweight table referenced by another table. Let's
I use c#, linq and EF4. I have two tables in my DataBase represented
I have been tasked to write a module for importing data into a client's

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.