饿虎岗资源网 Design By www.oxmxm.com
复制代码 代码如下:
--行转列问题
--建立測試環境
Create Table TEST
(DATES Varchar(6),
EMPNO Varchar(5),
STYPE Varchar(1),
AMOUNT Int)
--插入數據
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不固定,用動態語句
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
--如果被转置的是数字类型的话,应用下列语句
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)
如果是列转行的话直接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
就可以了
--行转列问题
--建立測試環境
Create Table TEST
(DATES Varchar(6),
EMPNO Varchar(5),
STYPE Varchar(1),
AMOUNT Int)
--插入數據
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不固定,用動態語句
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
--如果被转置的是数字类型的话,应用下列语句
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)
如果是列转行的话直接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
就可以了
饿虎岗资源网 Design By www.oxmxm.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
饿虎岗资源网 Design By www.oxmxm.com
暂无评论...
更新日志
2024年11月18日
2024年11月18日
- 胡歌.2006-珍惜(EP)【步升大风】【FLAC分轨】
- 洪荣宏.2014-拼乎自己看【华特】【WAV+CUE】
- 伊能静.1999-从脆弱到勇敢1987-1996精选2CD【华纳】【WAV+CUE】
- 刘亮鹭《汽车DJ玩主》[WAV+CUE][1.1G]
- 张杰《最接近天堂的地方》天娱传媒[WAV+CUE][1.1G]
- 群星《2022年度发烧天碟》无损黑胶碟 2CD[WAV+CUE][1.4G]
- 罗文1983-罗文甄妮-射雕英雄传(纯银AMCD)[WAV+CUE]
- 群星《亚洲故事香港纯弦》雨果UPMAGCD2024[低速原抓WAV+CUE]
- 群星《经典咏流传》限量1:1母盘直刻[低速原抓WAV+CUE]
- 庾澄庆1993《老实情歌》福茂唱片[WAV+CUE][1G]
- 许巍《在别处》美卡首版[WAV+CUE][1G]
- 林子祥《单手拍掌》华纳香港版[WAV+CUE][1G]
- 郑秀文.1997-我们的主题曲【华纳】【WAV+CUE】
- 群星.2001-生命因爱动听电影原创音乐AVCD【MEDIA】【WAV+CUE】
- 林志颖.1994-别了晴雨的回忆【飞碟】【WAV+CUE】