Sql server® 2012 t-sql fundamentals


   Microsoft SQL Server 2012 T-SQL Fundamentals aggregate Window Functions



Download 10,93 Mb.
Pdf ko'rish
bet268/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   264   265   266   267   268   269   270   271   ...   443
Bog'liq
BookSQL

220  

Microsoft SQL Server 2012 T-SQL Fundamentals



aggregate Window Functions

Prior to SQL Server 2012, window aggregate functions supported only a window partition clause. 

In SQL Server 2012, they also support window order and frame clauses, advancing their usefulness 

dramatically.

I’ll start with an example that doesn’t involve ordering and framing. Recall that using an OVER 

clause with empty parentheses exposes a window of all rows from the underlying query’s result 

set to the function. So, for example, SUM(val) OVER() returns the grand total of all values. If you 

do add a window partition clause, you expose a restricted window to the function, with only those 

rows from the underlying query’s result set that share the same values in the partitioning elements 

as in the current row. So, for example, SUM(val) OVER(PARTITION BY custid) returns the total values 

for the current customer.

Here’s a query against OrderValues that returns, along with each order, the grand total of all order 

values, as well as the customer total.

SELECT orderid, custid, val, 

  SUM(val) OVER() AS totalvalue, 

  SUM(val) OVER(PARTITION BY custid) AS custtotalvalue 

FROM Sales.OrderValues;

This query returns the following output, shown here in abbreviated form.

orderid     custid      val          totalvalue       custtotalvalue 

----------- ----------- ------------ ---------------- --------------- 

10643       1           814.50       1265793.22       4273.00 

10692       1           878.00       1265793.22       4273.00 

10702       1           330.00       1265793.22       4273.00 

10835       1           845.80       1265793.22       4273.00 

10952       1           471.20       1265793.22       4273.00 

11011       1           933.50       1265793.22       4273.00 

10926       2           514.40       1265793.22       1402.95 

10759       2           320.00       1265793.22       1402.95 

10625       2           479.75       1265793.22       1402.95 

10308       2           88.80        1265793.22       1402.95 

10365       3           403.20       1265793.22       7023.98 

... 


 

(830 row(s) affected)

The totalvalue column shows, for each row, the total value calculated for all rows. The column 

custtotalvalue has the total value for all rows that have the same custid value as in the current row.

As mentioned, one of the great advantages of window functions is that by enabling you to return 

detail elements and aggregate them in the same row, they also enable you to write expressions that 

mix detail and aggregates. For example, the following query calculates for each row the percentage 

that the current value is of the grand total, and also the percentage that the current value is of the 

customer total.

www.it-ebooks.info




Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   264   265   266   267   268   269   270   271   ...   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