在Sql Server中选择Rows作为列

时间:2013-12-03 07:01:13

标签: sql sql-server sql-server-2008 pivot

我有以下表格:

id  configname      value
--------------------------------------
1   set_count       3         
2   pass_ratio      2         
3   min_right_count     1         
.
.
.

我需要创建选择查询,以这种方式给出结果:

set_one_count   pass_ratio  min_right_count
--------------------------------------------------------
3                    2               1

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

试试这个:

修改检查一下以获取更多SQLFiddle

select set_ratio,pass_ratio,min_right_count
from
(
  select configname,value
  from yourtable
) d
pivot
(
  max(value)
  for configname in (set_ratio,pass_ratio,min_right_count)
) piv;

答案 1 :(得分:0)

SQLFiddle Demo

select 
sum(case when configname='set_count' then 1 else 0 end) as set_one_count,
sum(case when configname='pass_ratio' then 1 else 0 end) as set_one_count,
sum(case when configname='min_right_count' then 1 else 0 end) as min_right_count
from tablename;