日期:2014-05-17  浏览次数:20392 次

SQL 统计数字差内的数据请问怎么写?
表A
ID   字段A
1    2000
2    5000
3    6000
4    7000

想计算字段A 正负1000以内的统计,然后得出

输出

ID   地段A   相似价格数量  相似ID
1    2000        0            0
2    5000        1            3
3    6000        2            2,4
4    7000        1            3

------解决方案--------------------
select *,
      相似价格数量=(select count(*) from 表A where ID<>t.ID and abs(字段A-t.字段A) between 0 and 1000),
      相似ID=stuff((select ','+rtrim(id) from 表A where ID<>t.ID and abs(字段A-t.字段A) between 0 and 1000 for xml path('')),1,1,'')
from 表A t