stacking rows

Hi,

I want to stack my data as follows:

Date      columnA           columnB
8th oct,2007      2           5
9th oct,2007      3          4
10th oct,20074           6

Date       column                Value
8th oct,2007      columnA        2
8th oct,2007      columnB        5
9th oct,2007      columnA  3
9th oct,2007      columnB        4

How can I write a sql query for this?

Thanks
camper12Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Missus Miss_SellaneusCommented:
Create a JOIN that uses SELECTs like these:

a)
SELECT date, "columnA" as column, ColumnA AS value ...........
b)
SELECT date, "columnB" as column, ColumnB AS value ...........

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Éric MoreauSenior .Net ConsultantCommented:
not a JOIN, a UNION

select Date, 'ColumnA', ColumnA AS Value
from YourTableName
where ColumnA is not null
UNION ALL
select Date, 'ColumnB', ColumnB AS Value
from YourTableName
where ColumnB is not null

Open in new window

Vitor MontalvãoMSSQL Senior EngineerCommented:
Don't forget the ORDER BY clause:
select Date, 'ColumnA', ColumnA AS Value
from YourTableName
where ColumnA is not null
UNION ALL
select Date, 'ColumnB', ColumnB AS Value
from YourTableName
where ColumnB is not null
order by 1, 2

Open in new window

Scott PletcherSenior DBACommented:
You can do it in a single pass of the table like this:

SELECT Date,
    CASE which_column WHEN 'A' THEN 'columnA' ELSE 'columnB' END AS [Column],
    CASE which_column WHEN 'A' THEN columnA ELSE columnB END AS value
FROM table_name
CROSS JOIN (
    SELECT 'A' AS which_column UNION ALL
    SELECT 'B'
) AS which_columns
PortletPaulEE Topic AdvisorCommented:
It is also possible to do this with a single pass of the data using CROSS APPLY and VALUES

SELECT
      t.[Date]
    , ca.column
    , ca.value
FROM table_name as t
CROSS APPLY (
      VALUES
                ('columnA', columnA)
              , ('columnB', columnB)
          ) AS ca (column, value)

more examples using that technique here
http://www.experts-exchange.com/articles/12316/Unpivotting-by-CROSS-APPLY-and-VALUES.html
or for much more detail:
http://bradsruminations.blogspot.com.au/2010/02/spotlight-on-unpivot-part-1.html

It is also possible (again with a single pass of the data) to use UNPIVOT but I prefer the method seen above.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.