现在有一个表 数据如下 :
id   home  num Time
12    中国  32  2000-12:00:00
23    美国  34  2000-12:00:00
24    法国  12  2000-12:00:00
25    日本  12 2000-12:00:00
34    英国  45 2000-12:00:00
35    中国  32  2000-12:00:00
36    美国  34  2000-12:00:00
37    法国  12  2000-12:00:00
38    日本  12 2000-12:00:00
39    英国  45 2000-12:00:00
。。
等等..好多数据..
当然国家不是固定这几个.. 可能还会有别的国家.
我现在想 查出每个国家的前十条数据 改怎么查..
也就是 中国的前十条..美国的前十条..等等....要显示在一个表里..不要用union...

解决方案 »

  1.   

    select top 10 id,home,max(num),max(Time) from Table group by home,id order by id
      

  2.   

    构造临时表,使用right join来实现
      

  3.   

    select top 10 id,home,max(num),max(Time) from Table group by home
    我觉得这样就可以了,后面的不用了吧
      

  4.   

    1.先把国家取出来 select distinct home  from t
    2.遍历这个表取出国家和t比较(游标或者循环),然后再拼sqlsql=sql+'select top 10 id home num Time from t where home='日本' group by '日本' Union'不使用union很难实现。如果你不用这种方法,就坐等其他高手解答吧
      

  5.   


    你这种只是取前10条,而不是每个国家的。还是要构造sql
      

  6.   

    sql 2005:SELECT id ,home ,num ,[Time]
     
    FROM 

       SELECT rid=ROW_NUMBER() OVER(PARTITION BY home ORDER BY id),* 
       FROM #T 
    ) AS T 
    WHERE rid<=10
      

  7.   

    select (select count(id) from table a where a.home=table.home and a.id<=table.id) num,* 
    into #temp from table order by home,num
    select*from #temp where num<=10
      

  8.   

    sql  2000  SELECT * FROM #T AS T WHERE 10>(SELECT COUNT(*) FROM #T WHERE home=T.GID AND id>T.id)#T  是你的数据表名  
      

  9.   

    SELECT * FROM TB T WHERE ID IN(SELECT TOP 10 ID FROM TB WHERE home=T.home )
      

  10.   

    SELECT * FROM #T AS T WHERE 10>(SELECT COUNT(*) FROM #T WHERE home=T.home AND id>T.id)
      

  11.   

    用分析函数
    select
      *
    from (select row_number() over(partition by home order by home asc,id asc) as grp_no,
                 id,
                 home
          from a) t
    where t.grp_no <=10
      

  12.   

    SELECT * FROM #T AS T WHERE 10>(SELECT COUNT(*) FROM #T WHERE home=T.home AND id>T.id)
      

  13.   


    2005和ORACLE已经这么像了?一直用ORACLE的人与时代脱节。。
      

  14.   

    你直接分组查询就ok了么
    select top 10 id,home,max(num),max(Time) from Table group by id,home
      

  15.   

    在最后加上 ORDER BY TIME DESC 不好使么
      

  16.   

    SELECT * FROM TB T WHERE ID IN(SELECT TOP 10 ID FROM TB WHERE home=T.home ORDER BY TIME DESC )
      

  17.   

    个人看法 (其实我也很菜) 你的示例数据似乎都是一个时间 再有是没反应还是报错呀 要是没反应的话那你看看数据吧或者是我说的关键字错了 DESC和ASC我分不清 哈哈
      

  18.   

    试试看
    select DataTABLE.* from TABLE as DataTABLE left join(select home,(select top 1 id from(select top 10 id from TABLE where home=HomeTABLE.home order by id)as IdTABLE order by id desc)as maxId from(select distinct(home)as home in TABLE)as HomeTABLE)as WhereTABLE where DataTABLE.home=WhereTABLE.home and DataTABLE.id<=WhereTABLE.maxId order by home,id
      

  19.   

    select * from TABLE where id in(select IdTABLE.id from TABLE as IdTABLE left join(select distinct(home)as home in TABLE)as HomeTABLE where IdTABLE.id<=(select top 1 id from(select top 10 id from TABLE where home=HomeTABLE.home order by id)as MaxIdTABLE order by id desc))
      

  20.   

    select * from TABLE where id in(select id from(select id,(select count(*)from(select top 10 id from TABLE where home=DataTABLE.home and id<DataTABLE.id)as IdTABLE)as orderId from TABLE as DataTABLE)as OrderTABLE where orderId<10)
      

  21.   


     select  * from 
    ( select  *,ROW_NUMBER()  over (partition by  
    deptid order  by isManageSet desc,workid desc ) cid
    from v_gzdtInfo
    where 
    isManageSet is null or(isManageSet not in (1)) 
    and workStartTime <='2010-06-18'
    and workEndTime >='2010-06-18'
    ) d  where cid <=10
    自己修改下 能用  sql2005
      

  22.   

    再次感谢昨天回复的几位朋友...SQL77 写的sql简练.结果也正确,但是感觉速度慢了点.不过还是谢谢你..
    wyq29和squall_biming 思路差不多..wyq29后面给出的几个也不错..很感谢..sbwwkmyd 后面几个可以. 但是速度感觉上有点慢..sql高手好多..我得学习了..哈哈..
      

  23.   


    if object_ID('TextTable') is null
    create Table TextTable
    (
      ID int IDENTITY(1,1),
      home varchar(10), 
      num int,
      CTime Varchar(10)
    )
    GOdelete from TextTable
    GODECLARE @i int
    set @i=0
    while @i<50
    begin
      insert into TextTable(Home,Num,Ctime)
      select '中国',@i,'2000-01-01'
      union all
      select '美国',@i,'2000-01-02'
      union all
      select '德国',@i,'2000-01-03'
      set @i=@i+1
    endselect * from TextTable
    GOselect * from TextTable a where 
    exists( select * from (select top 10 ID,Home from TextTable where home=a.Home order by ID) b where home=a.home and a.ID<=B.ID )
    order by Home,ID
    GO
      

  24.   


    declare @tb as Table
    (
      ID int IDENTITY(1,1),
      home nvarchar(10), 
      num int,
      CTime Varchar(10)
    )
    DECLARE @i int
    set @i=0
    while @i<50
    begin
      insert into @tb(Home,Num,Ctime)
      select N'中国',@i,'2000-01-01'
      union all
      select N'美国',@i,'2000-01-02'
      union all
      select N'德国',@i,'2000-01-03'
      set @i=@i+1
    end
    select distinct b.* from @tb a
    cross apply
    (select top 10* from @tb b where b.home=a.home  )as b
    order by 2