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 7612901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:02:04+00:00 2026-05-31T02:02:04+00:00

I have a table (items) which is in the following format: ITEMNO | WEEKNO

  • 0

I have a table (items) which is in the following format:

ITEMNO | WEEKNO | VALUE  
A1234  |   1    |  805  
A2345  |   2    |  14.50  
A3547  |   2    |  1396.70  
A2208  |   1    |  17.65  
A4326  |   6    |  19.99

It’s a table which shows the value of sales for items in a given week.

The results or what I want to display in a table format is the item number in a row followed by columns for each week containing the values, e.g.

ITEMNO | WK1 | WK2 | WK3 | WK4 | WK5  ...etc up to 52  
A1234  | 805 | 345 | 234 | 12  | 10   ...etc up to 52  
A2345  |  23 | 12  | 456 | 34  | 99   ...etc up to 52  
A3456  | 234 | 123 | 34  | 25  | 190  ...etc up to 52  

Although I’ve 52…so I’ve only data for up to week9 but that will increase with time.

So basically what it is I’m looking to display is the week number value as a column header.

Is this possible…although I’m tempted to just grab the data and display properly through code/(asp.net) but I was wondering if there was away to display it like this in SQL?

Does anyone know or think that that this might be the best way?

  • 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-31T02:02:06+00:00Added an answer on May 31, 2026 at 2:02 am

    There are two ways of doing this with static SQL and dynamic SQL:

    Static Pivot:

    SELECT P.ItemNo, IsNull(P.[1], 0) as Wk1, IsNull(P.[2], 0) as Wk2
        , IsNull(P.[3], 0) as Wk3, IsNull(P.[4], 0) as Wk4
        , IsNull(P.[5], 0) as Wk5, IsNull(P.[6], 0) as Wk6
        , IsNull(P.[7], 0) as Wk7, IsNull(P.[8], 0) as Wk8
        , IsNull(P.[9], 0) as Wk9
    FROM
    (
        SELECT ItemNo, WeekNo, [Value]
        FROM dbo.Items
    ) I
    PIVOT
    (
        SUM([Value])
        FOR WeekNo IN ([1], [2], [3], [4], [5], [6], [7], [8], [9])
    ) as P
    

    Dynamic Pivot:

    DECLARE
    @cols AS NVARCHAR(MAX),
    @y    AS INT,
    @sql  AS NVARCHAR(MAX);
    
    -- Construct the column list for the IN clause
    SET @cols = STUFF(
    (SELECT N',' + QUOTENAME(w) AS [text()]
    FROM (SELECT DISTINCT WeekNo AS W FROM dbo.Items) AS W
    ORDER BY W
    FOR XML PATH('')),
    1, 1, N'');
    
    
    -- Construct the full T-SQL statement
    -- and execute dynamically
    SET @sql = N'SELECT *
                FROM (SELECT ItemNo, WeekNo, Value
                        FROM dbo.Items) AS I
                PIVOT(SUM(Value) FOR WeekNo IN(' + @cols + N')) AS P;';
    
    EXEC sp_executesql @sql;
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Situation Say I have the following: a table items a table lists to which
I have a database which has an orders table and inventory table. The order-items
I have a table with a field (cat_tags) which contain the following rows:- id
I have the following code which inserts a task into a table with a
I have the following setup: <asp:treeview cssclass=draggable><items...> <table /> And I set it up
We have a table of transactions which is structured like the following : TranxID
I have a table Transactions(store_id, item_id, price). I want to find store_id's which sell
I have a table Users and a table Items In the Items table, I
I'm working on a very basic shopping cart system. I have a table items
I have a table of items. item has id, score 1, score 2. I

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.