Sql server® 2012 t-sql fundamentals



Download 10,93 Mb.
Pdf ko'rish
bet93/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   89   90   91   92   93   94   95   96   ...   443
Bog'liq
BookSQL

 

CHAPTER 2

 

Single-Table Queries



 

 49

This query generates the following output.

orderid     custid      val          rownum 

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

10702       1           330.00       1 

10952       1           471.20       2 

10643       1           814.50       3 

10835       1           845.80       4 

10692       1           878.00       5 

11011       1           933.50       6 

10308       2           88.80        1 

10759       2           320.00       2 

10625       2           479.75       3 

10926       2           514.40       4 

10682       3           375.50       1 

... 


 

(830 row(s) affected)

The ROW_NUMBER function assigns unique, sequential, incrementing integers to the rows in the 

result within the respective partition, based on the indicated ordering. The OVER clause in the exam-

ple function partitions the window by the custid attribute, hence the row numbers are unique to each 

customer. The OVER clause also defines ordering in the window by the val attribute, so the sequential 

row numbers are incremented within the partition based on val.

Note that the ROW_NUMBER function must produce unique values within each partition. This 

means that even when the ordering value doesn’t increase, the row number still must increase. There-

fore, if the ROW_NUMBER function’s ORDER BY list is non-unique, as in the preceding example, the 

query is nondeterministic. That is, more than one correct result is possible. If you want to make a row 

number calculation deterministic, you must add elements to the ORDER BY list to make it unique. 

For example, you can add the orderid attribute as a tiebreaker to the ORDER BY list to make the row 

number calculation deterministic.

As mentioned, the ORDER BY specified in the OVER clause should not be confused with presen-

tation and does not change the nature of the result from being relational. If you do not specify a 

presentation ORDER BY in the query, as explained earlier, you don’t have any guarantees in terms of 

the order of the rows in the output. If you need to guarantee presentation ordering, you have to add 

a presentation ORDER BY clause, as I did in the last query.

Note that expressions in the SELECT list are evaluated before the DISTINCT clause (if one exists). 

This applies to expressions based on window functions that appear in the SELECT list. I explain the 

significance of this in Chapter 7.

www.it-ebooks.info



50  

Microsoft SQL Server 2012 T-SQL Fundamentals

To put it all together, the following list presents the logical order in which all clauses discussed so 

far are processed: 



FROM





WHERE



GROUP BY





HAVING



SELECT



• 

Expressions

• 

DISTINCT



ORDER BY

• 

TOP / OFFSET-FETCH




Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   89   90   91   92   93   94   95   96   ...   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