Sql server® 2012 t-sql fundamentals



Download 10,93 Mb.
Pdf ko'rish
bet197/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   193   194   195   196   197   198   199   200   ...   443
Bog'liq
BookSQL

Using running aggregates

Running aggregates are aggregates that accumulate values over time. In this section, I use the 

Sales.OrderTotalsByYear view to demonstrate the technique for calculating running aggregates. 

The view shows total order quantities by year. Query the view to examine its contents.

SELECT orderyear, qty 

FROM Sales.OrderTotalsByYear;

You get the following output.

orderyear   qty 

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

2007        25489 

2008        16247 

2006        9581

Suppose you need to return for each year the order year, quantity, and running total quantity over 

the years. That is, for each year, return the sum of the quantity up to that year. So for the earliest year 

recorded in the view (2006), the running total is equal to that year’s quantity. For the second year 

(2007), the running total is the sum of the first year plus the second year, and so on.

You can complete this task by querying one instance of the view (call it O1) to return for each year 

the order year and quantity, and then by using a correlated subquery against a second instance of 

the view (call it O2) to calculate the running-total quantity. The subquery should filter all years in O2 

www.it-ebooks.info




142  

Microsoft SQL Server 2012 T-SQL Fundamentals

that are smaller than or equal to the current year in O1, and sum the quantities from O2. Here’s the 

solution query.

SELECT orderyear, qty, 

  (SELECT SUM(O2.qty) 

   FROM Sales.OrderTotalsByYear AS O2 

   WHERE O2.orderyear <= O1.orderyear) AS runqty 

FROM Sales.OrderTotalsByYear AS O1 

ORDER BY orderyear;

This query returns the following output.

orderyear   qty         runqty 

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

2006        9581        9581 

2007        25489       35070 

2008        16247       51317

Note that SQL Server 2012 enhances the capabilities of window aggregate functions, allowing new, 

highly efficient solutions for running totals needs. As mentioned, I will discuss window functions in 

Chapter 7.


Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   193   194   195   196   197   198   199   200   ...   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