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.
Author |
Topic |
wilkid
Starting Member
1 Post |
Posted - 2010-09-24 : 10:41:59
|
Hi,I'm having some trouble getting a query just the way I need it and am looking for some help.The data in the table XYZ looks like this:Date Status01/01/2010 A02/01/2010 A03/01/2010 A04/01/2010 P04/05/2010 P05/10/2010 P06/07/2010 A07/30/2010 AThe result set that I'm looking to get from my query should look like this:Date Status01/01/2010 A04/01/2010 P06/07/2010 AMy current query looks like:Select min(Date) as Date, StatusFrom XYZGroup by StatusAnd returns the result set:Date Status01/01/2010 A04/01/2010 PBut doesn't return the fact that the status changed back to A on 06/07/2010.Any help will be greatly appreciated.Thanks in advance. |
|
rohitvishwakarma
Posting Yak Master
232 Posts |
Posted - 2010-09-24 : 12:18:40
|
Here is your query:CREATE TABLE #temp_1(id INT IDENTITY(1,1),Date Datetime,Status VARCHAR(50))CREATE TABLE #temp_2(id INT IDENTITY(1,1),Date DATETIME,Status VARCHAR(50),)INSERT INTO #temp_1SELECT Date,Status FROM tbl_XYZINSERT INTO #temp_2SELECT Date,Status FROM tbl_XYZ SELECT * FROM #temp_1 WHERE id NOT IN ( SELECT B.id FROM #temp_1 A INNER JOIN #temp_2 B ON A.id + 1 = B.id AND A.Status = B.Status AND A.Date < B.date ) |
 |
|
namman
Constraint Violating Yak Guru
285 Posts |
Posted - 2010-09-24 : 22:14:16
|
Try this ...declare @temp table(a datetime, b char(1))insert into @temp values('01/01/2010', 'A')insert into @temp values('02/01/2010', 'A')insert into @temp values('03/01/2010', 'A')insert into @temp values('04/01/2010', 'P')insert into @temp values('04/05/2010', 'P')insert into @temp values('05/10/2010', 'P')insert into @temp values('06/07/2010', 'A')insert into @temp values('07/30/2010', 'A')select * from @temp;with temp as (select *, ROW_NUMBER() over(order by a) as rn from @temp)select convert(varchar(10), t2.a,101) as a, t2.b from temp t1 join temp t2 on (t1.b!=t2.b and t1.rn=t2.rn-1) or (t1.rn=1 and t2.rn=1)--result--a b---------- ----01/01/2010 A04/01/2010 P06/07/2010 A |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-09-25 : 00:31:22
|
[code]SELECT t.*FROM YourTable tOUTER APPLY(SELECT TOP 1 Status FROM YourTable WHERE Date < t.Date ORDER BY Date DESC)t1WHERE t1.Status <> t.StatusOR t1.Status IS NULL[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-09-25 : 04:12:03
|
One from meinsert into @temp values('01/01/2010', 'A')insert into @temp values('02/01/2010', 'A')insert into @temp values('03/01/2010', 'A')insert into @temp values('04/01/2010', 'P')insert into @temp values('04/05/2010', 'P')insert into @temp values('05/10/2010', 'P')insert into @temp values('06/07/2010', 'A')insert into @temp values('07/30/2010', 'A')select b,min(a)dt from(select a,b,dense_rank()over(partition by b order by a)-row_number()over(order by a)rid from @temp)t group by b,rid order by min(a) PBUH |
 |
|
|
|
|
|
|