Sql server® 2012 t-sql fundamentals



Download 10,93 Mb.
Pdf ko'rish
bet212/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   208   209   210   211   212   213   214   215   ...   443
Bog'liq
BookSQL

LISTING 5-1

  Query with a Derived Table Using Inline Aliasing Form

SELECT orderyear, COUNT(DISTINCT custid) AS numcusts 

FROM (SELECT YEAR(orderdate) AS orderyear, custid 

      FROM Sales.Orders) AS D 

GROUP BY orderyear;

This query returns the following output.

orderyear   numcusts 

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

2006        67 

2007        86 

2008        81

www.it-ebooks.info



160  

Microsoft SQL Server 2012 T-SQL Fundamentals

This code defines a derived table called D based on a query against the Orders table that returns 

the order year and customer ID from all rows. The SELECT list of the inner query uses the inline alias-

ing form to assign the alias orderyear to the expression YEAR(orderdate). The outer query can refer 

to the orderyear column alias in both the GROUP BY and SELECT clauses, because as far as the outer 

query is concerned, it queries a table called D with columns called orderyear and custid.

As I mentioned earlier, SQL Server expands the definition of the table expression and accesses the 

underlying objects directly. After expansion, the query in Listing 5-1 looks like the following.

SELECT YEAR(orderdate) AS orderyear, COUNT(DISTINCT custid) AS numcusts 

FROM Sales.Orders 

GROUP BY YEAR(orderdate);

This is just to emphasize that you use table expressions for logical (not performance-related) rea-

sons. Generally speaking, table expressions have neither positive nor negative performance impact.

The code in Listing 5-1 uses the inline aliasing form to assign column aliases to expressions. The 

syntax for inline aliasing is  [AS] . Note that the word AS is optional in the syntax 

for inline aliasing; however, I find that it helps the readability of the code and recommend using it.

In some cases, you might prefer to use a second supported form for assigning column aliases, 

which you can think of as an external form. With this form, you do not assign column aliases following 

the expressions in the SELECT list—you specify all target column names in parentheses following the 

table expression’s name, like the following.

SELECT orderyear, COUNT(DISTINCT custid) AS numcusts 

FROM (SELECT YEAR(orderdate), custid 

      FROM Sales.Orders) AS D(orderyear, custid) 

GROUP BY orderyear;

It is generally recommended that you use the inline form for a couple of reasons. If you need to 

debug the code when using the inline form, when you highlight the query defining the table expres-

sion and run it, the columns in the result appear with the aliases you assigned. With the external form, 

you cannot include the target column names when you highlight the table expression query, so the 

result appears with no column names in the case of the unnamed expressions. Also, when the table 

expression query is lengthy, using the external form can make it quite difficult to figure out which 

column alias belongs to which expression.

Even though it’s a best practice to use the inline aliasing form, in some cases you may find the ex-

ternal form more convenient to work with. For example, when the query defining the table expression 

isn’t going to undergo any further revisions and you want to treat it like a “black box”—that is, you 

want to focus your attention on the table expression name followed by the target column list when 

you look at the outer query. To use terminology from traditional programming languages, it allows 

you to specify a contract interface between the outer query and the table expression.

www.it-ebooks.info




Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   208   209   210   211   212   213   214   215   ...   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