select * from t aa
where (select count(*) from t where  规格=aa.规格 and 日期<=aa.日期)=3

解决方案 »

  1.   

    select * from t aa
    where (select count(*) from t where  规格=aa.规格 and 日期<=aa.日期)<=3  --前三个,应该是
      

  2.   

    --或者:
    select * from t
    where 日期 in(select top 3 日期 from t as a where t.规格=a.规格 order by 日期)
      

  3.   

    zjcxc(邹建)  的那个对   我那个少了一个小于
    select * from t aa
    where (select count(*) from t where  规格=aa.规格 and 日期<=aa.日期)<=3
      

  4.   

    谢谢两位高手,
    select * from t
    where 日期 in(select top 3 日期 from t as a where t.规格=a.规格 order by 日期)
    挺好的
      

  5.   

    --如果有日期重复,这样写!
    select * from t
    where 日期 in(select top 3 with ties 日期 from t as a where t.规格=a.规格 order by 日期)
      

  6.   

    select * from tab a where a.日期 in(select top 3 b.日期 from tab b where a.规格=b.规格)
      

  7.   

    我是说楼上   shuiniu(飞扬的梦)(我是一头只吃西红柿的水牛)
      

  8.   

    create table tab(名称 char(6),规格 char(4),单价 money,日期 datetime)
    insert into tab select
    '电脑', 'A1', 2, '2004-3-3' union all select
    '电脑', 'A1', 3, '2004-4-4' union all select
    '电脑', 'A1', 4, '2004-5-5' union all select
    '电脑', 'A1', 10, '2004-6-6' union all select
    '电脑', 'A2', 5, '2004-3-3' union all select
    '电脑', 'A2', 6, '2004-4-4' union all select
    '电脑', 'A2', 7, '2004-5-5' union all select
    '电脑', 'A2', 8, '2004-6-6' union all select
    '电脑', 'A4',    5, '2004-3-3' union all selectselect * from tab a where a.日期 in(select top 3 b.日期 from tab b where a.规格=b.规格) 名称     规格   单价                    日期                                                     
    ------ ---- --------------------- ------------------------------------------------------ 
    电脑     A1   2.0000                2004-03-03 00:00:00.000
    电脑     A1   3.0000                2004-04-04 00:00:00.000
    电脑     A1   4.0000                2004-05-05 00:00:00.000
    电脑     A2   5.0000                2004-03-03 00:00:00.000
    电脑     A2   6.0000                2004-04-04 00:00:00.000
    电脑     A2   7.0000                2004-05-05 00:00:00.000
    电脑     A4   5.0000                2004-03-03 00:00:00.000
    电脑     A4   6.0000                2004-04-04 00:00:00.000
    电脑     A4   7.0000                2004-05-05 00:00:00.000(所影响的行数为 9 行)
      

  9.   

    shuiniu(飞扬的梦)(我是一头只吃西红柿的水牛)   是可以处理重复情况   呵呵
      

  10.   

    --是有问题,这样试试!
    select * from t
    where 日期 in(select top 3 日期 from t as a where t.规格=a.规格 group by 日期)
      

  11.   

    呵呵少了个order by
    create table tab(名称 char(6),规格 char(4),单价 money,日期 datetime)
    insert into tab select
    '电脑', 'A1', 2, '2004-3-3' union all select
    '电脑', 'A1', 3, '2004-4-4' union all select
    '电脑', 'A1', 4, '2004-5-5' union all select
    '电脑', 'A1', 10, '2004-6-6' union all select
    '电脑', 'A2', 5, '2004-3-3' union all select
    '电脑', 'A2', 6, '2004-4-4' union all select
    '电脑', 'A2', 7, '2004-5-5' union all select
    '电脑', 'A2', 8, '2004-6-6' union all select
    '电脑', 'A4',    5, '2004-3-3' union all select
    '电脑', 'A4',   6, '2004-4-4' union all select
    '电脑', 'A4', 7, '2004-5-5' union all select
    '电脑', 'A4', 8, '2004-6-6'
    select * from tab a where a.日期 in(select top 3 b.日期 from tab b where a.规格=b.规格 order by b.日期)名称     规格   单价                    日期                                                     
    ------ ---- --------------------- ------------------------------------------------------ 
    电脑     A1   2.0000                2004-03-03 00:00:00.000
    电脑     A1   3.0000                2004-04-04 00:00:00.000
    电脑     A1   4.0000                2004-05-05 00:00:00.000
    电脑     A2   5.0000                2004-03-03 00:00:00.000
    电脑     A2   6.0000                2004-04-04 00:00:00.000
    电脑     A2   7.0000                2004-05-05 00:00:00.000
    电脑     A4   5.0000                2004-03-03 00:00:00.000
    电脑     A4   6.0000                2004-04-04 00:00:00.000
    电脑     A4   7.0000                2004-05-05 00:00:00.000(所影响的行数为 9 行)
      

  12.   

    没问题的  楼主要求是最早的三个电脑     A1   2.0000                2004-03-03 00:00:00.000
    电脑     A1   44.0000                2004-03-03 00:00:00.000  --日期相同的
    电脑     A1   4.0000                2004-05-05 00:00:00.000那么这样也算最早的 注意单价不同
      

  13.   

    select * from Table1
    where 日期 in
         (select top 3 with ties 日期 from Table1 t where table1.guige=t.guige order by 日期) 
    order by 规格
      

  14.   

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[temp1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[temp1]if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[temp2]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[temp2]create table temp1(名稱 varchar(20) not null,規格 varchar(20) not null,單價 int not null,日期 smalldatetime not null)
    create table temp2(名稱 varchar(20) not null,規格 varchar(20) not null,單價 int not null,日期 smalldatetime not null)
    goinsert into temp1
    select '電腦', 'A1', 2,  '2004-3-3' union all
    select '電腦', 'A1', 3,  '2004-4-4' union all
    select '電腦', 'A1', 4,  '2004-5-5' union all
    select '電腦', 'A1', 10, '2004-6-6' union all
    select '電腦', 'A2', 5,  '2004-3-3' union all
    select '電腦', 'A2', 6,  '2004-4-4' union all
    select '電腦', 'A2', 7,  '2004-5-5' union all
    select '電腦', 'A2', 8,  '2004-6-6' union all
    select '電腦', 'A4', 5,  '2004-3-3' union all
    select '電腦', 'A4', 6,  '2004-4-4' union all
    select '電腦', 'A4', 7,  '2004-5-5' union all
    select '電腦', 'A4', 8,  '2004-6-6'    insert into temp2
    select '電腦', 'A1', 2,  '2004-3-3' union all
    select '電腦', 'A1', 3,  '2004-4-4' union all
    select '電腦', 'A1', 4,  '2004-5-5' union all
    select '電腦', 'A1', 10, '2004-6-6' union all
    select '電腦', 'A2', 5,  '2004-3-3' union all
    select '電腦', 'A2', 6,  '2004-4-4' union all
    select '電腦', 'A2', 7,  '2004-5-5' union all
    select '電腦', 'A2', 8,  '2004-6-6' union all
    select '電腦', 'A2', 9,  '2004-6-6' union all
    select '電腦', 'A4', 5,  '2004-3-3' union all
    select '電腦', 'A4', 6,  '2004-4-4' union all
    select '電腦', 'A4', 7,  '2004-5-5' union all
    select '電腦', 'A4', 7,  '2004-5-5' union all
    select '電腦', 'A4', 8,  '2004-6-6'    
     
    goselect * from temp1
    /*
    名稱     規格     單價              日期
    電腦 A1 2 2004-03-03 00:00:00
    電腦 A1 3 2004-04-04 00:00:00
    電腦 A1 4 2004-05-05 00:00:00
    電腦 A1 10 2004-06-06 00:00:00
    電腦 A2 5 2004-03-03 00:00:00
    電腦 A2 6 2004-04-04 00:00:00
    電腦 A2 7 2004-05-05 00:00:00
    電腦 A2 8 2004-06-06 00:00:00
    電腦 A4 5 2004-03-03 00:00:00
    電腦 A4 6 2004-04-04 00:00:00
    電腦 A4 7 2004-05-05 00:00:00
    電腦 A4 8 2004-06-06 00:00:00*/select * from temp2
    /*
    名稱     規格     單價             日期
    電腦 A1 2 2004-03-03 00:00:00
    電腦 A1 3 2004-04-04 00:00:00
    電腦 A1 4 2004-05-05 00:00:00
    電腦 A1 10 2004-06-06 00:00:00
    電腦 A2 5 2004-03-03 00:00:00
    電腦 A2 6 2004-04-04 00:00:00
    電腦 A2 7 2004-05-05 00:00:00
    電腦 A2 8 2004-06-06 00:00:00
    電腦 A2 9 2004-06-06 00:00:00
    電腦 A4 5 2004-03-03 00:00:00
    電腦 A4 6 2004-04-04 00:00:00
    電腦 A4 7 2004-05-05 00:00:00
    電腦 A4 7 2004-05-05 00:00:00
    電腦 A4 8 2004-06-06 00:00:00*/--------------------(名稱+規格+日期唯一)-------------------------------
    --方法一
    select * from temp1 a
    where (select count(*) from temp1 where  a.名稱=名稱 and a.規格=規格 and a.日期>日期)<3
    /*
    名稱     規格     單價            日期
    電腦 A1 2 2004-03-03 00:00:00
    電腦 A1 3 2004-04-04 00:00:00
    電腦 A1 4 2004-05-05 00:00:00
    電腦 A2 5 2004-03-03 00:00:00
    電腦 A2 6 2004-04-04 00:00:00
    電腦 A2 7 2004-05-05 00:00:00
    電腦 A4 5 2004-03-03 00:00:00
    電腦 A4 6 2004-04-04 00:00:00
    電腦 A4 7 2004-05-05 00:00:00*/--方法二
    select * from temp1 a
    where 日期 in (select top 3 日期 from temp1 where  a.名稱=名稱 and a.規格=規格  order by 日期)
    /*
    名稱     規格     單價            日期
    電腦 A1 2 2004-03-03 00:00:00
    電腦 A1 3 2004-04-04 00:00:00
    電腦 A1 4 2004-05-05 00:00:00
    電腦 A2 5 2004-03-03 00:00:00
    電腦 A2 6 2004-04-04 00:00:00
    電腦 A2 7 2004-05-05 00:00:00
    電腦 A4 5 2004-03-03 00:00:00
    電腦 A4 6 2004-04-04 00:00:00
    電腦 A4 7 2004-05-05 00:00:00*/
    --------------------(名稱+規格+日期不唯一)-------------------------------
    alter table temp2 add id int not null identity(1,1)
    goselect a.名稱,a.規格,a.單價,a.日期 from temp2 a
    where id in (select top 3 id from temp2 where  a.名稱=名稱 and a.規格=規格  order by 日期)
    /*
    名稱     規格     單價            日期
    電腦 A1 2 2004-03-03 00:00:00
    電腦 A1 3 2004-04-04 00:00:00
    電腦 A1 4 2004-05-05 00:00:00
    電腦 A2 5 2004-03-03 00:00:00
    電腦 A2 6 2004-04-04 00:00:00
    電腦 A2 7 2004-05-05 00:00:00
    電腦 A4 5 2004-03-03 00:00:00
    電腦 A4 6 2004-04-04 00:00:00
    電腦 A4 7 2004-05-05 00:00:00*/alter table temp2 drop column id
      

  15.   

    老大们,这个 语句 写成  连接  如何写啊??、inner join