Sql server® 2012 t-sql fundamentals


Multiple references in CTes



Download 10,93 Mb.
Pdf ko'rish
bet219/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   215   216   217   218   219   220   221   222   ...   443
Bog'liq
BookSQL

Multiple references in CTes

The fact that a CTE is defined first and then queried has another advantage: As far as the FROM clause 

of the outer query is concerned, the CTE already exists; therefore, you can refer to multiple instances 

of the same CTE. For example, the following code is the logical equivalent of the code shown earlier in 

Listing 5-3, using CTEs instead of derived tables.

WITH YearlyCount AS 

  SELECT YEAR(orderdate) AS orderyear, 



    COUNT(DISTINCT custid) AS numcusts 

  FROM Sales.Orders 

  GROUP BY YEAR(orderdate) 

SELECT Cur.orderyear,  



  Cur.numcusts AS curnumcusts, Prv.numcusts AS prvnumcusts, 

  Cur.numcusts - Prv.numcusts AS growth 

FROM YearlyCount AS Cur 

  LEFT OUTER JOIN YearlyCount AS Prv 

    ON Cur.orderyear = Prv.orderyear + 1; 

As you can see, the CTE YearlyCount is defined once and accessed twice in the FROM clause of the 

outer query—once as Cur and once as Prv. You need to maintain only one copy of the CTE query and 

not multiple copies as you would with derived tables. This leads to a query that is much clearer and 

easier to follow, and therefore less prone to errors.

If you’re curious about performance, recall that earlier I mentioned that table expressions typically 

have no performance impact because they are not physically materialized anywhere. Both refer-

ences to the CTE in the previous query are going to be expanded. Internally, this query has a self 

join between two instances of the Orders table, each of which involves scanning the table data and 

aggregating it before the join—the same physical processing that takes place with the derived table 

approach. If the work done per reference is very expensive and you want to avoid doing it multiple 

times, you should persist the inner query’s result in a temporary table or a table variable. My focus in 

this discussion is on coding aspects and not performance, and clearly the ability to specify the inner 

query only once, and refer to the CTE name multiple times, is a great benefit over the counterpart 

that uses derived tables.

recursive CTes

This section is optional because it covers subjects that are beyond the fundamentals. 

www.it-ebooks.info




Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   215   216   217   218   219   220   221   222   ...   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