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 2005 Forums
 Transact-SQL (2005)
 Select Query help

Author  Topic 

srujanavinnakota
Starting Member

34 Posts

Posted - 2011-03-23 : 03:36:19
Please help me with select query,
I have data in the following format.
Id Amt
1 18
2 -14
3 1
4 1
5 43
6 7.
7 163
8 319
9 475
10 359
11 160
12 612
13 -382
14 -223
15 -243

I need select the Id where amt are negative and the previous amounts before them are positive.
In the above data, I need select the following.
Id Amt
2 -14
13 -382



khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-03-23 : 04:18:22
[code]
select *
from yourtable t
where t.Amt < 0
and exists
(
select *
from yourtable x
where x.Id = t.Id - 1
and x.At > 0
)
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

srujanavinnakota
Starting Member

34 Posts

Posted - 2011-03-23 : 17:30:16
Thank you so much..
It worked.
Go to Top of Page
   

- Advertisement -