sql - SQLite select next and previous row based on a where clause -


I want to be able to be able to get the next and previous row using SQLite.

ID Status Date 168 1 2010-01-28 16: 42: 27.167 164 1 2010-01-28 08:52 52: 07.207 163 1 2010-01-28 08:51 51: 20.813 161 1 2010-01-28 07:10 10: 35.373 160 1 2010-01-27 16: 9: 32.550 46 2 2010-01-30 17:13 13:45 4550 145 2 2010-01-30 17:13 13:42 427.00 142 2 2010 For example:

ID 46

strong> I 160 (last one) And 145 (next one) ID

ID 160 I 161 (last one) and 46 (next one) etc.

would like to come back

 from  to @  status ID  and  Expertise is  Stisel test data server is built ... 

  set nocount; Set dateform ymd; Announce @t table (id int, status entry, date sorted datetime) @t in selection 168,1, '2010-01-28 16: 42: 27.167' Select Union 164,1, '2010-01- 28 08:52: 07.207 'Selecting the Association 163.1,' 2010-01-28 08: 51: 20.813 'Selecting the Union 161,1,' 2010-01-28 07:10 10: 35.373 'Select the Union 160.1,' 2010-01- 27 16: 09: 32.550 'Association selection 46,2', 2010-01-30 17: 13: 45.750 'Association selection 145,2,' 2010-01-30 17: 13: 42.607 'Union Choosing 142,2, '2010-01-30 16: 11: 58.020' Union Select 140,2 ', 2010-01-30 15:45:45 00.543'  

SQL Server 2005 Using + will be quite trivial!

Edit:

There is only one test data script but for SQLite which is the focus of the question.

  create a table (id int, statusId int, dateCreated datetime); Insert into T 168,1, '2010-01-28 16: 42: 27.167' Selecting the Association 164,1, '2010-01-28 08: 52: 07.207' Selecting the Union 163,1, '2010-01- 28 08: 51: 20.813 'Select the union 161,1,' 2010-01-28 07: 10: 35.373 'Select the union 160.1,' 2010-01-27 16: 09: 32.550 'Choose Union 46.2,' 2010 -01-30 17: 13: 45.750 'Select Union Union 145,2', 2010-01-30 17: 13: 42.607 'Union 142,2,' 2010-01-30 16: 11: 58.020 Select Union 140, 2, '2010-01-30 15: 45: 00.543'; Please note that the data is not a good example, so I  id 146  to  46  

/ Code>

This problem is more complex than before. Two order fields are controlled separately and then together with a union, get the proper results. To get past and the next, we need another union, so we end up with a union with sub-associations.

It works with the supplied data. I have tested many inputs and I have the previous / next output is correct, while using it, make sure that you change 146 Find all examples.

  SELECT * FROM (SELECT t1. * TROM T1, (SELECT * FROM T WHERE ID = 146) T2 WHERE t1.statusid = t2.statusid and t1.dateCreated & gt; = t2 Select .dateCreated and t1.id> 146 Union Select t1 * from T1, (choose from * FROMT WHERE ID = 146) T2 WHERE t1.statusid & lt; t2.statusid by command t1.statusid DESC, t1 .dateCreated LIMIT 1) Select Union * from (Select t1. * TT1, (Choose from * T WHERE ID = 146) T2 WHERE T 1. StatSet = T 2. STTUSSAD & T1 DUT CUT & Lt ; = T2 Dates Ready and select t1.id> <146 Union Select t1. * From T T1, (SELECT * FROM T WHERE ID = 146) T 2 WHERE t1.statusid & gt; T2.statusid ordered by t1.statusid, t1.dateCreated DESC limit 1) Date by position, Tarikit DESC;  

Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -