读书人

《转》sql server行转列函数的懂得

发布时间: 2013-10-24 18:27:24 作者: rapoo

《转》sql server行转列函数的理解

考料:使用 PIVOT 和 UNPIVOT http://technet.microsoft.com/zh-tw/library/ms177410.aspx

前言
T-SQL PIVOT的法看了好次,今天於看懂了到底在什了。把心得先下免得又忘。

PIVOT法:
先看一下法,如下:

SELECT <non-pivoted column>,
??? [first pivoted column] AS <column name>,
??? [second pivoted column] AS <column name>,
??? ...
??? [last pivoted column] AS <column name>
FROM
??? (<SELECT query that produces the data>)
?? AS <alias for the source query>
PIVOT
(
??? <aggregation function>(<column being aggregated>)
FOR
[<column that contains the values that will become column headers>]
??? IN ( [first pivoted column], [second pivoted column],
??? ... [last pivoted column])
) AS <alias for the pivot table>
<optional ORDER BY clause>;

老吧,第一眼、第二眼是看不懂。用案例反推照了好次,於看懂了。

PIVOT法剖析:

PIVOT的法分三,用三步使用。
第一步:先把要PIVOT的原始料查(Query)好。
第二步:定好PIVOT的位方式。
第三步:依PIVOT好了的料,呈果。

SELECT <non-pivoted column>,??? ---- 第三步在此,呈PIVOT後的料。
??? [first pivoted column] AS <column name>,
??? [second pivoted column] AS <column name>,
??? ...
??? [last pivoted column] AS <column name>
FROM
?? (<SELECT query that produces the data>) ---- 第一步在此,料(Query)。
?? AS <alias for the source query>
PIVOT ---- 第二步在此,依第一步的料位定PIVOT方式。
(
??? <aggregation function>(<column being aggregated>)
FOR
[<column that contains the values that will become column headers>]
??? IN ( [first pivoted column], [second pivoted column],
??? ... [last pivoted column])
) AS <alias for the pivot table>
<optional ORDER BY clause>;

用案例明:

案例一:

--## 一PIVOT?
目的:各(ldap_sts)的量。
select *? ---- 第三步:把PIVOT好的料直接呈出。
from
(
??? select [ldap_id], [ldap_sts] from ccldap?? -- 第一步:料。
? ? ? ? ? -- 只原料了位,PK位(ldap_id)位(ldap_sts)。
) S? -- 一定要有,不然法。
pivot
(
??? count([ldap_id]) -- 量
??? for [ldap_sts] in ([1],[2],[3],[4],[5],[6],[7]) ?-- 位[ldap_sts]的值[1][2]…[7]行算。
-- 注意:[1][2]…[7]是[ldap_sts]的值,以位表示法描述[ldap_sts]的值。
) P? -- 一定要有,不然法。

下面是行果:

1???????? 2???????? 3???????? 4???????? 5???????? 6???????? 7?? <---值
--------- --------- --------- --------- --------- --------- ---------
1???????? 12528???? 68519???? 120?????? 8???????? 5???????? 36? <---量

(1 料列受到影)

======================================================
# 案例二:

--## 二PIVOT
目的:不同用途(app_rsn_cod )下,各(ldap_sts)的量。
select * ?-- 第三步:把PIVOT好的料直接呈出。
from
(
??? select [ldap_id], [ldap_sts], [app_rsn_cod] from ccldap ? -- 第一步:料。
???????? -- 原料了三位,PK位(ldap_id)、位(ldap_sts)用途位(app_rsn_cod)。
) S ?-- 一定要有,不然法。
pivot
(
??? count([ldap_id])-- 量
??? for [ldap_sts] in ( [1],[2],[3],[4],[5],[6],[7]) ?-- 位[ldap_sts]的值[1][2]…[7]行算。
-- 注意:[1][2]…[7]是[ldap_sts]的值,以位表示法描述[ldap_sts]的值。
) P

下面是行果:

(用途)????? (1)?? (2)?? (3)?? (4)?? (5)?? (6)?? (7)??
app_rsn_cod 1???????? 2???????? 3???????? 4???????? 5???????? 6???????? 7
----------- --------- --------- --------- --------- --------- --------- ---------
NULL??????? 0???????? 12515???? 59676???? 0???????? 2???????? 0???????? 0
1?????????? 1???????? 10??????? 8104????? 1???????? 4???????? 5???????? 0
2?????????? 0???????? 3???????? 739?????? 119?????? 2???????? 0???????? 36

(3 料列受到影)

注意到了,在此例的第二步,未定用途位(app_rsn_cod),但在最後的PIVOT果料神奇的合(join)成希望到的效果。

读书人网 >SQL Server

热点推荐