SQL Server Transpose , partially at least

Hi, i am trying to create a SQL Sp that will output some data, but it needs to do a partial transpose.

the current data is in rows, and looks like this. Lets call this Table CURRDATA
ID 1             ID2           TYPE        RATE   END_DATE
ABC123      786efr      XXX         5.5       12/31/2019
XXYYZZ       AABBCC   YYY         6.9        6/30/2020

etc... they key for the table is the combination of ID1 and ID 2

I need to re-arrange this output for import to another system, and it needs to be somewhat transposed. The output should look like this, with a line for each value outside the key.
ID1               ID2                Property                    Value
ABC123      786efr            TYPE                           XXX
ABC123      786efr            RATE                           5.5
ABC123      786efr            END_DATE                 12/31/2019
XXYYZZ       AABBCC         TYPE                           YYY
XXYYZZ       AABBCC         RATE                           6.9
XXYYZZ       AABBCC          END_DATE                6/30/2020

Can someone help me write this SQL to get the desired output?
Who is Participating?
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.

Scott PletcherSenior DBACommented:
SELECT ID1, ID2, ca.Property, ca.Value
FROM table_name
    VALUES('Type', TYPE),('Rate',RATE),('End_Date',END_DATE)
) AS ca(Property, Value)

--This syntax assumes at least SQL 2008.
--If you're on 2005, the CROSS APPLY will need the code adjusted, but the basic approach still works.
Mike EghtebasDatabase and Application DeveloperCommented:

To learn from your good solution, I made #t to test it. I found that we need to do some conversion like:
SELECT ID1, ID2, ca.Property, ca.Value
    VALUES('Type', TYPE),('Rate',cast(RATE as varchar(10))),('End_Date',cast(END_DATE as varchar(10)))
) AS ca(Property, Value)

Open in new window

temp table:
create table #t(ID1 varchar(10), ID2 varchar(10), [TYPE] varchar(10), RATE decimal(6,2), END_DATE date);
insert into #t(ID1, ID2, [TYPE], RATE, END_DATE) values
('ABC123', '786efr', 'XXX', 5.5, '12/31/2019')
, ('XXYYZZ', 'AABBCC', 'YYY', 6.9, '6/30/2020');

Open in new window


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
Vitor MontalvãoMSSQL Senior EngineerCommented:
mikegrad7, do you still need help with this question?
mikegrad7Author Commented:
thanks to everyone for you help, i was able to get this working as i needed.
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.