简体   繁体   English

选择行作为Sql Server中的列

[英]Select Rows as Columns in Sql Server

I have following table: 我有下表:

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

I need to create select query which gives result in this manner: 我需要创建以这种方式给出结果的选择查询:

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

Any ideas? 有任何想法吗?

Try this: 尝试这个:

Edit Check this out for more SQLFiddle 编辑 出更多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;

SQLFiddle Demo SQLFiddle演示

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM