SQL语句积累1:自身查询、区间查询、同号排查
2013-03-20 22:27
591 查看
数据库表:t_persons
select * from t_persons
ID name date money
1 李天 2012-01-01 00:00:00.000 100
2 李忠 2012-02-01 00:00:00.000 200
3 李天 2012-01-01 00:00:00.000 100
4 李忠 2012-02-02 00:00:00.000 500
5 李敏 2011-01-01 00:00:00.000 600
6 李吧 2011-02-03 00:00:00.000 1000
1.
/*查询相同名字的最大时间的记录*/
select * from t_persons a where a.date=(select max(date) from t_persons b where b.name=a.name)
2.
/*区间数据: 前第4-5条记录*/
select top 2 * from( select top 5 * from t_persons order by ID asc ) a order by ID desc
select top 2 * from t_persons where ID not in (select top 3 ID from t_persons)
扩展:
以查询前20到30条为例,主键名为id
方法一: 先正查,再反查
select top 10 * from (select top 30 * from tablename order by id asc) A order by id desc
方法二: 使用left join
select top 10 A.* from tablename A
left outer join (select top 20 * from tablename order by id asc) B
on A.id = B.id
where B.id is null
order by A.id asc
方法三: 使用not exists
select top 10 * from tablename A
where id not exists
(select top 20 * from tablename B on A.id = B.id)
方法四: 使用not in
select top 10 * from tablename
where id not in
(select top 20 id from tablename order by id asc)
order by id asc
方法五: 使用rank()
select id from
(select rank() over(order by id asc) rk, id from tablename) T
where rk between 20 and 30
中第五种方法看上去好像没有问题,查了下文档,当over()用于rank/row_number时,整型列不能描述一个列,所以会产生非预期的效果. 待考虑下,有什么办法可以修改为想要的结果.
3.删除重复的数据,只留一条
select * from t_persons where ID=
(select max(ID) from t_persons where
name =(select name from t_persons group by name,date,money having count(*)>1)
and date=(select date from t_persons group by name,date,money having count(*)>1)
and money=(select money from t_persons group by name,date,money having count(*)>1)
)
select * from t_persons
ID name date money
1 李天 2012-01-01 00:00:00.000 100
2 李忠 2012-02-01 00:00:00.000 200
3 李天 2012-01-01 00:00:00.000 100
4 李忠 2012-02-02 00:00:00.000 500
5 李敏 2011-01-01 00:00:00.000 600
6 李吧 2011-02-03 00:00:00.000 1000
1.
/*查询相同名字的最大时间的记录*/
select * from t_persons a where a.date=(select max(date) from t_persons b where b.name=a.name)
2.
/*区间数据: 前第4-5条记录*/
select top 2 * from( select top 5 * from t_persons order by ID asc ) a order by ID desc
select top 2 * from t_persons where ID not in (select top 3 ID from t_persons)
扩展:
以查询前20到30条为例,主键名为id
方法一: 先正查,再反查
select top 10 * from (select top 30 * from tablename order by id asc) A order by id desc
方法二: 使用left join
select top 10 A.* from tablename A
left outer join (select top 20 * from tablename order by id asc) B
on A.id = B.id
where B.id is null
order by A.id asc
方法三: 使用not exists
select top 10 * from tablename A
where id not exists
(select top 20 * from tablename B on A.id = B.id)
方法四: 使用not in
select top 10 * from tablename
where id not in
(select top 20 id from tablename order by id asc)
order by id asc
方法五: 使用rank()
select id from
(select rank() over(order by id asc) rk, id from tablename) T
where rk between 20 and 30
中第五种方法看上去好像没有问题,查了下文档,当over()用于rank/row_number时,整型列不能描述一个列,所以会产生非预期的效果. 待考虑下,有什么办法可以修改为想要的结果.
3.删除重复的数据,只留一条
select * from t_persons where ID=
(select max(ID) from t_persons where
name =(select name from t_persons group by name,date,money having count(*)>1)
and date=(select date from t_persons group by name,date,money having count(*)>1)
and money=(select money from t_persons group by name,date,money having count(*)>1)
)
相关文章推荐
- SQL 查询语句积累
- SQL 查询语句积累
- 查询ip区间的ip地址的sql语句
- SQL 一些有用的查询语句积累
- SQL 语句 知识积累----------子查询实例
- 营配数据质量核查,关于营销mis系统与配电gis系统里面的sql语句查询,做为积累使用,下次就不用重复写同样的语句了。
- SQL查询语句积累
- 【SQL积累】查询成绩语句,带总分
- MySql常用30种SQL查询语句优化方法
- Questions:连接两个表进行查询:可以输入或不输入查询条件的sql 语句
- sql查询语句的优化,exists与in的更换
- sql语句实现查询实例
- sql 语句查询大全
- PHP获取MySQL执行sql语句的查询时间
- SQL语句:orac 3ff0 le中如何插入Date类型的数据和根据Date数据进行查询的方法
- oracle中查询和定位数据库问题的SQL语句
- Hibernate使用sql语句查询
- SQL查询语句
- SQL语句多表连接查询优化
- ThinkPHP 查询生成的sql语句