Sql server® 2012 t-sql fundamentals



Download 10,93 Mb.
Pdf ko'rish
bet271/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   267   268   269   270   271   272   273   274   ...   443
Bog'liq
BookSQL

 

CHAPTER 7

 

Beyond the Fundamentals of Querying



 

 223

INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty) 

VALUES 

  (30001, ‘20070802’, 3, ‘A’, 10), 

  (10001, ‘20071224’, 2, ‘A’, 12), 

  (10005, ‘20071224’, 1, ‘B’, 20), 

  (40001, ‘20080109’, 2, ‘A’, 40), 

  (10006, ‘20080118’, 1, ‘C’, 14), 

  (20001, ‘20080212’, 2, ‘B’, 12), 

  (40005, ‘20090212’, 3, ‘A’, 10), 

  (20002, ‘20090216’, 1, ‘C’, 20), 

  (30003, ‘20090418’, 2, ‘B’, 15), 

  (30004, ‘20070418’, 3, ‘C’, 22), 

  (30007, ‘20090907’, 3, ‘D’, 30); 

 

SELECT * FROM dbo.Orders;



The query at the end of the code in Listing 7-1 produces the following output showing the 

contents of the dbo.Orders table.

orderid     orderdate   empid          custid    qty 

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

10001       2007-12-24  2              A         12 

10005       2007-12-24  1              B         20 

10006       2008-01-18  1              C         14 

20001       2008-02-12  2              B         12 

20002       2009-02-16  1              C         20 

30001       2007-08-02  3              A         10 

30003       2009-04-18  2              B         15 

30004       2007-04-18  3              C         22 

30007       2009-09-07  3              D         30 

40001       2008-01-09  2              A         40 

40005       2009-02-12  3              A         10

Before I further explain what pivoting is, consider a request to produce a report with the total 

order quantity for each employee and customer. The request is satisfied with the following simple 

query.


SELECT empid, custid, SUM(qty) AS sumqty 

FROM dbo.Orders 

GROUP BY empid, custid;

This query generates the following output.

empid       custid    sumqty 

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

2           A         52 

3           A         20 

1           B         20 

2           B         27 

1           C         34 

3           C         22 

3           D         30

www.it-ebooks.info




224  

Microsoft SQL Server 2012 T-SQL Fundamentals

Suppose, however, that you have a requirement to produce the output in the form shown in 

Table 7-1.




Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   267   268   269   270   271   272   273   274   ...   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