Sql server® 2012 t-sql fundamentals



Download 10,93 Mb.
Pdf ko'rish
bet173/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   169   170   171   172   173   174   175   176   ...   443
Bog'liq
BookSQL

 

CHAPTER 3

 

Joins


 

 119

custid      numorders 

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

1           6 

2           4 

3           7 

4           13 

5           18 

... 

22          1 



... 

57          1 

... 

87          15 



88          9 

89          14 

90          7 

91          7 

 

(91 row(s) affected)



The COUNT(*) aggregate function cannot detect whether a row really represents an order. To fix 

the problem, you should use COUNT() instead of COUNT(*), and provide a column from 

the nonpreserved side of the join. This way, the COUNT() aggregate ignores outer rows because they 

have a NULL in that column. Remember to use a column that can only be NULL, in case the row is an 

outer row—for example, the primary key column orderid.

SELECT C.custid, COUNT(O.orderid) AS numorders 

FROM Sales.Customers AS C 

  LEFT OUTER JOIN Sales.Orders AS O 

    ON C.custid = O.custid 

GROUP BY C.custid;

Notice in the output shown here in abbreviated form that the customers 22 and 57 now show up 

with a count of 0.

custid      numorders 

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

1           6 

2           4 

3           7 

4           13 

5           18 

... 


22          0 

... 


57          0 

... 


87          15 

88          9 

89          14 

90          7 

91          7 

 

(91 row(s) affected)



www.it-ebooks.info



Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   169   170   171   172   173   174   175   176   ...   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