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
 Pivoting Problem
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ransomsreason
Starting Member

USA
2 Posts

Posted - 04/03/2013 :  18:24:05  Show Profile  Reply with Quote
Hi, so I'm working with data on mental health patients and I need to compare their scores on an assessment at their admit and their discharge from a program. Some of them have been admitted and discharged multiple times and I'd need a comparison for each time. My data currently looks like this, with a new row for each assessment:



And I'd like it to look something like this afterwards:


I can imagine it's very complicated to get it exactly how I want it, but any solutions or even pointers in the right direction would be much appreciated! Thanks!

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 04/04/2013 :  01:09:23  Show Profile  Reply with Quote

WITH PersonDetails
AS
(
SELECT *,ROW_NUMBER() OVER (PARTITION BY Person,TestInterval ORDER BY Date) AS Seq
FROM Table
)
SELECT pd.Person,pd.Score AS BeforeScore,pd.[Date] AS BeforeDate,pd1.Score AS AfterScore, pd1.[Date] AS AfterDate
FROM PersonDetails pd
LEFT JOIN PersonDetails pd1
ON pd1.Person = pd.Person
AND pd1.TestInterval = pd.TestInterval
AND pd1.Seq = pd.Seq + 1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2215 Posts

Posted - 04/04/2013 :  03:03:19  Show Profile  Reply with Quote

DECLARE @tab TABLE (Person varchaR(10), Date DATE, TestInterval varchaR(15), Score INT)
INSERT INTO @tab
SELECT 'Larry', '3/11/2012', 'Admit', 12 union all
SELECT 'Larry', '3/12/2012', 'Discharge', 16 union all
SELECT 'Larry', '3/13/2012', 'Admit', 14 union all
SELECT 'Larry', '3/14/2012', 'Discharge', 18 union all
SELECT 'Sushan', '3/15/2012', 'Admit', 21 union all
SELECT 'Sushan', '3/16/2012', 'Discharge', 26 union all
SELECT 'Kevin', '3/17/2012', 'Admit', 16 union all
SELECT 'Kevin', '3/18/2012', 'Discharge', 19 union all
SELECT 'Kevin', '3/19/2012', 'Admit', 17
;WITH CTE AS(SELECT *,ROW_NUMBER() OVER (PARTITION BY Person, testInterval ORDER BY Date) AS Seq FROM @tab)
SELECT Person,
	MAX(case when TestInterval = 'Admit' then DATE END) as BeforeDate, 
	MAX(case when TestInterval = 'Discharge' then DATE END) as AfterDate,
	MAX(case when TestInterval = 'Admit' then Score END) as BeforeScore, 
	MAX(case when TestInterval = 'Discharge' then Score END) as AfterScore
FROM CTE
GROUP BY Person, Seq
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