需求是这样的
有4个类型分别为 行业,地域,学校,老乡  这是一个表。第二个表是 具体的信息表了。和上面的表进行了主外关链
而现在的要求是 我要展现成这样。行业:地域:学校:老乡:每一种类型要显示前30个数据。这个大概怎么做。。我现在的做法是,,用union把这4个连接起来的。有没有好点的办法,提高效率的办法

解决方案 »

  1.   

    分析函数
    效率较低,但对于动态分类比较方便
    select * from (
      select t.*,row_number() over(partition by 分组 order by 顺序) as num from t
    )where num<=30
      

  2.   

    select * from (
      select t.*,row_number() over(partition by 类型 order by 1) as num from 
                (Select 类型表,信息表 where 类型表.pk=信息表.fk) t
    )where num<=30
      

  3.   

    按如下表取某个字段(ID)分类后同时按时间(rdate)升序、降序前三条记录。if object_id('tempdb..#tab') is not null
       drop table #tab
    GO
    create table #tab (
    [id] [char](10),[age] [int],[rdate] [datetime])insert into #tab(id,age,rdate) values('a' , 1 , '2006-01-01')
    insert into #tab(id,age,rdate) values('a' , 2 , '2006-01-02')
    insert into #tab(id,age,rdate) values('a' , 3 , '2006-01-03')
    insert into #tab(id,age,rdate) values('a' , 4 , '2006-01-04')
    insert into #tab(id,age,rdate) values('a' , 5 , '2006-01-05')
    insert into #tab(id,age,rdate) values('a' , 6 , '2006-01-06')
    insert into #tab(id,age,rdate) values('b' , 1 , '2006-02-01')
    insert into #tab(id,age,rdate) values('b' , 2 , '2006-02-02')
    insert into #tab(id,age,rdate) values('b' , 3 , '2006-02-03')
    insert into #tab(id,age,rdate) values('b' , 4 , '2006-02-04')
    insert into #tab(id,age,rdate) values('c' , 1 , '2006-03-01')
    insert into #tab(id,age,rdate) values('c' , 2 , '2006-03-02')
    insert into #tab(id,age,rdate) values('c' , 3 , '2006-03-03')
    insert into #tab(id,age,rdate) values('d' , 1 , '2006-04-01')
    insert into #tab(id,age,rdate) values('d' , 2 , '2006-04-02')
    insert into #tab(id,age,rdate) values('e' , 1 , '2006-05-01')--按时间rdate升序取前三条
    select * from #tab t
    where rdate in 
    (
    select top 3 rdate from #tab where id=t.id order by rdate
    )id         age         rdate                                                  
    ---------- ----------- ------------------------------------------------------ 
    a          1           2006-01-01 00:00:00.000
    a          2           2006-01-02 00:00:00.000
    a          3           2006-01-03 00:00:00.000
    b          1           2006-02-01 00:00:00.000
    b          2           2006-02-02 00:00:00.000
    b          3           2006-02-03 00:00:00.000
    c          1           2006-03-01 00:00:00.000
    c          2           2006-03-02 00:00:00.000
    c          3           2006-03-03 00:00:00.000
    d          1           2006-04-01 00:00:00.000
    d          2           2006-04-02 00:00:00.000
    e          1           2006-05-01 00:00:00.000(所影响的行数为 12 行)--按时间rdate降序取前三条
    select * from #tab t
    where rdate in 
    (
    select top 3 rdate from #tab where id=t.id order by rdate desc
    )
    order by id , rdate descid         age         rdate                                                  
    ---------- ----------- ------------------------------------------------------ 
    a          6           2006-01-06 00:00:00.000
    a          5           2006-01-05 00:00:00.000
    a          4           2006-01-04 00:00:00.000
    b          4           2006-02-04 00:00:00.000
    b          3           2006-02-03 00:00:00.000
    b          2           2006-02-02 00:00:00.000
    c          3           2006-03-03 00:00:00.000
    c          2           2006-03-02 00:00:00.000
    c          1           2006-03-01 00:00:00.000
    d          2           2006-04-02 00:00:00.000
    d          1           2006-04-01 00:00:00.000
    e          1           2006-05-01 00:00:00.000
    (所影响的行数为 12 行)
    --上面包含了总数不到3个的记录(即id为d,e的数据),如果要取消它们,以升序为例(降序同理)
    select * from #tab m
    where rdate in 
    (
      select top 3 rdate from 
      (
      select * from #tab t
      where id in 
        (
        select id from #tab group by id having(count(*)) >= 3
        )
      ) n
      where m.id = n.id order by rdate
    )id         age         rdate                                                  
    ---------- ----------- ------------------------------------------------------ 
    a          1           2006-01-01 00:00:00.000
    a          2           2006-01-02 00:00:00.000
    a          3           2006-01-03 00:00:00.000
    b          1           2006-02-01 00:00:00.000
    b          2           2006-02-02 00:00:00.000
    b          3           2006-02-03 00:00:00.000
    c          1           2006-03-01 00:00:00.000
    c          2           2006-03-02 00:00:00.000
    c          3           2006-03-03 00:00:00.000
    (所影响的行数为 9 行)--在上面的例中我们发现rdate都是不相同的,如果相同怎么办?
    --例如id=a,第三条,第四条rdate相同,都为2006-01-03。
    id         age         rdate                                                  
    ---------- ----------- ------------------------------------------------------ 
    a          1           2006-01-01 00:00:00.000
    a          2           2006-01-02 00:00:00.000
    a          3           2006-01-03 00:00:00.000
    a          4           2006-01-03 00:00:00.000
    a          5           2006-01-05 00:00:00.000
    a          6           2006-01-06 00:00:00.000
    b          1           2006-02-01 00:00:00.000
    b          2           2006-02-02 00:00:00.000
    b          3           2006-02-03 00:00:00.000
    b          4           2006-02-04 00:00:00.000
    c          1           2006-03-01 00:00:00.000
    c          2           2006-03-02 00:00:00.000
    c          3           2006-03-03 00:00:00.000
    d          1           2006-04-01 00:00:00.000
    d          2           2006-04-02 00:00:00.000
    e          1           2006-05-01 00:00:00.000--如果想把第三、四都取出来,使用上面的语句即可。如果只取一条(只取第三不取第四)则要使用临时表了。
    if object_id('tempdb..#tab') is not null
       drop table #tab
    GO
    if object_id('tempdb..#temp') is not null
       drop table #temp
    GO
    create table #tab (
    [id] [char](10),[age] [int],[rdate] [datetime])insert into #tab(id,age,rdate) values('a' , 1 , '2006-01-01')
    insert into #tab(id,age,rdate) values('a' , 2 , '2006-01-02')
    insert into #tab(id,age,rdate) values('a' , 3 , '2006-01-03')
    insert into #tab(id,age,rdate) values('a' , 4 , '2006-01-03')
    insert into #tab(id,age,rdate) values('a' , 5 , '2006-01-05')
    insert into #tab(id,age,rdate) values('a' , 6 , '2006-01-06')
    insert into #tab(id,age,rdate) values('b' , 1 , '2006-02-01')
    insert into #tab(id,age,rdate) values('b' , 2 , '2006-02-02')
    insert into #tab(id,age,rdate) values('b' , 3 , '2006-02-03')
    insert into #tab(id,age,rdate) values('b' , 4 , '2006-02-04')
    insert into #tab(id,age,rdate) values('c' , 1 , '2006-03-01')
    insert into #tab(id,age,rdate) values('c' , 2 , '2006-03-02')
    insert into #tab(id,age,rdate) values('c' , 3 , '2006-03-03')
    insert into #tab(id,age,rdate) values('d' , 1 , '2006-04-01')
    insert into #tab(id,age,rdate) values('d' , 2 , '2006-04-02')
    insert into #tab(id,age,rdate) values('e' , 1 , '2006-05-01')--按时间rdate升序取前三条(其他方法同上)
    select id1=identity(int,1,1),* into #temp from #tab order by id , rdate /*(降序用rdate desc)*/
    select * from #temp t
    where id1 in 
    (
    select top 3 id1 from #temp where id=t.id order by id1
    )
      

  4.   

    看不懂,觉得UNION是一种解决方法。