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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:28:04+00:00 2026-05-10T20:28:04+00:00

I have a data set that is organized in the following manner: Timestamp|A0001|A0002|A0003|A0004|B0001|B0002|B0003|B0004 …

  • 0

I have a data set that is organized in the following manner:

Timestamp|A0001|A0002|A0003|A0004|B0001|B0002|B0003|B0004 ... ---------+-----+-----+-----+-----+-----+-----+-----+----- 2008-1-1 |  1  |  2  | 10  |   6 |  20 |  35 | 300 |  8 2008-1-2 |  5  |  2  |  9  |   3 |  50 |  38 | 290 |  2     2008-1-4 |  7  |  7  | 11  |   0 |  30 |  87 | 350 |  0 2008-1-5 |  1  |  9  |  1  |   0 |  25 | 100 |  10 |  0 ... 

Where A0001 is Value A of item #1 and B0001 is Value B of item #1. There can be over 60 different items in a table, and each item has an A value column and a B value column, meaning a total of over 120 columns in the table.

Where I want to get to is a 3 column result (Item index, A Value, B Value) that sums the A and B values for each item:

Index | A Value | B Value ------+---------+--------  0001 |   14    |   125  0002 |   20    |   260  0003 |   31    |   950  0004 |    9    |    10  ....  

As I am going from columns to rows I would expect a pivot in the solution, but I am not sure of how to flesh it out. Part of the issue is how to strip out the A’s and B’s to form the values for the Index column. The other part is that I have never had to use a Pivot before, so I am stumbling over the basic syntax as well.

I think that ultimately I need to have a multi step solution that first builds the summations as:

ColName | Value --------+------ A0001   |  14 A0002   |  20 A0003   |  31 A0004   |   9 B0001   | 125 B0002   | 260 B0003   | 950 B0004   |  10 

Then modify the ColName data to strip out the index:

ColName | Value | Index | Aspect --------+-------+-------+------- A0001   |  14   | 0001  |  A A0002   |  20   | 0002  |  A A0003   |  31   | 0003  |  A A0004   |   9   | 0004  |  A B0001   | 125   | 0001  |  B B0002   | 260   | 0002  |  B B0003   | 950   | 0003  |  B B0004   |  10   | 0004  |  B 

Finally self join to move the B values up next to the A Values.

This seems to be a long winded process to get what I want. So I am after advice as to whether I am headed down the right path, or is there another approach that I have over looked that will make my life so much easier.

Note 1) The solution has to be in T-SQL on MSSQL 2005.

Note 2) The format of the table cannot be changed.

Edit Another method I have thought about uses UNIONs and individual SUM()s on each column:

SELECT '0001' as Index, SUM(A0001) as A, SUM(B0001) as B FROM TABLE UNION SELECT '0002' as Index, SUM(A0002) as A, SUM(B0002) as B FROM TABLE UNION SELECT '0003' as Index, SUM(A0003) as A, SUM(B0003) as B FROM TABLE UNION SELECT '0004' as Index, SUM(A0004) as A, SUM(B0004) as B FROM TABLE UNION ... 

But this approach really doesn’t look very nice either

EDIT So far there are 2 great responses. But I would like to add two more conditions to the query 🙂

1) I need to select the rows based on a range of timestamps (minv < timestamp < maxv).

2) I also need to conditionally select rows on a UDF that processes the timestamp

Using Brettski’s table names, would the above translate to:

... (SELECT A0001, A0002, A0003, B0001, B0002, B0003   FROM ptest   WHERE timestamp>minv AND timestamp<maxv AND fn(timestamp)=fnv) p unpivot (val for item in (A0001, A0002, A0003, B0001, B0002, B0003)) as unpvt ... 

Given that I have conditionally add the fn() requirement, I think that I also need to go down the dynamic SQL path as proposed by Jonathon. Especially as I have to build the same query for 12 different tables – all of the same style.

  • 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. 2026-05-10T20:28:05+00:00Added an answer on May 10, 2026 at 8:28 pm

    Same kinda answer here, that was fun:

    -- Get column names from system table DECLARE @phCols NVARCHAR(2000) SELECT @phCols = COALESCE(@phCols + ',[' + name + ']', '[' + name + ']')      FROM syscolumns WHERE id = (select id from sysobjects where name = 'Test' and type='U')  -- Get rid of the column we don't want SELECT @phCols = REPLACE(@phCols, '[Timestamp],', '')  -- Query & sum using the dynamic column names DECLARE @exec nvarchar(2000) SELECT @exec = '     select         SUBSTRING([Value], 2, LEN([Value]) - 1) as [Index],         SUM(CASE WHEN (LEFT([Value], 1) = ''A'') THEN Cols ELSE 0 END) as AValue,          SUM(CASE WHEN (LEFT([Value], 1) = ''B'') THEN Cols ELSE 0 END) as BValue     FROM     (         select *         from (select ' + @phCols + ' from Test) as t         unpivot (Cols FOR [Value] in (' + @phCols + ')) as p     ) _temp     GROUP BY SUBSTRING([Value], 2, LEN([Value]) - 1) ' EXECUTE(@exec) 

    You don’t need to hard code column names in this one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 70k
  • Answers 70k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Store a reference to the window (w in your case)… May 11, 2026 at 1:01 pm
  • added an answer It sounds like you're doing fairly simple document classification. This… May 11, 2026 at 1:01 pm
  • added an answer Yes, this is possible. Depending on which version of Visual… May 11, 2026 at 1:01 pm

Related Questions

I have a data set that is organized in the following manner: Timestamp|A0001|A0002|A0003|A0004|B0001|B0002|B0003|B0004 ...
I have a DataSet that contains a few columns. One of these columns is
I have a set of data that models a hierarchy of categories. A root
I have a set of data that needs to be displayed as a crosstab
I have a set of data protected by 16bit checksums that I need to
I'm a freshman in college going for my computer science degree... I've programmed plenty
How can I best set up my PHP (LAMP) development environment so that I
I have a large set of data (a data cube of 250,000 X 1,000
I'm trying to insert a column into an existing DataSet using C#. As an

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.