如下数据
id  type date     data  xx
1    16  2010-4-3 3     0
1    16  2010-4-4 4     1
2    48  2010-4-3 3     0
2    48  2010-4-4 2     1
如果得出以下结果呢id  type data1 data2 data   xx1  xx2
1    16  3      4     1     正常 异常
2    48  3      2     -1    正常 异常id  type为唯一
xx
0代表正常
1代表异常

解决方案 »

  1.   

    *
    标题:普通行列转换(version 2.0)
    作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
    时间:2008-03-09
    地点:广东深圳
    说明:普通行列转换(version 1.0)仅针对sql server 2000提供静态和动态写法,version 2.0增加sql server 2005的有关写法。问题:假设有张学生成绩表(tb)如下:
    姓名 课程 分数
    张三 语文 74
    张三 数学 83
    张三 物理 93
    李四 语文 74
    李四 数学 84
    李四 物理 94
    想变成(得到如下结果): 
    姓名 语文 数学 物理 
    ---- ---- ---- ----
    李四 74   84   94
    张三 74   83   93
    -------------------
    */create table tb(姓名 varchar(10) , 课程 varchar(10) , 分数 int)
    insert into tb values('张三' , '语文' , 74)
    insert into tb values('张三' , '数学' , 83)
    insert into tb values('张三' , '物理' , 93)
    insert into tb values('李四' , '语文' , 74)
    insert into tb values('李四' , '数学' , 84)
    insert into tb values('李四' , '物理' , 94)
    go--SQL SERVER 2000 静态SQL,指课程只有语文、数学、物理这三门课程。(以下同)
    select 姓名 as 姓名 ,
      max(case 课程 when '语文' then 分数 else 0 end) 语文,
      max(case 课程 when '数学' then 分数 else 0 end) 数学,
      max(case 课程 when '物理' then 分数 else 0 end) 物理
    from tb
    group by 姓名--SQL SERVER 2000 动态SQL,指课程不止语文、数学、物理这三门课程。(以下同)
    declare @sql varchar(8000)
    set @sql = 'select 姓名 '
    select @sql = @sql + ' , max(case 课程 when ''' + 课程 + ''' then 分数 else 0 end) [' + 课程 + ']'
    from (select distinct 课程 from tb) as a
    set @sql = @sql + ' from tb group by 姓名'
    exec(@sql) --SQL SERVER 2005 静态SQL。
    select * from (select * from tb) a pivot (max(分数) for 课程 in (语文,数学,物理)) b--SQL SERVER 2005 动态SQL。
    declare @sql varchar(8000)
    select @sql = isnull(@sql + '],[' , '') + 课程 from tb group by 课程
    set @sql = '[' + @sql + ']'
    exec ('select * from (select * from tb) a pivot (max(分数) for 课程 in (' + @sql + ')) b')---------------------------------
      

  2.   


    --好郁闷啊,行转列不会写了
    if object_id('tempdb..#','U') is not null
        drop table #;
    go
    create table #(id int, type int, [date] datetime, data int, xx int);
    insert into #
    select 1,16,'2010-4-3',3,0 union all
    select 1,16,'2010-4-4',4,1 union all
    select 2,48,'2010-4-3',3,0 union all
    select 2,48,'2010-4-4',2,1
    godeclare @firstdate char(8)
    declare @seconddate char(8)set @firstdate = '20100403'
    set @seconddate = '20100404'select 
        id, type
        ,[data1] = max(case when convert(char(8),[date],112) = @firstdate then isnull(data,0) else -9999 end)
        ,[data2] = max(case when convert(char(8),[date],112) = @seconddate then isnull(data,0) else -9999 end)
        ,[data]  = max(case when convert(char(8),[date],112) = @seconddate then isnull(data,0) else -9999 end)
                   - max(case when convert(char(8),[date],112) = @firstdate then isnull(data,0) else -9999 end)
        ,[xx1] = max(case when convert(char(8),[date],112) = @firstdate and xx = 0 then '正常' 
                          when convert(char(8),[date],112) = @firstdate and xx = 1 then '异常' 
                          else NULL
                     end)
        ,[xx2] = max(case when convert(char(8),[date],112) = @seconddate and xx = 0 then '正常' 
                          when convert(char(8),[date],112) = @seconddate and xx = 1 then '异常' 
                          else NULL
                     end)
    from 
        #
    group by
        id, type--结果
    /*
    (4 行受影响)
    id          type        data1       data2       data        xx1  xx2
    ----------- ----------- ----------- ----------- ----------- ---- ----
    1           16          3           4           1           正常   异常
    2           48          3           2           -1          正常   异常
    警告: 聚合或其它 SET 操作消除了空值。(2 行受影响)
    */