Sql server® 2012 t-sql fundamentals


pivoting with the native T-SQL



Download 10,93 Mb.
Pdf ko'rish
bet274/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   270   271   272   273   274   275   276   277   ...   443
Bog'liq
BookSQL

pivoting with the native T-SQL 

PIVOT

 Operator

SQL Server supports a T-SQL–specific table operator called PIVOT. The PIVOT operator operates in 

the context of the FROM clause of a query like other table operators (for example, JOIN). It operates 

on a source table or table expression, pivots the data, and returns a result table. The PIVOT operator 

involves the same logical processing phases as described earlier (grouping, spreading, and aggregat-

ing) with the same pivoting elements, but it uses different, native syntax. 

www.it-ebooks.info



226  

Microsoft SQL Server 2012 T-SQL Fundamentals

The general form of a query with the PIVOT operator is shown here.

SELECT ... 

FROM  

  PIVOT((

        FOR  

          IN ()) AS  

...;

In the parentheses of the PIVOT operator, you specify the aggregate function (SUM, in this ex-



ample), aggregation element (qty), spreading element (custid), and the list of target column names (A, 

B, C, D). Following the parentheses of the PIVOT operator, you specify an alias for the result table. 

It is important to note that with the PIVOT operator, you do not explicitly specify the grouping ele-

ments, removing the need for GROUP BY in the query. The PIVOT operator figures out the grouping 

elements implicitly as all attributes from the source table (or table expression) that were not specified 

as either the spreading element or the aggregation element. You must ensure that the source table 

for the PIVOT operator has no attributes besides the grouping, spreading, and aggregation elements

so that after specifying the spreading and aggregation elements, the only attributes left are those 

you intend as grouping elements. You achieve this by not applying the PIVOT operator to the original 

table directly (Orders in this case), but instead to a table expression that includes only the attributes 

representing the pivoting elements and no others. For example, here’s the solution query to the origi-

nal pivoting request, using the native PIVOT operator.

SELECT empid, A, B, C, D 

FROM (SELECT empid, custid, qty 

      FROM dbo.Orders) AS D 

  PIVOT(SUM(qty) FOR custid IN(A, B, C, D)) AS P;

Instead of operating directly on the dbo.Orders table, the PIVOT operator operates on a derived 

table called D that includes only the pivoting elements empidcustid, and qty. When you account for 

the spreading element, which is custid, and the aggregation element, which is qty, what’s left is empid

which will be considered the grouping element.

This query returns the output shown earlier in Table 7-1.

To understand why you’re required to use a table expression here, consider the following query 

that applies the PIVOT operator directly to the dbo.Orders table.

SELECT empid, A, B, C, D 

FROM dbo.Orders 

  PIVOT(SUM(qty) FOR custid IN(A, B, C, D)) AS P;

The dbo.Orders table contains the attributes orderidorderdateempidcustid, and qty. Because the 

query specified custid as the spreading element and qty as the aggregation element, the remaining 

attributes (orderidorderdate, and empid) are all considered the grouping elements. This query, there-

fore, returns the following output.

www.it-ebooks.info




Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   270   271   272   273   274   275   276   277   ...   443




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish