用一条查询语句取出price的和最大的连续5条记录
2007-10-08 11:50
507 查看
事情起因是由csdn上的一个问题引起的.(详见http://topic.csdn.net/u/20071006/18/b5c31f64-ff1f-4f55-9caf-f3c680a469c7.html?seed=305126211)
题目3:有一个表 ordertable(price money,id int), 表中有50条记录,其中price中数据是不规则的,要求用一条查询语句取出price的和最大的连续5条记录
例如:
1 10.00
2 5.00
3 6.00
4 8.00
5 1.00
6 20.00
7 60.00
8 4.00
9 3.00
10 2.00
11 80.00
12 120.00
13 1.00
和最大的连续5条记录就是
8 4.00
9 3.00
10 2.00
11 80.00
12 120.00
我见好多人都没把楼上的题意审清楚就搞了N多的解决方法 ,当然此题目对我而言也是有难度的,后来我找的我一位朋友帮我求出.一时没有理解透彻,记一下,以后慢慢理解.
请大家看清楚题目---要求用一条查询语句取出price的和最大的连续5条记录
这个题目应该说是要对sql语句理解到一个层次上才可以.不仅仅是简单的select如斯简单的了.部分答案如下...
SELECT DISTINCT t1.id, t2.id, t3.id, t4.id, t5.id, t1.price+t2.price +t3.price+t4.price +t5.price
FROM [select t1.*,t2.*,t3.*,t4.*,t5.* from ordertable t1,ordertable t2,ordertable t3,ordertable t4 ,ordertable t5,ordertable t6
where t1.id<t2.id and t2.id<t3.id and t3.id<t4.id and t4.id<t5.id
and not exists (select id from ordertable a where (a.id>t1.id and a.id<t2.id ) or (a.id>t2.id and a.id<t3.id)
or(a.id>t3.id and a.id<t4.id) or(a.id>t4.id and a.id<t5.id)
)]. AS p;
下载附件
题目3:有一个表 ordertable(price money,id int), 表中有50条记录,其中price中数据是不规则的,要求用一条查询语句取出price的和最大的连续5条记录
例如:
1 10.00
2 5.00
3 6.00
4 8.00
5 1.00
6 20.00
7 60.00
8 4.00
9 3.00
10 2.00
11 80.00
12 120.00
13 1.00
和最大的连续5条记录就是
8 4.00
9 3.00
10 2.00
11 80.00
12 120.00
我见好多人都没把楼上的题意审清楚就搞了N多的解决方法 ,当然此题目对我而言也是有难度的,后来我找的我一位朋友帮我求出.一时没有理解透彻,记一下,以后慢慢理解.
请大家看清楚题目---要求用一条查询语句取出price的和最大的连续5条记录
这个题目应该说是要对sql语句理解到一个层次上才可以.不仅仅是简单的select如斯简单的了.部分答案如下...
SELECT DISTINCT t1.id, t2.id, t3.id, t4.id, t5.id, t1.price+t2.price +t3.price+t4.price +t5.price
FROM [select t1.*,t2.*,t3.*,t4.*,t5.* from ordertable t1,ordertable t2,ordertable t3,ordertable t4 ,ordertable t5,ordertable t6
where t1.id<t2.id and t2.id<t3.id and t3.id<t4.id and t4.id<t5.id
and not exists (select id from ordertable a where (a.id>t1.id and a.id<t2.id ) or (a.id>t2.id and a.id<t3.id)
or(a.id>t3.id and a.id<t4.id) or(a.id>t4.id and a.id<t5.id)
)]. AS p;
下载附件
相关文章推荐
- 写出一条Sql语句:取出表A中第31到第40记录(SQLServer,以自动增长的ID作为主键,注意:ID可能不是连续的。
- 写出一条Sql语句,取出表A中的第31条到第40条记录。表A以自动增长的ID作为主键。(注意:ID可能不是连续的)
- 写出一条Sql语句:取出表A中第31到第40记录(SQLServer,以自动增长的ID作为主键,注意:ID可能不是连续的。
- [常见面试题]一条Sql语句:取出表A中第31到第40记录,ID可能不是连续的
- 关于SQL语句查询最近联系人聊天记录(即最近一条消息)
- 写一条SQL语句取出第31条到40条记录
- mysql取出表中,某字段值最大的一条纪录,sql语句
- 在mysql 中查询时间最大的一条记录
- Oracle 删除重复数据只留一条 查询及删除重复记录的SQL语句 1、查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断 select * from 表 wher
- mysql查询最后几条、生成一条包含多条记录、插入语句、差值查询
- 在mysql 中查询时间最大的一条记录
- 一条语句中查询数据库表中state字段为不同情况下的记录数
- sql 查询相同记录下日期最大的 一条
- 存储过程查询一张表中记录是否连续、重复并且取出对应的数据
- 关于SQL最大值查询语句的记录
- 查询表内多条记录的最大值一条
- 一条sql语句,查询出全部分类各前10记录
- 一条sql搞定从查询语句中向目标表中插入记录并去重发插入
- 数据库中随机查询一条记录的SQL语句
- 用一条SQL语句取出第 m 条到第 n 条记录的方法