亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

SQLServer:列到行

SQLServer:列到行

慕仙森 2019-06-25 14:38:35
SQLServer:列到行尋找優雅的(或任何)解決方案,將列轉換為行。下面是一個示例:我有一個具有以下模式的表:[ID] [EntityID] [Indicator1] [Indicator2] [Indicator3] ... [Indicator150]以下是我想得到的結果:[ID] [EntityId] [IndicatorName] [IndicatorValue]其結果值為:1 1 'Indicator1' 'Value of Indicator 1 for entity 1'2 1 'Indicator2' 'Value of Indicator 2 for entity 1'3 1 'Indicator3' 'Value of Indicator  3 for entity 1'4 2 'Indicator1' 'Value of Indicator 1 for entity 2'等等。這有道理嗎?對于在哪里查找和如何在T-SQL中完成任務,您有什么建議嗎?
查看完整描述

3 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

為了幫助新的讀者,我創造了一個例子來更好地理解@藍圖對UNPIVOT的回答。

 SELECT id        ,entityId        ,indicatorname        ,indicatorvalue  FROM (VALUES
        (1, 1, 'Value of Indicator 1 for entity 1', 'Value of Indicator 2 for entity 1', 'Value of Indicator 3 for entity 1'),
        (2, 1, 'Value of Indicator 1 for entity 2', 'Value of Indicator 2 for entity 2', 'Value of Indicator 3 for entity 2'),
        (3, 1, 'Value of Indicator 1 for entity 3', 'Value of Indicator 2 for entity 3', 'Value of Indicator 3 for entity 3'),
        (4, 2, 'Value of Indicator 1 for entity 4', 'Value of Indicator 2 for entity 4', 'Value of Indicator 3 for entity 4')
       ) AS Category(ID, EntityId, Indicator1, Indicator2, Indicator3)UNPIVOT(
    indicatorvalue    FOR indicatorname IN (Indicator1, Indicator2, Indicator3)) UNPIV;


查看完整回答
反對 回復 2019-06-25
  • 3 回答
  • 0 關注
  • 499 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號