Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 getting Previous & New Values in same row

Author  Topic 

hardikspider123
Starting Member

12 Posts

Posted - 2014-05-16 : 14:27:55

- I want previous value and new value in same row. Index can be multiple but label is always unique for that index.

--see 124515 index. Need help immediately. Appreciate your quick response.



TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2014-05-16 : 16:10:11
If you provide your source data as executable DDL/DML I'll provide a solution. But I don't feel like typing all that myself.
Otherwise here's one way to do it:
- create a CTE so that you can break out Label and Value into different columns (as they should be anyway)
- self join two instances of your table with a left outer join.
- join on index and label with one table instance type = 'P' and the other instance type = 'N'
- then just select what you want out of two instances of your table.

Be One with the Optimizer
TG
Go to Top of Page

hardikspider123
Starting Member

12 Posts

Posted - 2014-05-16 : 16:24:58
quote:
Originally posted by TG

If you provide your source data as executable DDL/DML I'll provide a solution. But I don't feel like typing all that myself.
Otherwise here's one way to do it:
- create a CTE so that you can break out Label and Value into different columns (as they should be anyway)
- self join two instances of your table with a left outer join.
- join on index and label with one table instance type = 'P' and the other instance type = 'N'
- then just select what you want out of two instances of your table.

Be One with the Optimizer
TG



Hi,

This is what I have written


Select dateupdated, LEFT(rtrim(ltrim(label)), CHARINDEX(' ', ltrim(label) + ' ') -1) as Userdefinedlabel,
isnull(PrevValue,'')as PrevValue, isnull(NewValue,'')as Newvalue, table1.Aindex
From table1

left join (select Aindex, Rtrim(Ltrim(SUBSTRING(label, CHARINDEX(' ', ltrim(label) + ' '), LEN(label)))) as PrevValue,
LEFT(rtrim(ltrim(label)), CHARINDEX(' ', label + ' ') -1) as PrevDesc,
ltrim(rtrim(label)) as pdesc from table1 where Atype = 'P')
as aup on aup.Aindex = table1.Aindex and aup.label = table1.label

left join (select Aindex, Rtrim(Ltrim(SUBSTRING(label, CHARINDEX(' ', ltrim(label) + ' '), LEN(label)))) as NewValue,
LEFT(rtrim(ltrim(label)), CHARINDEX(' ', label + ' ') -1) as NewDesc,
ltrim(rtrim(label)) as pdesc from table1 where Atype = 'N')
as aun on aun.Aindex = table1.Aindex and aun.label = table1.label
where keyvalue2 = '029125'

order by dateupdated, table1.Aindex

which gets me following result. which is not the desired result. Previous & New values for Same label comes in different rows. It should be in same row for same label.




Thanks.

Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2014-05-16 : 17:26:34
quote:
If you provide your source data as executable DDL/DML I'll provide a solution. But I don't feel like typing all that myself.
Thanks for nothing!

;with yourTable ([index], dateupdated, label, [type])
as
(
select 124512, '2013-05-10', 'PA 1', 'P' union all
select 124512, '2013-05-10', 'PA 2', 'N' union all
select 124513, '2013-05-11', 'NJ 10', 'N' union all
select 124514, '2013-05-12', 'NY 5', 'P' union all
select 124515, '2013-05-15', 'MD 20', 'P' union all
select 124515, '2013-05-15', 'MD 21', 'N' union all
select 124515, '2013-05-15', 'CT 22', 'N'
)

,cte ([index], dateupdated, label, value, [type])
as
(
select [index]
, dateupdated
, left([Label], charindex(' ', Label)-1)
, substring([Label], charindex(' ', [Label])+1, 10)
, [Type]
from yourTable
)
select coalesce(yt1.[index], yt2.[index]) [index]
, max(coalesce(yt1.dateupdated, yt2.dateupdated)) dateupdated
, coalesce(yt1.label, yt2.Label) as Label
, max(yt1.value) as PValue
, max(yt2.value) as NValue
from cte yt1
full outer join cte yt2
on yt2.[index] = yt1.[index]
and yt2.[label] = yt1.[label]
and yt2.[type] = 'N'
and yt1.[type] = 'P'
where (yt2.[type] = 'N' or yt2.[index] is null)
and (yt1.[type] = 'P' or yt1.[index] is null)

group by coalesce(yt1.[index], yt2.[index])
, coalesce(yt1.label, yt2.Label)

order by 1,2, 3 desc

OUTPUT:

index dateupdated Label PValue NValue
----------- ----------- ----- ------ ------
124512 2013-05-10 PA 1 2
124513 2013-05-11 NJ NULL 10
124514 2013-05-12 NY 5 NULL
124515 2013-05-15 MD 20 21
124515 2013-05-15 CT NULL 22



Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -