主頁 > 知識庫 > mssql 數(shù)據(jù)庫表行轉(zhuǎn)列,列轉(zhuǎn)行終極方案

mssql 數(shù)據(jù)庫表行轉(zhuǎn)列,列轉(zhuǎn)行終極方案

熱門標(biāo)簽:小e電話機器人 一個導(dǎo)航軟件能用幾個地圖標(biāo)注點 百度地圖標(biāo)注改顏色 臨沂智能電銷機器人加盟哪家好 貴州房產(chǎn)智能外呼系統(tǒng)供應(yīng)商 鎮(zhèn)江網(wǎng)路外呼系統(tǒng)供應(yīng)商 電銷外呼有錄音系統(tǒng)有哪些 申請400電話在哪辦理流程 外呼運營商線路收費
復(fù)制代碼 代碼如下:

--行轉(zhuǎn)列問題
--建立測試環(huán)境
Create Table TEST
(DATES Varchar(6),
EMPNO Varchar(5),
STYPE Varchar(1),
AMOUNT Int)
--插入數(shù)據(jù)
Insert TEST Select '200605', '02436', 'A', 5
Union All Select '200605', '02436', 'B', 3
Union All Select '200605', '02436', 'C', 3
Union All Select '200605', '02436', 'D', 2
Union All Select '200605', '02436', 'E', 9
Union All Select '200605', '02436', 'F', 7
Union All Select '200605', '02436', 'G', 6
Union All Select '200605', '02438', 'A', 7
Union All Select '200605', '02438', 'B', 8
Union All Select '200605', '02438', 'C', 0
Union All Select '200605', '02438', 'D', 3
Union All Select '200605', '02438', 'E', 4
Union All Select '200605', '02438', 'F', 5
Union All Select '200605', '02438', 'G', 1
GO
--測試
--如果STYPE固定,可以這麼寫
Select
DATES,
EMPNO,
SUM(Case STYPE When 'A' Then AMOUNT Else 0 End) As A,
SUM(Case STYPE When 'B' Then AMOUNT Else 0 End) As B,
SUM(Case STYPE When 'C' Then AMOUNT Else 0 End) As C,
SUM(Case STYPE When 'D' Then AMOUNT Else 0 End) As D,
SUM(Case STYPE When 'E' Then AMOUNT Else 0 End) As E,
SUM(Case STYPE When 'F' Then AMOUNT Else 0 End) As F,
SUM(Case STYPE When 'G' Then AMOUNT Else 0 End) As G
From TEST
Group By DATES,EMPNO
Order By DATES,EMPNO

--如果STYPE不固定,用動態(tài)語句
Declare @S Varchar(1000)
Set @S=''
Select @S=@S+',SUM(Case STYPE When '''+STYPE+''' Then AMOUNT Else 0 End) As '+STYPE From (Select Distinct STYPE From TEST) A Order By STYPE
Set @S='Select DATES,EMPNO'+@S+' From TEST Group By DATES,EMPNO Order By DATES,EMPNO'
EXEC(@S)
GO
--如果被轉(zhuǎn)置的是數(shù)字類型的話,應(yīng)用下列語句
DECLARE @S VARCHAR(1000)
SET @S='SELECT DATES,EMPNO '
SELECT @S=@S+',['+STYPE+']=SUM(CASE WHEN STYPE='''+STYPE+''' THEN AMOUNT ELSE 0 END)'
FROM (Select Distinct STYPE From TEST) A Order By STYPE
SET @S=@S+' FROM TEST GROUP BY DATES,EMPNO'
EXEC(@S)

如果是列轉(zhuǎn)行的話直接Union All就可以了

例如 :

city style color 46 48 50 52
長沙 S6MF01002 152 1 2 2 1
長沙 S6MF01002 201 1 2 2 1
上面到下面的樣子
city style color size qty
長沙 S6MF01002 152 46 1
長沙 S6MF01002 152 48 2
長沙 S6MF01002 152 50 2
長沙 S6MF01002 152 52 1
長沙 S6MF01002 201 46 1
長沙 S6MF01002 201 48 2
長沙 S6MF01002 201 50 2
長沙 S6MF01002 201 52 1

Select City,Style,Color,[46] From Test
Union all
Select City,Style,Color,[48] From Test
Union all
Select City,Style,Color,[50] From Test
Union all
Select City,Style,Color,[52] From Test

就可以了
您可能感興趣的文章:
  • mysql 行轉(zhuǎn)列和列轉(zhuǎn)行實例詳解
  • sql語句實現(xiàn)行轉(zhuǎn)列的3種方法實例
  • SQLServer行轉(zhuǎn)列實現(xiàn)思路記錄
  • MySQL存儲過程中使用動態(tài)行轉(zhuǎn)列
  • Sql Server 2000 行轉(zhuǎn)列的實現(xiàn)(橫排)
  • SQL查詢語句行轉(zhuǎn)列橫向顯示實例解析
  • sql動態(tài)行轉(zhuǎn)列的兩種方法
  • table 行轉(zhuǎn)列的sql詳解
  • SQL行轉(zhuǎn)列和列轉(zhuǎn)行代碼詳解
  • SQL基礎(chǔ)教程之行轉(zhuǎn)列Pivot函數(shù)

標(biāo)簽:澳門 晉城 延邊 日照 合肥 保定 嘉興 三明

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《mssql 數(shù)據(jù)庫表行轉(zhuǎn)列,列轉(zhuǎn)行終極方案》,本文關(guān)鍵詞  mssql,數(shù)據(jù)庫,表行轉(zhuǎn),列,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《mssql 數(shù)據(jù)庫表行轉(zhuǎn)列,列轉(zhuǎn)行終極方案》相關(guān)的同類信息!
  • 本頁收集關(guān)于mssql 數(shù)據(jù)庫表行轉(zhuǎn)列,列轉(zhuǎn)行終極方案的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章