高手可能觉得很容易了,但我折腾了两天还没头绪
现有的table1:
总共抽奖四次抽奖号码 抽奖日期       奖分类
-------------------------------
0000      2010-12-15    一等奖;数字0,一次出现在抽奖结果中
1111      2010-12-15    二等奖;数字1,三次出现在抽奖结果中
1111      2010-12-16    一等奖
1111      2010-12-17    一等奖
2222      2010-12-16    二等奖;2也出现三次
2222      2010-12-17    二等奖
2222      2010-12-18    三等奖
3333      2010-12-16    三等奖;3出现两次
3333      2010-12-18    一等奖;
4444      2010-12-17    三等奖;4,5,7都只出现一次
5555      2010-12-18    二等奖
7777      2010-12-15    三等奖
想把上表转换成如下表格:
抽奖日期     一等奖 二等奖 三等奖
2010-12-15   0000   1111   7777  
2010-12-16   1111   2222   3333  
2010-12-17   1111   2222   4444  
2010-12-18   3333   5555   2222  

解决方案 »

  1.   

    select 抽奖日期,一等奖=(select 抽奖号码 from tb where a.抽奖日期=抽奖日期 and  奖分类='一等奖'),
    二等奖=(select 抽奖号码 from tb where a.抽奖日期=抽奖日期 and  奖分类='二等奖'),
    三等奖=(select 抽奖号码 from tb where a.抽奖日期=抽奖日期 and  奖分类='三等奖')
    from tb a group by 抽奖日期
      

  2.   

    create table tb(抽奖号码 varchar(10),抽奖日期 datetime,奖分类 varchar(10))insert into tbselect '0000','2010-12-15','一等奖' union all
    select '1111','2010-12-15','二等奖' union all
    select '1111','2010-12-16','一等奖' union all
    select '1111','2010-12-17','一等奖' union all
    select '2222','2010-12-16','二等奖' union all
    select '2222','2010-12-17','二等奖' union all
    select '2222','2010-12-18','三等奖' union all
    select '3333','2010-12-16','三等奖' union all
    select '3333','2010-12-18','一等奖' union all
    select '4444','2010-12-17','三等奖' 
    select 抽奖日期,一等奖=(select 抽奖号码 from tb where a.抽奖日期=抽奖日期 and  奖分类='一等奖'),
    二等奖=(select 抽奖号码 from tb where a.抽奖日期=抽奖日期 and  奖分类='二等奖'),
    三等奖=(select 抽奖号码 from tb where a.抽奖日期=抽奖日期 and  奖分类='三等奖')
    from tb a group by 抽奖日期/*
    抽奖日期                    一等奖        二等奖        三等奖
    ----------------------- ---------- ---------- ----------
    2010-12-15 00:00:00.000 0000       1111       NULL
    2010-12-16 00:00:00.000 1111       2222       3333
    2010-12-17 00:00:00.000 1111       2222       4444
    2010-12-18 00:00:00.000 3333       NULL       2222
      

  3.   

     
    --> 数据库版本:
    --> Microsoft SQL Server 2008 (RTM) - 10.0.1600.22
    --> 测试数据:[TB]
    IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[TB]') 
    AND type in (N'U')) --U 代表你查询的是表
    DROP TABLE [TB]
    GO---->建表
    create table [TB]([抽奖号码] varchar(4),[抽奖日期] datetime,[奖分类] varchar(6))
    insert [TB]
    select '0000','2010-12-15','一等奖' union all
    select '1111','2010-12-15','二等奖' union all
    select '1111','2010-12-16','一等奖' union all
    select '1111','2010-12-17','一等奖' union all
    select '2222','2010-12-16','二等奖' union all
    select '2222','2010-12-17','二等奖' union all
    select '2222','2010-12-18','三等奖' union all
    select '3333','2010-12-16','三等奖' union all
    select '3333','2010-12-18','一等奖' union all
    select '4444','2010-12-17','三等奖' union all
    select '5555','2010-12-18','二等奖' union all
    select '7777','2010-12-15','三等奖'
    GOdeclare @sql varchar(8000)
    select @sql = isnull(@sql+',','')+'max(case when [奖分类]='''+ltrim([奖分类])+'''  then 抽奖号码 else null end) as ['+ltrim([奖分类])+']'
    from (
    select distinct [奖分类] from TB t
    ) r
    select @sql = '
    select [抽奖日期],'+@sql+' 
    from TB r
    group by [抽奖日期]'
    exec(@sql)--> 删除表格
    --DROP TABLE [TB]
     
      

  4.   

    create table #temp
    (
    抽奖号码 varchar(10),
    抽奖日期 datetime,
    奖分类 nvarchar(10)
    )
    insert #temp
    select '0000','2010-12-15',N'一等奖' union all
    select '1111','2010-12-15',N'二等奖' union all
    select '1111','2010-12-16',N'一等奖' union all
    select '1111','2010-12-17',N'一等奖' union all
    select '2222','2010-12-16',N'二等奖' union all
    select '2222','2010-12-17',N'二等奖' union all
    select '2222','2010-12-18',N'三等奖' union all
    select '3333','2010-12-16',N'三等奖' union all
    select '3333','2010-12-18',N'一等奖' union all
    select '4444','2010-12-17',N'三等奖' union all
    select '5555','2010-12-18',N'二等奖' union all
    select '7777','2010-12-15',N'三等奖'SELECT * FROM
    (select * from #temp) a
    pivot
    (max(抽奖号码) for 奖分类 in([一等奖], [二等奖],[三等奖] )) as b
      

  5.   


    declare @table table (抽奖号码 varchar(4),抽奖日期 datetime,奖分类 varchar(6))
    insert into @table
    select '0000','2010-12-15','一等奖' union all
    select '1111','2010-12-15','二等奖' union all
    select '1111','2010-12-16','一等奖' union all
    select '1111','2010-12-17','一等奖' union all
    select '2222','2010-12-16','二等奖' union all
    select '2222','2010-12-17','二等奖' union all
    select '2222','2010-12-18','三等奖' union all
    select '3333','2010-12-16','三等奖' union all
    select '3333','2010-12-18','一等奖' union all
    select '4444','2010-12-17','三等奖' union all
    select '5555','2010-12-18','二等奖' union all
    select '7777','2010-12-15','三等奖'select 
    [抽奖日期]=CONVERT(VARCHAR(10),抽奖日期,120),
    [一等奖]=max(CASE 奖分类 WHEN '一等奖' THEN 抽奖号码 ELSE '' END),
    [二等奖]=max(CASE 奖分类 WHEN '二等奖' THEN 抽奖号码 ELSE '' END),
    [三等奖]=max(CASE 奖分类 WHEN '三等奖' THEN 抽奖号码 ELSE '' END)
     from @table GROUP BY 抽奖日期
     
    /*
    抽奖日期       一等奖  二等奖  三等奖
    ---------- ---- ---- ----
    2010-12-15 0000 1111 7777
    2010-12-16 1111 2222 3333
    2010-12-17 1111 2222 4444
    2010-12-18 3333 5555 2222
    */
      

  6.   

    Msg 512, Level 16, State 1, Line 1
    Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.我这里出现这样的错误
      

  7.   


    This one tested good !
      

  8.   

    This one is also good.