2

I'm trying to take a raw data set that adds columns for new data and convert it to a more traditional table structure. The idea is to have the script pull the column name (the date) and put that into a new column and then stack each dates data values on top of each other.

Example

Store     1/1/2013     2/1/2013
XYZ INC   $1000        $2000

To

Store     Date         Value
XYZ INC   1/1/2013     $1000
XYZ INC   2/1/2013     $2000

thanks

1
  • Strictly speaking, it looks simple. If you can show us how the data is stored now (table defs) or show us what you already have, we can show you how to get there. Commented Jan 6, 2014 at 18:25

3 Answers 3

6

There are a few different ways that you can get the result that you want.

You can use a SELECT with UNION ALL:

select store, '1/1/2013' date, [1/1/2013] value
from yourtable
union all
select store, '2/1/2013' date, [2/1/2013] value
from yourtable;

See SQL Fiddle with Demo.

You can use the UNPIVOT function:

select store, date, value
from yourtable
unpivot
(
  value
  for date in ([1/1/2013], [2/1/2013])
) un;

See SQL Fiddle with Demo.

Finally, depending on your version of SQL Server you can use CROSS APPLY:

select store, date, value
from yourtable
cross apply
(
  values
    ('1/1/2013', [1/1/2013]),
    ('2/1/2013', [2/1/2013])
) c (date, value)

See SQL Fiddle with Demo. All versions will give a result of:

|   STORE |     DATE | VALUE |
|---------|----------|-------|
| XYZ INC | 1/1/2013 |  1000 |
| XYZ INC | 2/1/2013 |  2000 |
Sign up to request clarification or add additional context in comments.

Comments

0

Depending on the details of the problem (i.e. source format, number and variability of dates, how often you need to perform the task, etc), it very well may be much easier to use some other language to parse the data and perform either a reformatting function or the direct insert into the final table.

The above said, if you're interested in a completely SQL solution, it sounds like you're looking for some dynamic pivot functionality. The keywords being dynamic SQL and unpivot. The details vary based on what RDBMS you're using and exactly what the specs are on the initial data set.

2 Comments

It is very rare that a pivot or un-pivot would need to be dynamic and there is nothing to suggest a dynamic solution is needed from the question.
@Hogan, you're correct, there is very little detail provided in this question. However, I completely disagree with your assessment that it's rare for a pivot/unpivot to be dynamic. While it might not be needed here, there is a very clear indication that the columns being unpivoted change somewhat frequently. So, unless you like constantly revisiting the code you write, a little dynamic SQL can solve this problem neatly.
-1

I would use a scripting language (Perl, Python, etc.) to generate an INSERT statement for each date column you have in the original data and transpose it into a row keyed by Store and Date. Then run the inserts into your normalized table.

1 Comment

This makes no sense, why use a scripting language when the pivot can be done in SQL?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.