简体   繁体   中英

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

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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