来自:http://www.qqgb.com/DataDB/MSSQL/Sql2000JiChu/DataDB_247314.html
问题:
--记录: aa 5 1 2 4 6 2 2 3 6 7 7 7 1 4 5 1 4 6 --要得到的结果: aa 5 6 7 5 总数 4 也就是: 相邻两条记录之间进行比较,如果前一条记录的值大于下一条记录的值,就随便输出一条记录, 其实真正要得到的结果是:总共有多少条前一条记录的值大于下一条记录的值的记录? |
解决方法:
SQL code
declare @t table(n int) insert @t select 5 union all select 1 union all select 2 union all select 4 union all select 6 union all select 2 union all select 2 union all select 3 union all select 6 union all select 7 union all select 7 union all select 7 union all select 1 union all select 4 union all select 5 union all select 1 union all select 4 union all select 6 select identity(int) id,n into # from @t select * from # a where exists (select 1 from # where id=a.id+1 and n<a.n) drop table #