开发者

T-SQL select starting from value condition

In SQL server 2008, I have below table.

T-SQL select starting from value condition

Thanks

cr开发者_如何学编程eate table test1 ([Number] int, Item varchar(10))
insert into test1 values (20 , 'Item 1'),(30 , 'Item 2'),(60 , 'Item 3'),(23 , 'Item 4'),(10 , 'Item 5'),(76 , 'Item 6'),(44 , 'Item 7'),(99 , 'Item 8'),(10 , 'Item 9'),(22 , 'Item 10'),(77 , 'Item 11'),(10 , 'Item 12')


Without a specific primary key, there is no inherent order to the data in the table, e.g. imagine each record as a sheet of paper dumped in a basket (in no particular order).

select a.*--, b.pvt
from test1 a
inner join (select MIN(1*substring(item,6,10)) pvt from test1 where number=10) b
    on 1*substring(a.item,6,10) >= b.pvt
order by 1*substring(a.item,6,10)

I have made the following assumptions:

  • The order is by the Item number, where
  • Item number is always the 6th character onwards in the column "Item"

If the assumptions are wrong, then you can still use a similar technique, which is to find the pivotal record and join to it using >=

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜