如题,
stcd z tm c1 ...
01 3 2014 a
02 2 2014 b
01 2 2014 a
03 1 2015 v
01 3 2015 d
想要的结果:
01 3 2015 d
02 2 2014 b
03 1 2015 v
(要求结果的stcd列不重复,取z列最大值的最近的一条记录)
分不多了。。
数据库设计的时候没主键,没索引。。
20 个解决方案
#1
该回复于2015-02-04 15:25:45被管理员删除
#2
with a(stcd,z,tm,c1) as
(
select '01',3,2014,'a' union all
select '02',2,2014,'b' union all
select '01',2,2014,'a' union all
select '03',1,2015,'v' union all
select '01',3,2015,'d'
)
select b.stcd,b.z,b.tm,b.c1 from
(select *,ROW_NUMBER()over(partition by stcd order by tm desc,z desc) as tid
from a) as b where b.tid=1
/*
stcd z tm c1
----------------------
01 3 2015 d
02 2 2014 b
03 1 2015 v
-----------------------
*/
#3
SELECT * FROM(
SELECT ROW_NUMBER()OVER(PARTITION BY stcd ORDER BY z DESC,c1 DESC)RN
,*
FROM tb
)T
WHERE RN=1
#4
declare @table table(stcd int,z int,tm varchar(5),c1 varchar(2))
insert into @table(stcd,z,tm,c1)
select 01,3,'2014','a' union all
select 02,2,'2014','b' union all
select 01,2,'2014','a' union all
select 03,1,'2015','v' union all
select 01,3,'2015','d'
select max(stcd)stcd,max(z)z,max(tm)tm,max(c1)c1 from @table group by stcd
with a(stcd,z,tm,c1) as
(
select '01',3,2014,'a' union all
select '02',2,2014,'b' union all
select '01',2,2014,'a' union all
select '03',1,2015,'v' union all
select '01',3,2015,'d'
)
select b.stcd,b.z,b.tm,b.c1 from
(select *,ROW_NUMBER()over(partition by stcd order by tm desc,z desc) as tid
from a) as b where b.tid=1
/*
stcd z tm c1
----------------------
01 3 2015 d
02 2 2014 b
03 1 2015 v
-----------------------
*/
with table1(stcd,z,tm,c1) as
(
select '01',3,2014,'a' union all
select '02',2,2014,'b' union all
select '01',2,2014,'a' union all
select '03',1,2015,'v' union all
select '01',3,2015,'d'
)
select t1.*
from table1 t1,(select stcd,max(z) z,max(tm) tm from table1 group by stcd) t2
where t1.stcd=t2.stcd and t1.z=t2.z and t1.tm=t2.tm
#11
该回复于2015-02-09 15:51:29被管理员删除
#12
with a(stcd,z,tm,c1) as
(
select '01',3,2014,'a' union all
select '02',2,2014,'b' union all
select '01',2,2014,'a' union all
select '03',1,2015,'v' union all
select '01',3,2015,'d'
)
select b.stcd,b.z,b.tm,b.c1 from
(select *,ROW_NUMBER()over(partition by stcd order by tm desc,z desc) as tid
from a) as b where b.tid=1
/*
stcd z tm c1
----------------------
01 3 2015 d
02 2 2014 b
03 1 2015 v
-----------------------
*/
with table1(stcd,z,tm,c1) as
(
select '01',3,2014,'a' union all
select '02',2,2014,'b' union all
select '01',2,2014,'a' union all
select '03',1,2015,'v' union all
select '01',3,2015,'d'
)
select t1.*
from table1 t1,(select stcd,max(z) z,max(tm) tm from table1 group by stcd) t2
where t1.stcd=t2.stcd and t1.z=t2.z and t1.tm=t2.tm
很遗憾这样是不对的。。max(z),max(tm)只是把最大值选出来而已,没有他俩可能不是同一行的
#13
with a(stcd,z,tm,c1) as
(
select '01',3,2014,'a' union all
select '02',2,2014,'b' union all
select '01',2,2014,'a' union all
select '03',1,2015,'v' union all
select '01',3,2015,'d'
)
select b.stcd,b.z,b.tm,b.c1 from
(select *,ROW_NUMBER()over(partition by stcd order by tm desc,z desc) as tid
from a) as b where b.tid=1
/*
stcd z tm c1
----------------------
01 3 2015 d
02 2 2014 b
03 1 2015 v
-----------------------
*/
with table1(stcd,z,tm,c1) as
(
select '01',3,2014,'a' union all
select '02',2,2014,'b' union all
select '01',2,2014,'a' union all
select '03',1,2015,'v' union all
select '01',3,2015,'d'
)
select t1.*
from table1 t1,(select stcd,max(z) z,max(tm) tm from table1 group by stcd) t2
where t1.stcd=t2.stcd and t1.z=t2.z and t1.tm=t2.tm
很遗憾这样是不对的。。max(z),max(tm)只是把最大值选出来而已,没有他俩可能不是同一行的
他俩可能不是同一行的。。打错
#14
/* 测试数据
with a(stcd,z,tm,c1) as
(
select '01',3,2014,'a' union all
select '02',2,2014,'b' union all
select '01',2,2014,'a' union all
select '01',2,2016,'a' union all --加了一条比最大z还新的数据
select '03',1,2015,'v' union all
select '01',3,2015,'d'
)*/
SELECT *
FROM a
WHERE NOT EXISTS (SELECT *
FROM a t
WHERE t.stcd = a.stcd
AND ( t.z > a.z
OR (t.z = a.z
AND t.tm > a.tm
)
)
)
ORDER BY stcd
stcd z tm c1
---- ----------- ----------- ----
01 3 2015 d
02 2 2014 b
03 1 2015 v
#15
SELECT *
FROM tb T1
WHERE NOT EXISTS(SELECT 1 FROM tb T2 WHERE T1.stcd=T2.stcd
AND T1.z<T2.z OR(T1.z=T2.z AND T1.tm<T2.tm))
ORDER BY stcd
如果根据 z和tm还不能过滤出stcd相同情况的其中一列的话,还需要其它唯一性的列
#16
#15: 你少了个括号,OR 后面部分变成不判断stcd相等了。
#17
谢谢14#、15#
我自己也想了个方法
SELECT T1.STCD,T1.Z,MAX(T2.TM)
FROM table1 AS T2,(SELECT STCD,MAX(Z) Z from table1 group by STCD) AS T1
WHERE T1.STCD=T2.STCD AND T1.Z=T2.Z
GROUP BY T1.STCD,T1.Z
with a(stcd,z,tm,c1) as
(
select '01',3,2014,'a' union all
select '02',2,2014,'b' union all
select '01',2,2014,'a' union all
select '03',1,2015,'v' union all
select '01',3,2015,'d'
)
select b.stcd,b.z,b.tm,b.c1 from
(select *,ROW_NUMBER()over(partition by stcd order by tm desc,z desc) as tid
from a) as b where b.tid=1
/*
stcd z tm c1
----------------------
01 3 2015 d
02 2 2014 b
03 1 2015 v
-----------------------
*/
#3
SELECT * FROM(
SELECT ROW_NUMBER()OVER(PARTITION BY stcd ORDER BY z DESC,c1 DESC)RN
,*
FROM tb
)T
WHERE RN=1
#4
declare @table table(stcd int,z int,tm varchar(5),c1 varchar(2))
insert into @table(stcd,z,tm,c1)
select 01,3,'2014','a' union all
select 02,2,'2014','b' union all
select 01,2,'2014','a' union all
select 03,1,'2015','v' union all
select 01,3,'2015','d'
select max(stcd)stcd,max(z)z,max(tm)tm,max(c1)c1 from @table group by stcd
#5
不能用 max 那种,这个取出来的数据,可能不是同一行的, 2# 3# 的方法是常用的方法。
#6
with a(stcd,z,tm,c1) as
(
select '01',3,2014,'a' union all
select '02',2,2014,'b' union all
select '01',2,2014,'a' union all
select '03',1,2015,'v' union all
select '01',3,2015,'d'
)
select b.stcd,b.z,b.tm,b.c1 from
(select *,ROW_NUMBER()over(partition by stcd order by tm desc,z desc) as tid
from a) as b where b.tid=1
/*
stcd z tm c1
----------------------
01 3 2015 d
02 2 2014 b
03 1 2015 v
-----------------------
*/
#7
with a(stcd,z,tm,c1) as
(
select '01',3,2014,'a' union all
select '02',2,2014,'b' union all
select '01',2,2014,'a' union all
select '03',1,2015,'v' union all
select '01',3,2015,'d'
)
select b.stcd,b.z,b.tm,b.c1 from
(select *,ROW_NUMBER()over(partition by stcd order by tm desc,z desc) as tid
from a) as b where b.tid=1
/*
stcd z tm c1
----------------------
01 3 2015 d
02 2 2014 b
03 1 2015 v
-----------------------
*/
with a(stcd,z,tm,c1) as
(
select '01',3,2014,'a' union all
select '02',2,2014,'b' union all
select '01',2,2014,'a' union all
select '03',1,2015,'v' union all
select '01',3,2015,'d'
)
select b.stcd,b.z,b.tm,b.c1 from
(select *,ROW_NUMBER()over(partition by stcd order by tm desc,z desc) as tid
from a) as b where b.tid=1
/*
stcd z tm c1
----------------------
01 3 2015 d
02 2 2014 b
03 1 2015 v
-----------------------
*/
#9
该回复于2015-02-04 17:22:06被管理员删除
#10
with a(stcd,z,tm,c1) as
(
select '01',3,2014,'a' union all
select '02',2,2014,'b' union all
select '01',2,2014,'a' union all
select '03',1,2015,'v' union all
select '01',3,2015,'d'
)
select b.stcd,b.z,b.tm,b.c1 from
(select *,ROW_NUMBER()over(partition by stcd order by tm desc,z desc) as tid
from a) as b where b.tid=1
/*
stcd z tm c1
----------------------
01 3 2015 d
02 2 2014 b
03 1 2015 v
-----------------------
*/
with table1(stcd,z,tm,c1) as
(
select '01',3,2014,'a' union all
select '02',2,2014,'b' union all
select '01',2,2014,'a' union all
select '03',1,2015,'v' union all
select '01',3,2015,'d'
)
select t1.*
from table1 t1,(select stcd,max(z) z,max(tm) tm from table1 group by stcd) t2
where t1.stcd=t2.stcd and t1.z=t2.z and t1.tm=t2.tm
#11
该回复于2015-02-09 15:51:29被管理员删除
#12
with a(stcd,z,tm,c1) as
(
select '01',3,2014,'a' union all
select '02',2,2014,'b' union all
select '01',2,2014,'a' union all
select '03',1,2015,'v' union all
select '01',3,2015,'d'
)
select b.stcd,b.z,b.tm,b.c1 from
(select *,ROW_NUMBER()over(partition by stcd order by tm desc,z desc) as tid
from a) as b where b.tid=1
/*
stcd z tm c1
----------------------
01 3 2015 d
02 2 2014 b
03 1 2015 v
-----------------------
*/
with table1(stcd,z,tm,c1) as
(
select '01',3,2014,'a' union all
select '02',2,2014,'b' union all
select '01',2,2014,'a' union all
select '03',1,2015,'v' union all
select '01',3,2015,'d'
)
select t1.*
from table1 t1,(select stcd,max(z) z,max(tm) tm from table1 group by stcd) t2
where t1.stcd=t2.stcd and t1.z=t2.z and t1.tm=t2.tm
很遗憾这样是不对的。。max(z),max(tm)只是把最大值选出来而已,没有他俩可能不是同一行的
#13
with a(stcd,z,tm,c1) as
(
select '01',3,2014,'a' union all
select '02',2,2014,'b' union all
select '01',2,2014,'a' union all
select '03',1,2015,'v' union all
select '01',3,2015,'d'
)
select b.stcd,b.z,b.tm,b.c1 from
(select *,ROW_NUMBER()over(partition by stcd order by tm desc,z desc) as tid
from a) as b where b.tid=1
/*
stcd z tm c1
----------------------
01 3 2015 d
02 2 2014 b
03 1 2015 v
-----------------------
*/
with table1(stcd,z,tm,c1) as
(
select '01',3,2014,'a' union all
select '02',2,2014,'b' union all
select '01',2,2014,'a' union all
select '03',1,2015,'v' union all
select '01',3,2015,'d'
)
select t1.*
from table1 t1,(select stcd,max(z) z,max(tm) tm from table1 group by stcd) t2
where t1.stcd=t2.stcd and t1.z=t2.z and t1.tm=t2.tm
很遗憾这样是不对的。。max(z),max(tm)只是把最大值选出来而已,没有他俩可能不是同一行的
他俩可能不是同一行的。。打错
#14
/* 测试数据
with a(stcd,z,tm,c1) as
(
select '01',3,2014,'a' union all
select '02',2,2014,'b' union all
select '01',2,2014,'a' union all
select '01',2,2016,'a' union all --加了一条比最大z还新的数据
select '03',1,2015,'v' union all
select '01',3,2015,'d'
)*/
SELECT *
FROM a
WHERE NOT EXISTS (SELECT *
FROM a t
WHERE t.stcd = a.stcd
AND ( t.z > a.z
OR (t.z = a.z
AND t.tm > a.tm
)
)
)
ORDER BY stcd
stcd z tm c1
---- ----------- ----------- ----
01 3 2015 d
02 2 2014 b
03 1 2015 v
#15
SELECT *
FROM tb T1
WHERE NOT EXISTS(SELECT 1 FROM tb T2 WHERE T1.stcd=T2.stcd
AND T1.z<T2.z OR(T1.z=T2.z AND T1.tm<T2.tm))
ORDER BY stcd
如果根据 z和tm还不能过滤出stcd相同情况的其中一列的话,还需要其它唯一性的列
#16
#15: 你少了个括号,OR 后面部分变成不判断stcd相等了。
#17
谢谢14#、15#
我自己也想了个方法
SELECT T1.STCD,T1.Z,MAX(T2.TM)
FROM table1 AS T2,(SELECT STCD,MAX(Z) Z from table1 group by STCD) AS T1
WHERE T1.STCD=T2.STCD AND T1.Z=T2.Z
GROUP BY T1.STCD,T1.Z