数量 位置 时间
2 abc 2010-5-1
1 abc 2010-5-1
1 abc 2010-5-2
2 efg 2010-5-2
2 efg 2010-5-3期望结果
每个位置只出一条记录,且是数量最小的,并且是时间最久的。数量 位置 时间
1 abc 2010-5-1
2 efg 2010-5-2
2 abc 2010-5-1
1 abc 2010-5-1
1 abc 2010-5-2
2 efg 2010-5-2
2 efg 2010-5-3期望结果
每个位置只出一条记录,且是数量最小的,并且是时间最久的。数量 位置 时间
1 abc 2010-5-1
2 efg 2010-5-2
from TB A
where not exists(select 1 from TB where A.数量>数量 and A.时间>时间 and A.位置 = 位置)
select * from 表名 a
where not exists(select 1 from 表名 where 位置 =a.位置 and 数量<a.数量 and 时间<a.时间 )
select * from(
select *
from tb t
where not exists(
select 1 from tb where t.数量>=数量 and t.位置 = 位置)
) t
where not exists(
select 1 from (
select *
from tb t
where not exists(
select 1 from tb where t.数量>=数量 and t.位置 = 位置)
) p where p.时间>t.时间 and t.位置 = p.位置)
from TB
group by [位置]/*
数量 位置 时间
----------- ---- -----------------------
1 abc 2010-05-01 00:00:00.000
2 efg 2010-05-02 00:00:00.000(2 行受影响)
*/
搞复杂了。呵呵。
谢谢你。
这个看起来正确,后来详细试了一下,还是有问题,查询出的结果不是一条记录,而可能是由不同记录的字段组合而成。如:
数量 位置 时间
2 abc 2010-5-1
1 abc 2010-5-2 //修改
1 abc 2010-5-2
2 efg 2010-5-2
2 efg 2010-5-3数量 位置 时间
1 abc 2010-5-1 //这里期望是2010-5-2
2 efg 2010-5-2
select 2 ,'abc' ,'2010-5-1' union all
select 1 ,'abc' ,'2010-5-1' union all
select 1 ,'abc' ,'2010-5-2' union all
select 2 ,'efg' ,'2010-5-2' union all
select 2 ,'efg' ,'2010-5-3'select * from(
select *
from tb t
where not exists(
select 1 from tb where t.数量>数量 and t.位置 = 位置)
) t
where not exists(
select 1 from (
select *
from tb t
where not exists(
select 1 from tb where t.数量>数量 and t.位置 = 位置)
) p where p.时间>t.时间 and t.位置 = p.位置)/*
数量 位置 时间
----------- ---------- ------------------------------------------------------
1 abc 2010-05-02 00:00:00.000
2 efg 2010-05-03 00:00:00.000(所影响的行数为 2 行)*/
insert @a select 2,'abc' ,'2010-5-1'
union all select 1,'abc', '2010-5-2'
union all select 1,'abc' ,'2010-5-10'
union all select 2,'efg' ,'2010-5-2'
union all select 2,'efg' ,'2010-5-3'
SELECT *
FROM (SELECT * FROM @a a WHERE a = (SELECT MIN(a) FROM @a WHERE b = a.b)
) aa
WHERE c = (
SELECT MAX(c)
FROM @a
WHERE b = aa.b
)--result
/*
a b c
----------- -------------------- ------------------------------
1 abc 2010-05-10 00:00:00
2 efg 2010-05-03 00:00:00(所影响的行数为 2 行)*/
select 2 ,'abc' ,'2010-5-1' union all
select 1 ,'abc' ,'2010-5-2' union all
select 1 ,'abc' ,'2010-5-2' union all
select 2 ,'efg' ,'2010-5-2' union all
select 2 ,'efg' ,'2010-5-3'select distinct * from(
select *
from tb t
where not exists(
select 1 from tb where t.数量>数量 and t.位置 = 位置)
) t
where not exists(
select 1 from (
select *
from tb t
where not exists(
select 1 from tb where t.数量>数量 and t.位置 = 位置)
) p where p.时间<t.时间 and t.位置 = p.位置)/*
数量 位置 时间
----------- ---------- ------------------------------------------------------
1 abc 2010-05-02 00:00:00.000
2 efg 2010-05-02 00:00:00.000(所影响的行数为 2 行)
*/
insert @a select 2,'abc' ,'2010-5-1'
union all select 1,'abc', '2010-5-2'
union all select 1,'abc' ,'2010-5-4'
union all select 2,'efg' ,'2010-5-2'
union all select 2,'efg' ,'2010-5-3'
Select * From
(Select * From @a a Where not exists (Select * From @a Where sl<a.sl And wz=a.wz))b
Where not exists (Select * From (Select * From @a a
Where not exists (Select * From @a Where sl<a.sl And wz=a.wz)) t Where sl=b.sl and wz=b.wz and rq<b.rq)
/*
sl wz rq
----------- -------------------- -----------------------
1 abc 2010-05-02 00:00:00
2 efg 2010-05-02 00:00:00(2 行受影响)
*/
create table tb(数量 int,位置 varchar(10), 时间 datetime)insert tb
select 2 ,'abc' ,'2010-5-1' union all
select 1 ,'abc' ,'2010-5-2' union all
select 1 ,'abc' ,'2010-5-2' union all
select 2 ,'efg' ,'2010-5-2' union all
select 2 ,'efg' ,'2010-5-3'
select 数量,位置,min(时间) from
(select * from tb where exists( select 1 from
(select min(数量)as 数量 , 位置 from tb group by 位置) a where a. 数量 =tb .数量 and a.位置=tb.位置)
)b group by b.数量 , b.位置/* 数量 位置 时间 ----------- ---------- ------------------------------------------------------ 1 abc 2010-05-02 00:00:00.000 2 efg 2010-05-02 00:00:00.000 (所影响的行数为 2 行) */
select distinct 数量,位置,时间
from A
where 数量<=min(数量)and 时间>=max(时间)
group by 位置
FROM (
SELECT *,
SN = ROW_NUMBER() OVER(PARTITION BY 位置 ORDER BY 数量, 时间)
FROM [表]
) tmp
WHERE SN = 1
from TABLE1 A
where A.时间 in
(select distinct max(时间)
from TABLE1 B
where A.位置=B.位置
group by 位置)
and A.数量 in
(select distinct min(数量)
from TABLE1 C
where A.位置=C.位置
group by 位置)这是代码,我在SQL2000上运行通过了。它的功能就是把相同位置上数量是最少并且时间是最大的,就查询出来。但如果相同位置上数量是最少,但时间不最大的记录是查询不出来的。同样如果相同位置上时间是最大,但数量不是最少,也无法查询出来。
drop table dbo.tb1CREATE TABLE dbo.tb1
(
quantity int,
position varchar(10),
ctime char(10)
)INSERT dbo.tb1
SELECT 2, 'abc', '2010-4-29' UNION ALL
SELECT 1, 'abc', '2010-5-1' UNION ALL
SELECT 1, 'abc', '2010-5-2' UNION ALL
SELECT 2, 'efg', '2010-5-2' UNION ALL
SELECT 2, 'efg', '2010-5-3' UNION ALL
SELECT 3, 'efg', '2010-4-1'
select quantity, position, min(ctime) ctime from
(select a.quantity, a.position, b.ctime from
(select min(quantity) quantity,position from tb1 group by position) a join
tb1 b on a.quantity=b.quantity and a.position=b.position) c group by quantity, position/*
quantity position ctime
--------- ---------- ---------
1 abc 2010-5-1
2 efg 2010-5-2*/
数量 位置 时间
2 abc 2010-5-1
1 abc 2010-5-2
1 abc 2010-5-3
2 efg 2010-5-2
2 efg 2010-5-3数量 位置 时间
1 abc 2010-5-2
2 efg 2010-5-2楼上有几位的方法好像可以,我在验证一下。不过你也可以按照你的思路写SQL,互相学习,开阔思路嘛。
ROW_NUMBER是什么?怎么提示:'ROW_NUMBER' 不是可以识别的 函数名。
(numbers int,
weizhi nvarchar(20),
times datetime)drop table tbinsert into tb
select '2' ,'abc', '2010-5-1' union
select '1', 'abc', '2010-5-1' union
select '1', 'abc', '2010-5-2' union
select '2' ,'efg' ,'2010-5-2' union
select '2', 'efg', '2010-5-3'
select weizhi,min(numbers) nubmers,min(times)times from tb
group by weizhiweizhi nubmers times
-------------------- ----------- ------------------------------------------------------
abc 1 2010-05-01 00:00:00.000
efg 2 2010-05-02 00:00:00.000select min(numbers),min(times),weizhi from tb
group by weizhi
你用的SQL 2000吧,2000里没有,2005才有这个的。
(num int,
location char(10),
timea datetime
)insert
into #a
select '2','abc','2010-5-1' union all
select '2','abc','2010-5-1' union all
select '1','abc','2010-5-1' union all
select '1','abc','2010-5-2' union all
select '2','efg','2010-5-2' union all
select '2','efg','2010-5-3'
select min(num) 数量,location,min(timea) 时间
from #a
group by location数量 location 时间
----------- ---------- ------------------------------------------------------
1 abc 2010-05-01 00:00:00.000
2 efg 2010-05-02 00:00:00.000(所影响的行数为 2 行)
我的思路是 先找出同位置数量最小的所有数据。
select * from tb where exists( select 1 from
(select min(数量)as 数量 , 位置 from tb group by 位置) a where a. 数量 =tb .数量 and a.位置=tb.位置然后把它当做表b,然后再按照b的数量和位置字段分组,找出最小时间(也就是距离当前时间最长的)的数据即:
select 数量,位置,min(时间) from bgroup by b.数量 , b.位置
这样就找出了同样位置中首先找数量最小然后再找时间最长的数据。
insert tb2
select 2 ,'abc' ,'2010-5-1' union all
select 1 ,'abc' ,'2010-5-1' union all
select 1 ,'abc' ,'2010-5-2' union all
select 2 ,'efg' ,'2010-5-2' union all
select 2 ,'efg' ,'2010-5-3'select * from tb2
select min(数量) as 数量,位置,min(时间) as 时间 from tb2 group by 位置
(
数量 int,
位置 varchar(3),
时间 datetime
)INSERT INTO @TABLE
SELECT 2,'abc','2010-5-1'
UNION
SELECT 1,'abc','2010-5-1'
UNION
SELECT 1,'abc','2010-5-2'
UNION
SELECT 2,'efg','2010-5-2'
UNION
SELECT 2,'efg','2010-5-3'
SELECT B.*
FROM ( SELECT A.*
FROM @TABLE AS A
WHERE NOT EXISTS (SELECT * FROM @TABLE WHERE 位置=A.位置 AND 数量<A.数量)
) AS B
WHERE NOT EXISTS (
SELECT * FROM ( SELECT A.*
FROM @TABLE AS A
WHERE NOT EXISTS (SELECT * FROM @TABLE WHERE 位置=A.位置 AND 数量<A.数量)
) AS C
WHERE C.位置 =B.位置 AND C.数量=B.数量 AND C.时间 <B.时间
)
2 abc 2010-5-1
1 abc 2010-5-1
1 abc 2010-5-2
2 efg 2010-5-2
2 efg 2010-5-3期望结果
每个位置只出一条记录,且是数量最小的,并且是时间最久的。数量 位置 时间
1 abc 2010-5-1
2 efg 2010-5-2select * from tb a where not exists(select 1 from tb where a.位置=位置 and 数量<a.数量 and 时间<a.时间)
from TB A
where not exists(select 1 from TB where A.数量>数量 and A.时间>时间 and A.位置 = 位置)
from tab
group by b
(select 数量,位置,时间,id=row_number() over(partition by 位置 order by 数量,时间)
from tablename) tt
where tt.id=1--数量 条件 优先于 时间,如果 时间条件 优先于 数量 修改order by 顺序为 时间,数量
where not exists(select 1 from 表名 where 位置 =a.位置 and 数量<a.数量 and 时间<a.时间 )
from TB
group by [位置]
这个方法最简单,效率最高,但有个问题。
就是时间需要转换为时间类型在进行比较,要不然会出问题。
如:2010-5-2 和 2010-10-3
a.位置,
min(a.时间) as 时间
from tb a
group by a.位置
order by a.位置;
IF OBJECT_ID('[tb]') IS NOT NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb] ([数量] [int],[位置] [nvarchar](10),[时间] [datetime])
INSERT INTO [tb]
SELECT '2','abc','2010-5-1' UNION ALL
SELECT '1','abc','2010-5-2' UNION ALL
SELECT '1','abc','2010-5-3' UNION ALL
SELECT '2','efg','2010-5-2' UNION ALL
SELECT '2','efg','2010-5-3'
-->SQL查询如下:
--
SELECT *
FROM [tb] t
WHERE NOT EXISTS(
SELECT 1
FROM tb
WHERE t.位置 = 位置
AND (
数量<t.数量
OR 数量=t.数量
AND 时间<t.时间
)
)
/*
数量 位置 时间
----------- ---------- -----------------------
1 abc 2010-05-02 00:00:00.000
2 efg 2010-05-02 00:00:00.000(2 行受影响)
*/
[征集]分组取最大N条记录方法征集,及散分....
select min(数量)as 数量,[位置],min([时间]) as 时间
from TB
group by [位置]这个是取的同种位置中数量最小的 时间最小(即距离现在时间最长的值)比如
2 杭州 2010.5.30
1 杭州 2010.5.31
1 杭州 2010.6.1
按照上面的语句查询的结果应该是:1 杭州 2010.5.30 而楼主的意思是要查询结果是 1 杭州 2010.5.31