数据如下
编号 产品名称 企业编号 时间
1 aaa 1 2007-5-20 18:01:00
2 bbb 1 2007-5-20 18:02:00
3 ccc 2 2007-5-20 18:03:00
4 aaa 2 2007-5-20 18:04:00
5 aaa 3 2007-5-20 18:05:00
6 bbb 3 2007-5-20 18:02:00
.......
希望取按时间排列最近的16条数据,同时一个企业只显示一个产品
5 aaa 3 2007-5-20 18:05:00
4 aaa 2 2007-5-20 18:04:00
2 bbb 1 2007-5-20 18:02:00

解决方案 »

  1.   

    /*create table t(编号 int,产品名称 varchar(10),企业编号 int,时间 datetime)
    insert t select 1,'aaa',1,'2007-5-20 18:01:00'
    union all select 2,'nnn',1,'2007-5-20 18:02:00'
    union all select 3,'ccc',2,'2007-5-20 18:03:00'
    union all select 4,'zzz',2,'2007-5-20 18:04:00'
    union all select 5,'jjj',3,'2007-5-20 18:05:00'
    union all select 6,'mmm',4,'2007-5-20 18:02:00'
    union all select 7,'bbb',4,'2007-5-20 18:03:00'
    union all select 8,'bbb',4,'2007-5-20 18:06:00'
    union all select 9,'bbb',4,'2007-5-20 18:03:00'
    union all select 10,'fff',4,'2007-5-20 18:06:00'
    union all select 11,'bbb',4,'2007-5-20 18:05:00'*/create table t(编号 int,产品名称 varchar(10),企业编号 int,时间 datetime)
    insert t select 1,'aaa',1,'2007-5-20 18:01:00'
    union all select 2,'bbb',1,'2007-5-20 18:02:00'
    union all select 3,'ccc',2,'2007-5-20 18:03:00'
    union all select 4,'aaa',2,'2007-5-20 18:04:00'
    union all select 5,'aaa',3,'2007-5-20 18:05:00'
    union all select 6,'bbb',3,'2007-5-20 18:02:00'
    select 编号,产品名称,企业编号,时间 from t c where 编号 in
    (
    select top 2 编号 from
    (
    select 编号,产品名称,企业编号,时间 from t a where 产品名称 in
    (
    select top 1 产品名称 from
    (
    select top 100 percent 产品名称,企业编号 from t group by 产品名称,企业编号 order by 企业编号
    )b where a.企业编号=b.企业编号
    )
    )d where d.企业编号=c.企业编号 order by 时间 desc

    order by 编号 descdrop table t编号        产品名称  企业编号     时间                                                     
    ----------- ---------- ----------- ------
    5           aaa        3           2007-05-20 18:05:00.000
    4           aaa        2           2007-05-20 18:04:00.000
    1           aaa        1           2007-05-20 18:01:00.000
      

  2.   

    --改下
    select 编号,产品名称,企业编号,时间 from t c where 编号 in
    (
    select top 16 编号 from
    (
    select 编号,产品名称,企业编号,时间 from t a where 产品名称 in
    (
    select top 1 产品名称 from
    (
    select top 100 percent 产品名称,企业编号 from t group by 产品名称,企业编号 order by 企业编号
    )b where a.企业编号=b.企业编号
    )
    )d where d.企业编号=c.企业编号 order by 时间 desc

    order by 编号 desc
      

  3.   

    兄弟(握手),有上万条记录,只是首页显示16条,有没有简单点,效率高点的,5个select看起来有点吓人。
    注:只需要调出企业最新发布的产品16条数据,每个企业只显示一个产品在首页,要得是这个效果,不需要编号排序thank you all the same
      

  4.   

    declare @t table(编号 int, 产品名称 varchar(20), 企业编号 int, 时间 datetime)
    insert @t
    select 1, 'aaa', 1, '2007-5-20 18:01:00' union all
    select 2, 'bbb', 1, '2007-5-20 18:02:00' union all
    select 3, 'ccc', 2, '2007-5-20 18:03:00' union all
    select 4, 'aaa', 2, '2007-5-20 18:04:00' union all
    select 5, 'aaa', 3, '2007-5-20 18:05:00' union all
    select 6, 'bbb', 3, '2007-5-20 18:02:00'----企业编号相同的行去时间最大的行
    select top 16 * from @t as a 
    where not exists(select 1 from @t where 企业编号=a.企业编号 and 时间>a.时间)
    order by 企业编号 desc/*结果
    5 aaa 3 2007-5-20 18:05:00
    4 aaa 2 2007-5-20 18:04:00
    2 bbb 1 2007-5-20 18:02:00
    */
      

  5.   

    CREATE TABLE #T(编号 int IDENTITY(1,1),产品名称 nvarchar(20),企业编号 int, 时间 datetime)
    INSERT INTO #T
    SELECT 'aaa', 1 ,'2007-5-20 18:01:00' UNION ALL
    SELECT 'bbb', 1 ,'2007-5-20 18:02:00' UNION ALL
    SELECT 'ccc', 2 ,'2007-5-20 18:03:00' UNION ALL
    SELECT 'aaa', 2 ,'2007-5-20 18:04:00' UNION ALL
    SELECT 'aaa', 3 ,'2007-5-20 18:05:00' UNION ALL
    SELECT 'bbb', 3 ,'2007-5-20 18:02:00'
    SELECT TOP 16 * FROM #T AS A  WHERE NOT EXISTS(SELECT 1 FROM #T AS B WHERE B.企业编号=A.企业编号 AND B.时间>A.时间)
    ORDER BY 时间 DESC 
    DROP TABLE #T
    /*
    编号 产品名称 企业编号 时间
    -----------------------------------
    5  aaa  3  2007-5-20 18:05:00
    4  aaa  2  2007-5-20 18:04:00
    2  bbb  1  2007-5-20 18:02:00
    */
      

  6.   

    Bingo~~~~
    Thank you~ 效果OK。
    不过想问问
    select 1 怎么也可以通过啊?
    我老潜意识觉得 select top 1 * from
      

  7.   

    select top 16 * from @t as a 
    where not exists(select 1 from @t where 企业编号=a.企业编号 and 时间>a.时间)
    order by 企业编号 desc 我喜欢这句!!简单