CREATE TABLE t
(
  商品ID VARCHAR(20),
  用户ID VARCHAR(20),
  商品名称 VARCHAR(20),
  添加日期 DATETIME
)INSERT INTO t
   SELECT '0001','user1','名称1','2007-10-18 10:11:50.483' UNION ALL 
   SELECT '0002','user2','名称2','2007-10-18 10:21:50.483' UNION ALL  
   SELECT '0003','user2','名称3','2007-10-18 10:31:50.483' UNION ALL   
   SELECT '0004','user2','名称4','2007-10-18 10:41:50.483' UNION ALL   
   SELECT '0005','user1','名称5','2007-10-18 10:51:50.483' DECLARE @sql VARCHAR(8000),@i INT  SELECT @sql = 'SELECT 用户ID',@i = 1  WHILE @i<3
  SELECT @sql = @sql + ',[商品'+CAST(@i AS VARCHAR)+'] = MAX(CASE WHEN id = '+CAST(@i AS VARCHAR)+'
                                                             THEN 商品名称 END)',
         @i = @i + 1
  SELECT @sql = @sql + ' FROM (  SELECT id=(SELECT COUNT(1) FROM t WHERE A.用户ID = 用户ID AND A.添加日期 <= 添加日期),*
   FROM t A WHERE (SELECT COUNT(1) FROM t WHERE A.用户ID=用户ID AND A.添加日期 < 添加日期)<2
  ) A GROUP BY 用户ID'  EXEC(@sql)
DROP TABLE t
用户ID                 商品1                  商品2                  
-------------------- -------------------- -------------------- 
user1                名称5                  名称1
user2                名称4                  名称3警告: 聚合或其它 SET 操作消除了空值。

解决方案 »

  1.   

    select 用户ID,
      max(case px when 1 then 商品名称 then '' end) '商品一',
      max(case px when 2 then 商品名称 then '' end) '商品二'
    from
    (
      select px=(select count(1) from tb where 用户ID=a.用户ID and 添加日期>a.添加日期)+1 , 用户ID,商品名称 from tb a
    ) t
    group by  用户ID
      

  2.   

    create table tb(商品ID varchar(10),用户ID varchar(10),商品名称 varchar(10),添加日期 datetime)
    insert into tb values('0001','user1','名称1','2007-10-10')
    insert into tb values('0002','user2','名称2','2007-10-11')
    insert into tb values('0003','user2','名称3','2007-10-12')
    insert into tb values('0004','user2','名称4','2007-10-13')
    insert into tb values('0005','user1','名称5','2007-10-14') 
    go
    select 用户ID,
      max(case px when 1 then 商品名称 else '' end) '商品一',
      max(case px when 2 then 商品名称 else '' end) '商品二'
    from
    (
      select px=(select count(1) from tb where 用户ID=a.用户ID and 添加日期>a.添加日期)+1 , 用户ID,商品名称 from tb a
    ) t
    group by  用户IDdrop table tb/*
    用户ID      商品一        商品二        
    ---------- ---------- ---------- 
    user1      名称5        名称1
    user2      名称4        名称3(所影响的行数为 2 行)
    */
      

  3.   

    declare @table table(
    商品ID  nvarchar(10),
    用户ID nvarchar(10),
    商品名称 nvarchar(10),
    添加日期 datetime
    )insert into @table
    select '0001','user1','名称1',getDate()
    union select '0002','user2','名称2',getDate()
    union select '0003','user2','名称3',getDate()
    union select '0004','user2','名称4',getDate()
    union select '0005','user1','名称5',getDate() 
    union select '0006','user3','名称6',getDate() 
    select 用户ID,商品名称
    into #tmp
    from @table  a
    where (select count(*)from @table b where a.用户id=b.用户id and b.商品名称<a.商品名称)<2select a.用户id,a.商品名称 as 商品一,b.商品名称 as 商品二 from #tmp a
    inner join #tmp b on a.商品名称<b.商品名称 and a.用户id=b.用户id
    order by a.用户iddrop table #tmp
      

  4.   

    declare @a table(商品ID   varchar(100),    用户ID   varchar(100),    商品名称 varchar(100),     添加日期 smalldatetime)
    insert @a select '0001',        'user1',        '名称1','2001-03-03'
    union all select '0002'         ,'user2'        ,'名称2', '2002-03-08'
    union all select    '0003'         ,'user2'        ,'名称3', '2007-02-09'
    union all select    '0004'         ,'user2'        ,'名称4', '2006-05-06'
    union all select    '0005'        ,'user1'         ,'名称5', '2004-08-09'select aa.用户id,aa.商品名称 商品一,bb.商品名称 商品二 from
    (select top 100 percent  * from @a a where 商品id in(select top 2 商品id from @a where 用户id=a.用户id order by 添加日期 desc) order by 用户id,添加日期)aa
    inner  join
    @a bb
    on aa.用户id=bb.用户id and aa.添加日期<bb.添加日期
      

  5.   

    --result
    /*用户id                           商品一                            商品二                            
    ------------------------------ ------------------------------ ------------------------------ 
    user1                          名称1                            名称5
    user2                          名称4                            名称3
    */
      

  6.   

    create table tb(商品ID varchar(10),用户ID varchar(10),商品名称 varchar(10),添加日期 datetime)
    insert into tb values('0001','user1','名称1','2007-10-10')
    insert into tb values('0002','user2','名称2','2007-10-11')
    insert into tb values('0003','user2','名称3','2007-10-12')
    insert into tb values('0004','user2','名称4','2007-10-13')
    insert into tb values('0005','user1','名称5','2007-10-14') 
    go
    --只显示前两个(1,2)
    select 用户ID,
      max(case px when 1 then 商品名称 else '' end) '商品一',
      max(case px when 2 then 商品名称 else '' end) '商品二'
    from
    (
      select px=(select count(1) from tb where 用户ID=a.用户ID and 添加日期>a.添加日期)+1 , 用户ID,商品名称 from tb a
    ) t
    group by  用户ID/*
    用户ID      商品一       商品二        
    ---------- ---------- ---------- 
    user1      名称5        名称1
    user2      名称4        名称3(所影响的行数为 2 行)
    */--如果都要显示,得用动态SQL
    declare @sql varchar(8000)
    set @sql = 'select 用户ID'
    select @sql = @sql + ' , max(case px when ''' + cast(px as varchar) + ''' then 商品名称 else '' '' end) [商品' + cast(px as varchar) + ']'
    from (select distinct px from (select px=(select count(1) from tb where 用户ID=a.用户ID and 添加日期>a.添加日期)+1 , 用户ID,商品名称 from tb a) t ) as a
    set @sql = @sql + ' from (select px=(select count(1) from tb where 用户ID=a.用户ID and 添加日期>a.添加日期)+1 , 用户ID,商品名称 from tb a) t group by 用户ID'
    exec(@sql) /*
    用户ID      商品1        商品2        商品3        
    ---------- ---------- ---------- ---------- 
    user1      名称5        名称1         
    user2      名称4        名称3        名称2
    */drop table tb
      

  7.   

    create table #(id int identity,matid varchar(10),user_id varchar(10),matname varchar(10),createtime datetime)
    insert into # select  '0001',        'user1',        '名称1',      getDate() 
    insert into # select  '0002',        'user2',        '名称2',      getDate() 
    insert into # select  '0003',        'user2',        '名称3',      getDate() 
    insert into # select  '0004',        'user2',        '名称4',      getDate() 
    insert into # select  '0005',        'user1',        '名称5',      getDate() 
    select * into #1 from # a
    where (select count(1) from # b where a.user_id=b.user_id and a.createtime<b.createtime)<2select a.user_id  as 用户名,a.matname 商品一,b.matname 商品二 from #1 a inner join #1 b
    on a.user_id=b.user_id and a.createtime<b.createtime/*
    用户名        商品一        商品二
    ---------- ---------- ----------
    user2      名称3        名称4
    user1      名称1        名称5(2 行受影响)
    */
      

  8.   

    请能否“潇洒老乌龟”解释下这句:
    select px=(select count(1) from tb where 用户ID=a.用户ID and 添加日期>a.添加日期)+1 , 用户ID,商品名称 from tb a