insert into #a(code,dt1) values(1001,'2013-07-02 08:51:18')
insert into #a(code,dt1) values(1001,'2013-07-02 08:52:18')
insert into #a(code,dt1) values(1001,'2013-07-02 08:53:18')
insert into #a(code,dt1) values(1001,'2013-07-02 08:55:18')
insert into #a(code,dt1) values(1001,'2013-07-02 08:57:18')
insert into #a(code,dt1) values(1001,'2013-07-02 08:58:18')
insert into #a(code,dt1) values(1002,'2013-07-02 08:31:18')
insert into #a(code,dt1) values(1002,'2013-07-02 08:33:18')
insert into #a(code,dt1) values(1002,'2013-07-02 08:45:18')
insert into #a(code,dt1) values(1002,'2013-07-02 08:47:18')
insert into #a(code,dt1) values(1002,'2013-07-02 08:56:18')
select * from #a
可以看到 1001 有6条数据,1002 有5条数据
我现在想 根据code分类,把 时间最小,时间最大,时间第二大 的三条数据查询出来
最后得到的结果为
code dt1
1001 2013-07-02 08:51:18.000
1001 2013-07-02 08:57:18.000
1001 2013-07-02 08:58:18.000
1002 2013-07-02 08:31:18.000
1002 2013-07-02 08:47:18.000
1002 2013-07-02 08:56:18.000
5 个解决方案
#1
select row_number() over (partition by code order by dt1 desc) as id ,* from tablename
#2
如果是低版本sql
SELECT t1.id,t1.date,COUNT(*) AS seq
FROM @t t1
INNER JOIN @t t2 ON t2.date <=t1.date AND t1.[Id]=t2.[Id]
GROUP BY t1.id,t1.date
ORDER BY t1.id,t1.date;
#3
这只是按时间排序了,还不能达到我的要求
#4
create table #a(code varchar(50),dt1 datetime)
insert into #a(code,dt1) values(1001,'2013-07-02 08:51:18')
insert into #a(code,dt1) values(1001,'2013-07-02 08:52:18')
insert into #a(code,dt1) values(1001,'2013-07-02 08:53:18')
insert into #a(code,dt1) values(1001,'2013-07-02 08:55:18')
insert into #a(code,dt1) values(1001,'2013-07-02 08:57:18')
insert into #a(code,dt1) values(1001,'2013-07-02 08:58:18')
insert into #a(code,dt1) values(1002,'2013-07-02 08:31:18')
insert into #a(code,dt1) values(1002,'2013-07-02 08:33:18')
insert into #a(code,dt1) values(1002,'2013-07-02 08:45:18')
insert into #a(code,dt1) values(1002,'2013-07-02 08:47:18')
insert into #a(code,dt1) values(1002,'2013-07-02 08:56:18')
SELECT * FROM
(select row_number() over (partition by code order by dt1 desc) as id ,* from #a) t
WHERE id=1 OR id=2 OR id=(SELECT count(1) FROM #a b WHERE b.code=t.code)
/*
id code dt1
-------------------- -------------------------------------------------- -----------------------
1 1001 2013-07-02 08:58:18.000
2 1001 2013-07-02 08:57:18.000
6 1001 2013-07-02 08:51:18.000
1 1002 2013-07-02 08:56:18.000
2 1002 2013-07-02 08:47:18.000
5 1002 2013-07-02 08:31:18.000
*/
drop table #a
#5
select
*
from
(
select
rank() over(partition by code order by dt1 desc) as id
, count(1) over(partition by code) as code_cnt
, *
from
tbl_test_kin
) main
where
id in (1, 2, code_cnt)
*
from
(
select
rank() over(partition by code order by dt1 desc) as id
, count(1) over(partition by code) as code_cnt
, *
from
tbl_test_kin
) main
where
id in (1, 2, code_cnt)
#1
select row_number() over (partition by code order by dt1 desc) as id ,* from tablename
#2
如果是低版本sql
SELECT t1.id,t1.date,COUNT(*) AS seq
FROM @t t1
INNER JOIN @t t2 ON t2.date <=t1.date AND t1.[Id]=t2.[Id]
GROUP BY t1.id,t1.date
ORDER BY t1.id,t1.date;
#3
这只是按时间排序了,还不能达到我的要求
#4
create table #a(code varchar(50),dt1 datetime)
insert into #a(code,dt1) values(1001,'2013-07-02 08:51:18')
insert into #a(code,dt1) values(1001,'2013-07-02 08:52:18')
insert into #a(code,dt1) values(1001,'2013-07-02 08:53:18')
insert into #a(code,dt1) values(1001,'2013-07-02 08:55:18')
insert into #a(code,dt1) values(1001,'2013-07-02 08:57:18')
insert into #a(code,dt1) values(1001,'2013-07-02 08:58:18')
insert into #a(code,dt1) values(1002,'2013-07-02 08:31:18')
insert into #a(code,dt1) values(1002,'2013-07-02 08:33:18')
insert into #a(code,dt1) values(1002,'2013-07-02 08:45:18')
insert into #a(code,dt1) values(1002,'2013-07-02 08:47:18')
insert into #a(code,dt1) values(1002,'2013-07-02 08:56:18')
SELECT * FROM
(select row_number() over (partition by code order by dt1 desc) as id ,* from #a) t
WHERE id=1 OR id=2 OR id=(SELECT count(1) FROM #a b WHERE b.code=t.code)
/*
id code dt1
-------------------- -------------------------------------------------- -----------------------
1 1001 2013-07-02 08:58:18.000
2 1001 2013-07-02 08:57:18.000
6 1001 2013-07-02 08:51:18.000
1 1002 2013-07-02 08:56:18.000
2 1002 2013-07-02 08:47:18.000
5 1002 2013-07-02 08:31:18.000
*/
drop table #a
#5
select
*
from
(
select
rank() over(partition by code order by dt1 desc) as id
, count(1) over(partition by code) as code_cnt
, *
from
tbl_test_kin
) main
where
id in (1, 2, code_cnt)
*
from
(
select
rank() over(partition by code order by dt1 desc) as id
, count(1) over(partition by code) as code_cnt
, *
from
tbl_test_kin
) main
where
id in (1, 2, code_cnt)