第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

TSQL PIVOT多列

TSQL PIVOT多列

智慧大石 2019-12-03 14:19:56
我有下表,但不確定是否可以旋轉(zhuǎn)它并保留所有標(biāo)簽。RATIO               RESULT   SCORE   GRADECurrent Ratio       1.294    60      GoodGearing Ratio       0.3384   70      GoodPerformance Ratio   0.0427   50      SatisfactoryTOTAL               NULL     180     Good我會(huì)承認(rèn)對(duì)樞軸的使用不是很好,因此經(jīng)過(guò)幾次嘗試導(dǎo)致此輸出:SELECT 'RESULT' AS 'Ratio'  ,[Current Ratio] AS 'Current Ratio'  ,[Gearing Ratio] AS 'Gearing Ratio'  ,[Performance Ratio] AS 'Performance Ratio'  ,[TOTAL] AS 'TOTAL'FROM(  SELECT RATIO, RESULT   FROM GRAND_TOTALS) AS SRECPIVOT (  MAX(RESULT)   FOR RATIO IN ([Current Ratio],[Gearing Ratio], [Performance Ratio], [TOTAL])) AS PVT結(jié)果如下:Ratio    Current Ratio   Gearing Ratio   Performance RatioResult   1.294           0.3384          0.0427我承認(rèn)在下一步該怎么做才能產(chǎn)生所需的結(jié)果時(shí)感到很沮喪:Ratio    Current Ratio   Gearing Ratio   Performance Ratio   TOTALResult   1.294           0.3384          0.0427              NULLScore    60              70              50                  180Grade    Good            Good            Satisfactory        Good
查看完整描述

1 回答

?
蝴蝶不菲

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

由于要轉(zhuǎn)動(dòng)多列數(shù)據(jù),我會(huì)首先建議unpivoting的result,score并且grade列,所以你不必多列,但你將有多個(gè)行。


根據(jù)您的SQL Server版本,您可以使用UNPIVOT函數(shù)或CROSS APPLY。取消數(shù)據(jù)透視的語(yǔ)法類(lèi)似于:


select ratio, col, value

from GRAND_TOTALS

cross apply

(

  select 'result', cast(result as varchar(10)) union all

  select 'score', cast(score as varchar(10)) union all

  select 'grade', grade

) c(col, value)

請(qǐng)參閱帶有演示的SQL Fiddle。一旦數(shù)據(jù)被取消透視,就可以應(yīng)用PIVOT功能:


select ratio = col,

  [current ratio], [gearing ratio], [performance ratio], total

from

(

  select ratio, col, value

  from GRAND_TOTALS

  cross apply

  (

    select 'result', cast(result as varchar(10)) union all

    select 'score', cast(score as varchar(10)) union all

    select 'grade', grade

  ) c(col, value)

) d

pivot

(

  max(value)

  for ratio in ([current ratio], [gearing ratio], [performance ratio], total)

) piv;

請(qǐng)參閱帶有演示的SQL Fiddle。這將為您提供結(jié)果:


|  RATIO | CURRENT RATIO | GEARING RATIO | PERFORMANCE RATIO |     TOTAL |

|--------|---------------|---------------|-------------------|-----------|

|  grade |          Good |          Good |      Satisfactory |      Good |

| result |       1.29400 |       0.33840 |           0.04270 |    (null) |

|  score |      60.00000 |      70.00000 |          50.00000 | 180.00000 |


查看完整回答
反對(duì) 回復(fù) 2019-12-03
  • 1 回答
  • 0 關(guān)注
  • 676 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)