EXCEL-SQL-group by计数
2017-07-20 本文已影响53人
叶知行
数据
数据求属性1和属性2的差值(绝对值)小于300的数据且属性1的计数大于等于2。
select a.*,abs(a.属性1-a.属性2) as 差
from [Sheet1$a1:c] a,
(
select 属性1 from [Sheet1$a1:c]
group by 属性1 having count(属性1)>1
) b
where a.属性1=b.属性1
and
abs(a.属性1-a.属性2) <300
解释:
- 1、先求出大于等于2的属性1有那些数据,用group by having count
select 属性1 from [Sheet1$a1:c]
group by 属性1 having count(属性1)>1
- 2、然后将原表和上表用where做筛选,筛选出属性1计数大于1的数据,在筛选的时候,新增一个‘差值’字段计算属性1和属性2的差,同样作为筛选的条件。这样即可筛选出结果集。条件:属性1计数大于1的且属性1和属性2差值的绝对值小于300的。
如上面代码
select a.*,abs(a.属性1-a.属性2) as 差
from [Sheet1$a1:c] a,
(
select 属性1 from [Sheet1$a1:c]
group by 属性1 having count(属性1)>1
) b
where a.属性1=b.属性1 --//条件1
and
abs(a.属性1-a.属性2) <300 --//条件2
结果
示例文件下载
链接: http://pan.baidu.com/s/1c1FnUwg 密码: seq8