简体   繁体   中英

Transpose Column to Row SQl one row

I have an sql query that is returning 12 columns with the percentage data. I would like to transpose the query to have 12 rows in one column and the % data in another column

What I have:

在此处输入图像描述

What I would like:

在此处输入图像描述

Tha nks

If your DBMS supports the UNPIVOT function, you could try this.

SELECT Proposal_source, [%]
from (select candidate_generator, 
             crm_srt_ui, 
             knock_on_effect, 
             matchmaker_mutator, 
             ml_models, 
             object_association, 
             script_manual_data, 
             unified_onboarding 
    from tableA) p
UNPIVOT
( [%] FOR Proposal_source in 
            (candidate_generator, 
             crm_srt_ui, 
             knock_on_effect, 
             matchmaker_mutator, 
             ml_models, 
             object_association, 
             script_manual_data, 
             unified_onboarding )
  ) as unpvt;

If it does not support UNPIVOT, Then you could try union all the columns together

SELECT 'unified_onboarding' as Proposal_source, unified_onboarding as '%' FROM tableA
UNION
SELECT 'script_manual_data' as Proposal_source, script_manual_data as '%' FROM tableA
UNION
SELECT 'matchmaker_mutator' as Proposal_source, matchmaker_mutator as '%' FROM tableA
UNION
SELECT 'ml_models' as Proposal_source, ml_models as '%' FROM tableA
UNION
SELECT 'candidate_generator' as Proposal_source, candidate_generator as '%' FROM tableA
UNION
SELECT 'knock_on_effect' as Proposal_source, knock_on_effect as '%' FROM tableA
UNION
SELECT 'crm_srt_ui' as Proposal_source, crm_srt_ui as '%' FROM tableA
UNION
SELECT 'object_association' as Proposal_source, object_association as '%' FROM tableA

db fiddle

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