当前位置: 代码迷 >> Sql Server >> 在此
  详细解决方案

在此

热度:318   发布时间:2016-04-24 19:47:50.0
在此求救
今天得交差

型号  年 月 日 准确率 缺到率 可用率
thd 2013 1 1  56   23    34
thd 2013 1 1  66   77    54
thd 2013 1 1  78   55    77
hhh 2012 9 18 89   55    23
hhn 2012 9 18 33   37    45
hhn 2012 9 18 67   56    12

上面的数据 怎样变成下面这样
即怎样将同一天同一型号的数据在一行显示

型号  年 月 日 准确率 缺到率 可用率 准确率 缺到率 可用率 准确率 缺到率 可用率
thd 2013 1 1  56   23    34    66   77    54     78   55    77
hhh 2012 9 18 89   55    23    33   37    45     67   56    12
拜托大家帮忙想想

------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:

今天得交差

型号  年 月 日 准确率 缺到率 可用率
thd 2013 1 1  56   23    34
thd 2013 1 1  66   77    54
thd 2013 1 1  78   55    77
hhh 2012 9 18 89   55    23
hhn 2012 9 18 33   37    45
hhn 2012 9 18 67   56    12

上面的数据 怎样变成下面这样
即怎样将同一天同一型号的数据在一行显示

型号  年 月 日 准确率 缺到率 可用率 准确率 缺到率 可用率 准确率 缺到率 可用率
thd 2013 1 1  56   23    34    66   77    54     78   55    77
hhh 2012 9 18 89   55    23    33   37    45     67   56    12
拜托大家帮忙想想


你的数据库是2000,还是2005呢?
安装的2005 


好了,你看看是这样吗:

drop table tb
go

create table tb(
型号 varchar(20),年 int, 月 int, 日 int, 
准确率 int, 缺到率 int,可用率 int
)

insert into tb
select 'thd' ,2013, 1, 1 , 56 ,  23  ,  34
union all select 'thd', 2013 ,1 ,1  ,66   ,77    ,54
union all select 'thd', 2013 ,1 ,1  ,78   ,55    ,77
union all select 'hhh', 2012 ,9 ,18 ,89   ,55    ,23
union all select 'hhh', 2012 ,9 ,18 ,33   ,37    ,45
union all select 'hhh', 2012 ,9 ,18 ,67   ,56    ,12
go


declare @sql nvarchar(max);

set @sql = '';

;with t
as
(
select *,
       ROW_NUMBER() over(partition by 型号,年,月,日 order by @@servername) as rownum
from tb
)

select 
       @sql = @sql + ',max(case when rownum = '+cast(rownum as varchar)+' then 准确率 else null end) as 准确率' +
                     ',max(case when rownum = '+cast(rownum as varchar)+' then 缺到率 else null end) as 缺到率' +
                     ',max(case when rownum = '+cast(rownum as varchar)+' then 可用率 else null end) as 可用率'
from t
group by rownum


select @sql = 'select 型号,年,月,日' + @sql + 
  相关解决方案