Using calculations within aggregate functions. : Aggregate function « Aggregate Functions « SQL Server / T-SQL Tutorial

SQL Server / T-SQL Tutorial
1. Query
2. Insert Delete Update
3. Table
4. Table Join
5. Data Types
6. Set Operations
7. Constraints
8. Subquery
9. Aggregate Functions
10. Date Functions
11. Math Functions
12. String Functions
13. Data Convert Functions
14. Analytical Functions
15. Sequence Indentity
16. View
17. Index
18. Cursor
19. Database
20. Transact SQL
21. Procedure Function
22. Trigger
23. Transaction
24. XML
25. System Functions
26. System Settings
27. System Tables Views
28. User Role
29. CLR
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
SQL Server / T-SQL Tutorial » Aggregate Functions » Aggregate function 
9. 1. 2. Using calculations within aggregate functions.
5>
6CREATE TABLE CD (
7>      CD_ID              integer  NOT NULL PRIMARY KEY,
8>      CD_Title           varchar(40),
9>      Composer_ID        integer  NOT NULL,
10>      Classif_ID         integer  NOT NULL,
11>      SalesPrice        money,
12>      AverageCost       money)
13> GO
1INSERT into CD VALUES(2000,"John",100,1,16.99,6.99)
2INSERT into CD VALUES(2001,"Chicago 16",107,1,14.99,5.99)
3INSERT into CD VALUES(2002,"Chicago 17",107,1,14.99,5.99)
4INSERT into CD VALUES(2003,"Chicago 18",107,1,14.99,5.99)
5INSERT into CD VALUES(2004,"Greatest Hits",107,1,16.99,7.99)
6INSERT into CD VALUES(2005,"Midnight",101,3,14.99,5.99)
7INSERT into CD VALUES(2006,"Mode",115,3,14.99,5.99)
8INSERT into CD VALUES(2007,"Ultra",115,3,15.99,5.99)
9INSERT into CD VALUES(2008,"Mindcrime",102,4,14.99,5.99)
10INSERT into CD VALUES(2009,"Empire",102,4,14.99,5.99)
11INSERT into CD VALUES(2010,"Land",102,4,12.99,4.99)
12INSERT into CD VALUES(2011,"Night",103,4,11.99,3.99)
13INSERT into CD VALUES(2012,"Pyromania",103,4,14.99,5.99)
14INSERT into CD VALUES(2013,"Hysteria",103,4,14.99,5.99)
15INSERT into CD VALUES(2014,"Hits",103,4,13.99,4.99)
16INSERT into CD VALUES(2015,"Hits 2",104,2,15.99,6.99)
17INSERT into CD VALUES(2016,"Greatest",105,2,14.99,5.99)
18INSERT into CD VALUES(2017,"Hits 3",106,1,13.99,5.99)
19INSERT into CD VALUES(2018,"Deep",108,1,12.99,2.99)
20INSERT into CD VALUES(2019,"Turning",109,1,14.99,5.99)
21INSERT into CD VALUES(2020,"TheHits",109,1,16.99,7.99)
22INSERT into CD VALUES(2021,"Cars",110,1,9.99,3.99)
23INSERT into CD VALUES(2022,"Anthology",110,1,25.99,11.99)
24INSERT into CD VALUES(2023,"City",110,1,14.99,5.99)
25INSERT into CD VALUES(2024,"Rick",111,1,11.99,2.99)
26INSERT into CD VALUES(2025,"Live",112,1,19.99,8.99)
27INSERT into CD VALUES(2026,"Pat",113,1,16.99,6.99)
28INSERT into CD VALUES(2027,"Big",114,1,14.99,5.99)
29INSERT into CD VALUES(2028,"Hurting",114,1,11.99,3.99)
30INSERT into CD VALUES(2029,"Vol 1",116,1,9.99,2.99)
31INSERT into CD VALUES(2030,"Vol 2",116,1,9.99,2.99)
32> GO

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)
1CREATE TABLE Inventory(
2>      CD_ID              integer  NOT NULL,
3>      StoreID           integer NOT NULL,
4>      QOH               integer NULL,
5>      Returns           integer NULL,
6>      QtyOrdered        integer NULL,
7>      Discontinued      char(1NOT NULL)
8> GO
1INSERT into Inventory VALUES(2000,1330,10,0,0,"N")
2INSERT into Inventory VALUES(2015,1330,5,0,3,"N")
3INSERT into Inventory VALUES(2016,1330,6,0,11,"N")
4INSERT into Inventory VALUES(2017,1330,5,2,0,"N")
5INSERT into Inventory VALUES(2000,1040,3,0,9,"N")
6INSERT into Inventory VALUES(2015,1040,1,0,10,"N")
7INSERT into Inventory VALUES(2016,1040,4,0,11,"N")
8INSERT into Inventory VALUES(2018,1040,15,8,0,"N")
9INSERT into Inventory VALUES(2019,1040,22,3,0,"N")
10INSERT into Inventory VALUES(2020,1040,10,0,0,"N")
11INSERT into Inventory VALUES(2021,1040,19,0,NULL,"N")
12INSERT into Inventory VALUES(2008,1200,11,0,NULL,"N")
13INSERT into Inventory VALUES(2009,1200,5,0,9,"N")
14INSERT into Inventory VALUES(2011,1210,18,0,NULL,"N")
15INSERT into Inventory VALUES(2012,1210,5,0,NULL,"N")
16INSERT into Inventory VALUES(2013,1210,1,0,6,"N")
17INSERT into Inventory VALUES(2025,1220,3,0,8,"N")
18INSERT into Inventory VALUES(2026,1220,3,0,7,"N")
19INSERT into Inventory VALUES(2019,1300,2,0,8,"N")
20INSERT into Inventory VALUES(2020,1300,9,0,0,"N")
21INSERT into Inventory VALUES(2027,1310,2,0,8,"N")
22INSERT into Inventory VALUES(2028,1310,4,0,4,"N")
23INSERT into Inventory VALUES(2029,1320,2,0,8,"N")
24INSERT into Inventory VALUES(2030,1320,6,0,0,"N")
25INSERT into Inventory VALUES(2001,1099,6,0,0,"N")
26INSERT into Inventory VALUES(2002,1099,2,0,3,"N")
27INSERT into Inventory VALUES(2003,1099,7,0,0,"N")
28> GO

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)
1>
2SELECT SUM(CD.AverageCost*Inventory.QOH),
3>        MAX(CD.AverageCost*Inventory.QOH),
4>        MIN(CD.AverageCost*Inventory.QOH)
5FROM CD,Inventory
6WHERE CD.CD_ID = Inventory.CD_ID
7> GO

--------------------- --------------------- ---------------------
            1032.1400              131.7800                5.9800

(rows affected)
1>
2>
3> drop table Inventory;
4> drop table CD;
5> GO
9. 1. Aggregate function
9. 1. 1. Aggregate functions are applied to a group of data values from a column.
9. 1. 2. Using calculations within aggregate functions.
9. 1. 3. Query with aggregate function as a column
9. 1. 4. Aggregate function in sub query
9. 1. 5. Using Aliase with aggregate function
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.