数量 位置 时间
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

解决方案 »

  1.   

    select * 
    from TB A
    where not exists(select 1 from TB where A.数量>数量 and A.时间>时间 and A.位置 = 位置)
      

  2.   


    select * from 表名 a
    where not exists(select 1 from 表名 where 位置 =a.位置 and 数量<a.数量 and 时间<a.时间 )
      

  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.位置)
      

  4.   

    select min(数量)as 数量,[位置],min([时间]) as 时间
    from TB 
    group by [位置]/*
    数量          位置   时间
    ----------- ---- -----------------------
    1           abc  2010-05-01 00:00:00.000
    2           efg  2010-05-02 00:00:00.000(2 行受影响)
    */
    搞复杂了。呵呵。
      

  5.   


    谢谢你。
    这个看起来正确,后来详细试了一下,还是有问题,查询出的结果不是一条记录,而可能是由不同记录的字段组合而成。如:
    数量 位置 时间
    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
      

  6.   

    一个=号的问题create table tb(数量 int,位置 varchar(10), 时间 datetime)insert tb
    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 行)*/
      

  7.   

    DECLARE @a TABLE(a INT,b VARCHAR(20),c SMALLDATETIME)
    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 行)*/ 
      

  8.   

    再次更正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 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 行)
    */
      

  9.   

    DECLARE @a TABLE(sl INT,wz VARCHAR(20),rq SMALLDATETIME)
    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 行受影响)
    */
      

  10.   


    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 行) */
      

  11.   

    试一下这个
    select distinct 数量,位置,时间
    from A
    where 数量<=min(数量)and 时间>=max(时间)
    group by 位置
      

  12.   

    想复杂了。LZ想要的是不是:SELECT *
    FROM (
        SELECT *,
            SN = ROW_NUMBER() OVER(PARTITION BY 位置 ORDER BY 数量, 时间)
        FROM [表]
    ) tmp
    WHERE SN = 1
      

  13.   

    select distinct 时间,位置,数量
    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上运行通过了。它的功能就是把相同位置上数量是最少并且时间是最大的,就查询出来。但如果相同位置上数量是最少,但时间不最大的记录是查询不出来的。同样如果相同位置上时间是最大,但数量不是最少,也无法查询出来。
      

  14.   

    如果我SQL语句功能不符合楼主的要求,希望楼主把你的工能要求写清楚点!我再帮你写SQL语句
      

  15.   

    新手,只是能得出结果if object_id('dbo.tb1') is not null
    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*/
      

  16.   

    谢谢。我的要求就是同位置上的纪录查询出一条:在所有同位置记录中数量是最小的,其次是时间最久远的一条。如:
    数量 位置 时间
    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,互相学习,开阔思路嘛。
      

  17.   

    问一下很精练的那个:
    ROW_NUMBER是什么?怎么提示:'ROW_NUMBER' 不是可以识别的 函数名。
      

  18.   

    create table tb
    (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
      

  19.   


    你用的SQL 2000吧,2000里没有,2005才有这个的。
      

  20.   

    create table #a
    (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 行)
      

  21.   

    引用 12 楼 yibey 的回复:
    我的思路是 先找出同位置数量最小的所有数据。
    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.位置
    这样就找出了同样位置中首先找数量最小然后再找时间最长的数据。
      

  22.   

    create table tb2(数量 int,位置 varchar(10), 时间 datetime)
    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 位置
      

  23.   

    declare @TABLE TABLE
    (
    数量 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.时间
    )
      

  24.   

    数量 位置 时间
    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.时间)
      

  25.   

    select * from tb  where  convert(char,num)+site+convert(char,time) in (select min(convert(char,num)+site+convert(char,time)) from tb group by num)
      

  26.   

    select * from tb  where  convert(char,数量)+位置+convert(char,时间) in (select min(convert(char,数量)+位置+convert(char,时间)) from tb group by 数量)
      

  27.   

    select * 
    from TB A
    where not exists(select 1 from TB where A.数量>数量 and A.时间>时间 and A.位置 = 位置)
      

  28.   

    select min(a) as 数量, b as 位置, convert(char(10), min(c), 120) as 时间
    from tab
    group by b
      

  29.   

    --数量 位置 时间select tt.数量,tt.位置,tt.时间
    (select 数量,位置,时间,id=row_number() over(partition by 位置 order by 数量,时间)
    from tablename) tt
    where tt.id=1--数量 条件 优先于 时间,如果 时间条件 优先于 数量 修改order by 顺序为 时间,数量
      

  30.   

    select 位置,数量,max(时间) from 表1 inner join (select 位置,min(数量) from 表2 group by 位置) on 表1.位置 = 表2.位置试试,没有sql server....
      

  31.   

    select * from 表名 a
    where not exists(select 1 from 表名 where 位置 =a.位置 and 数量<a.数量 and 时间<a.时间 )
      

  32.   

    select min(数量)as 数量,[位置],min([时间]) as 时间
    from TB 
    group by [位置]
    这个方法最简单,效率最高,但有个问题。
    就是时间需要转换为时间类型在进行比较,要不然会出问题。
    如:2010-5-2 和 2010-10-3
      

  33.   

    select min(a.数量) as 数量, 
           a.位置, 
           min(a.时间) as 时间
    from tb a
    group by a.位置
    order by a.位置;
      

  34.   

    --> 生成测试数据表: [tb]
    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 行受影响)
    */
      

  35.   

    参考下贴中的多种方法http://topic.csdn.net/u/20091231/16/2f268740-391e-40f2-a15e-f243b2c925ab.html
    [征集]分组取最大N条记录方法征集,及散分....
      

  36.   

    我觉得很多人误会楼主的意思了比如这种思路
    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