Sql server® 2012 t-sql fundamentals


   Microsoft SQL Server 2012 T-SQL Fundamentals 6



Download 10,93 Mb.
Pdf ko'rish
bet206/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   202   203   204   205   206   207   208   209   ...   443
Bog'liq
BookSQL

154  

Microsoft SQL Server 2012 T-SQL Fundamentals



6

You can solve this problem by querying the Customers table and using EXISTS and NOT EXISTS predi-

cates with correlated subqueries to ensure that the customer placed orders in 2007 but not in 2008. 

The EXISTS predicate returns TRUE only if at least one row exists in the Orders table with the same 

customer ID as in the outer row, within the date range representing the year 2007. The NOT EXISTS 

predicate returns TRUE only if no row exists in the Orders table with the same customer ID as in the 

outer row, within the date range representing the year 2008. Here’s the complete solution query.

SELECT custid, companyname 

FROM Sales.Customers AS C 

WHERE EXISTS 

  (SELECT * 

   FROM Sales.Orders AS O 

   WHERE O.custid = C.custid 

     AND O.orderdate >= '20070101' 

     AND O.orderdate < '20080101') 

  AND NOT EXISTS 

  (SELECT * 

   FROM Sales.Orders AS O 

   WHERE O.custid = C.custid 

     AND O.orderdate >= '20080101' 

     AND O.orderdate < '20090101');

7

You can solve this exercise by nesting EXISTS predicates with correlated subqueries. You write the 

outermost query against the Customers table. In the WHERE clause of the outer query, you can use 

the EXISTS predicate with a correlated subquery against the Orders table to filter only the current 

customer’s orders. In the filter of the subquery against the Orders table, you can use a nested EXISTS 

predicate with a subquery against the OrderDetails table that filters only order details with product 

ID 12. This way, only customers who placed orders that contain product 12 in their order details are 

returned. Here’s the complete solution query.

SELECT custid, companyname 

FROM Sales.Customers AS C 

WHERE EXISTS 

  (SELECT * 

   FROM Sales.Orders AS O 

   WHERE O.custid = C.custid 

     AND EXISTS 

       (SELECT * 

        FROM Sales.OrderDetails AS OD 

        WHERE OD.orderid = O.orderid 

          AND OD.ProductID = 12));

www.it-ebooks.info





Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   202   203   204   205   206   207   208   209   ...   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