前言
未來(lái)的一個(gè)月時(shí)間中,會(huì)總結(jié)一系列SQL知識(shí)點(diǎn),一次只總結(jié)一個(gè)知識(shí)點(diǎn),盡量說(shuō)明白,下面來(lái)說(shuō)說(shuō)SQL 中常用Pivot 函數(shù)(這里是用的數(shù)據(jù)庫(kù)是SQLSERVER,與其他數(shù)據(jù)庫(kù)是類似的,大家放心看就好)
讓我們先從一個(gè)虛構(gòu)的場(chǎng)景中來(lái)著手吧
萬(wàn)國(guó)來(lái)朝,很多供應(yīng)商每天都匯報(bào)各自的收入情況。先來(lái)創(chuàng)建一個(gè)DailyIncome 表
create table DailyIncome(VendorId nvarchar(10), IncomeDay nvarchar(10), IncomeAmount int)
--VendorId 供應(yīng)商ID,
--IncomeDay 收入時(shí)間
--IncomeAmount 收入金額
緊接著來(lái)插入數(shù)據(jù)看看
(留意看下,有的供應(yīng)商某天中會(huì)有多次收入,應(yīng)該是分批進(jìn)賬的)
insert into DailyIncome values ('SPIKE', 'FRI', 100)
insert into DailyIncome values ('SPIKE', 'MON', 300)
insert into DailyIncome values ('FREDS', 'SUN', 400)
insert into DailyIncome values ('SPIKE', 'WED', 500)
insert into DailyIncome values ('SPIKE', 'TUE', 200)
insert into DailyIncome values ('JOHNS', 'WED', 900)
insert into DailyIncome values ('SPIKE', 'FRI', 100)
insert into DailyIncome values ('JOHNS', 'MON', 300)
insert into DailyIncome values ('SPIKE', 'SUN', 400)
insert into DailyIncome values ('JOHNS', 'FRI', 300)
insert into DailyIncome values ('FREDS', 'TUE', 500)
insert into DailyIncome values ('FREDS', 'TUE', 200)
insert into DailyIncome values ('SPIKE', 'MON', 900)
insert into DailyIncome values ('FREDS', 'FRI', 900)
insert into DailyIncome values ('FREDS', 'MON', 500)
insert into DailyIncome values ('JOHNS', 'SUN', 600)
insert into DailyIncome values ('SPIKE', 'FRI', 300)
insert into DailyIncome values ('SPIKE', 'WED', 500)
insert into DailyIncome values ('SPIKE', 'FRI', 300)
insert into DailyIncome values ('JOHNS', 'THU', 800)
insert into DailyIncome values ('JOHNS', 'SAT', 800)
insert into DailyIncome values ('SPIKE', 'TUE', 100)
insert into DailyIncome values ('SPIKE', 'THU', 300)
insert into DailyIncome values ('FREDS', 'WED', 500)
insert into DailyIncome values ('SPIKE', 'SAT', 100)
insert into DailyIncome values ('FREDS', 'SAT', 500)
insert into DailyIncome values ('FREDS', 'THU', 800)
insert into DailyIncome values ('JOHNS', 'TUE', 600)
讓我們先來(lái)看看前十行數(shù)據(jù):
select top 10 * from DailyIncome
如圖所示:
DailyIncome
雖然數(shù)據(jù)是能夠完全給展示了,但好像一眼望去不能得到對(duì)我們用處更大的信息,比如說(shuō)我們想得到每個(gè)供應(yīng)商的每天的總收入,這時(shí)我們應(yīng)該做一些數(shù)據(jù)形式的轉(zhuǎn)變了,平常的所用的是這樣的。
select VendorId ,
sum(case when IncomeDay='MoN' then IncomeAmount else 0 end) MON,
sum(case when IncomeDay='TUE' then IncomeAmount else 0 end) TUE,
sum(case when IncomeDay='WED' then IncomeAmount else 0 end) WED,
sum(case when IncomeDay='THU' then IncomeAmount else 0 end) THU,
sum(case when IncomeDay='FRI' then IncomeAmount else 0 end) FRI,
sum(case when IncomeDay='SAT' then IncomeAmount else 0 end) SAT,
sum(case when IncomeDay='SUN' then IncomeAmount else 0 end) SUN
from DailyIncome group by VendorId
得到如下的結(jié)果:
case when結(jié)果
如果大家仔細(xì)看結(jié)果的話,會(huì)有這樣的發(fā)現(xiàn),這是把VendorID進(jìn)行了分組,并且對(duì)于每組中IncomeDay這一列中的值都變成了新的列名字,然后對(duì)IncomeAmount進(jìn)行求和操作。
這樣寫可能是有些麻煩,別著急,我們用Pivot函數(shù)進(jìn)行行轉(zhuǎn)列試下。
select * from DailyIncome ----第一步
pivot
(
sum (IncomeAmount) ----第三步
for IncomeDay in ([MON],[TUE],[WED],[THU],[FRI],[SAT],[SUN]) ---第二步
) as AvgIncomePerDay
來(lái)解釋下,要想用好Pivot函數(shù),應(yīng)該理解代碼注釋中的這幾步。
第一步:肯定是要明白數(shù)據(jù)源了,這里是DailyIncome
第二步:要明白要想讓哪一列的值做新的列名字
第三步:要明白對(duì)于這新的列要求那些值呢?
下面有個(gè)練習(xí)題目,做之前不要看答案啊
問(wèn):對(duì)于SPIKE這家供應(yīng)商來(lái)說(shuō),每天最大的入賬金額。
select * from DailyIncome
pivot (max (IncomeAmount) for IncomeDay in ([MON],[TUE],[WED],[THU],[FRI],[SAT],[SUN])) as MaxIncomePerDay
where VendorId in ('SPIKE')
參考鏈接如下:
1.Pivot tables in SQL Server. A simple sample
2.行轉(zhuǎn)列:SQL SERVER PIVOT與用法解釋
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
您可能感興趣的文章:- Pandas透視表(pivot_table)詳解
- pandas pivot_table() 按日期分多列數(shù)據(jù)的方法
- C#實(shí)現(xiàn)Excel動(dòng)態(tài)生成PivotTable
- 行轉(zhuǎn)列之SQL SERVER PIVOT與用法詳解
- SQL知識(shí)點(diǎn)之列轉(zhuǎn)行Unpivot函數(shù)
- Pandas 重塑(stack)和軸向旋轉(zhuǎn)(pivot)的實(shí)現(xiàn)
- Python Pivot table透視表使用方法解析