SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Move columns into rows for same items
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

maratasya
Starting Member

USA
3 Posts

Posted - 08/28/2014 :  13:08:24  Show Profile  Reply with Quote
I have a table in the following format:

Matter ID Index Description
00103-00048 6 Litigation
00103-00048 57 Trial
00245-00015 6 Deposition
00245-00015 57 Hearing

I need each matter in a single row with descriptions as columns:

Matter ID Description_6 Description_57
00103-00048 Litigation Trial
00245-00015 Deposition Hearing

Any help appreciated!!

Chris_Kelley
Posting Yak Master

112 Posts

Posted - 08/28/2014 :  23:13:09  Show Profile  Reply with Quote
----------------------------------------
CREATE TABLE #MARATASYA
(
MATTER_ID VARCHAR(100),
Index_ NVARCHAR(100),
description NVARCHAR(100)
)
----------------------------------------
INSERT INTO #MARATASYA
VALUES
('00103-00048', '6', 'Litigation'),
('00103-00048', '57', 'Trial'),
('00245-00015', '6','Deposition'),
('00245-00015', '57', 'Hearing')
-----------------------------------------------
SELECT * FROM #MARATASYA

RESULTS--

MATTER_ID Index_ description
00103-00048 6 Litigation
00103-00048 57 Trial
00245-00015 6 Deposition
00245-00015 57 Hearing


-------------------------------------------
SELECT TOP 1 (MATTER_ID) AS MATTER_ID,
(SELECT description FROM #MARATASYA WHERE Index_ =6 AND MATTER_ID = '00103-00048') AS DESCRIPTION1,
(SELECT description FROM #MARATASYA WHERE Index_ =57 AND MATTER_ID = '00103-00048') AS DESCRIPTION2
FROM #MARATASYA WHERE MATTER_ID = '00103-00048'


RESULTS --

MATTER_ID DESCRIPTION1 DESCRIPTION2
00103-00048 Litigation Trial

Thanks,
Chris
Jr Programmer
Go to Top of Page

maratasya
Starting Member

USA
3 Posts

Posted - 08/29/2014 :  20:08:29  Show Profile  Reply with Quote
Thank you, Chris!

My table includes many matters and various descriptions... I don't think I can write an insert statement for every possible case...
Go to Top of Page

bitsmed
Constraint Violating Yak Guru

413 Posts

Posted - 08/29/2014 :  20:56:04  Show Profile  Reply with Quote
How about this:
select [Matter ID]
      ,max(case when [Index]=6 then [Description] else null end) as Description_6
      ,max(case when [Index]=57 then [Description] else null end) as Description_57
  from yourtable
 group by [Matter ID]
Go to Top of Page

maratasya
Starting Member

USA
3 Posts

Posted - 09/02/2014 :  12:30:59  Show Profile  Reply with Quote
This seems to be working!

Thank you bitsmed!!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000