Sql server® 2012 t-sql fundamentals



Download 10,93 Mb.
Pdf ko'rish
bet190/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   186   187   188   189   190   191   192   193   ...   443
Bog'liq
BookSQL

 

CHAPTER 4

 

Subqueries



 

 135

I describe the INSERT statement in more detail in Chapter 8, “Data Modification,” so don’t worry if 

you’re not familiar with it yet. 

The task at hand is to return all individual order IDs that are missing between the minimum and 

maximum in the table. It can be quite complicated to solve this problem with a query without any 

helper tables. You might find the Nums table introduced in Chapter 3, “Joins,” very useful here. Re-

member that the Nums table contains a sequence of integers, starting with 1, with no gaps. To return 

all missing order IDs from the Orders table, query the Nums table and filter only numbers that are 

between the minimum and maximum in the dbo.Orders table and that do not appear in the set of 

order IDs in the Orders table. You can use scalar self-contained subqueries to return the minimum and 

maximum order IDs and a multivalued self-contained subquery to return the set of all existing order 

IDs. Here’s the complete solution query.

SELECT n 

FROM dbo.Nums 

WHERE n BETWEEN (SELECT MIN(O.orderid) FROM dbo.Orders AS O) 

            AND (SELECT MAX(O.orderid) FROM dbo.Orders AS O) 

  AND n NOT IN (SELECT O.orderid FROM dbo.Orders AS O);

Because the code that populated the dbo.Orders table filtered only even-numbered order IDs, this 

query returns all odd-numbered values between the minimum and maximum order IDs in the Orders 

table. The output of this query is shown here in abbreviated form.

----------- 



10249 

10251 


10253 

10255 


10257 

... 


11067 

11069 


11071 

11073 


11075 

 

(414 row(s) affected)



When you’re done, run the following code for cleanup.

DROP TABLE dbo.Orders;

www.it-ebooks.info




Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   186   187   188   189   190   191   192   193   ...   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