A summary query that uses the MIN and MAX functions : Min « Aggregate Functions « SQL Server / T-SQL Tutorial






4>
5> create table Billings (
6>     BankerID           INTEGER,
7>     BillingNumber      INTEGER,
8>     BillingDate        datetime,
9>     BillingTotal       INTEGER,
10>     TermsID            INTEGER,
11>     BillingDueDate     datetime ,
12>     PaymentTotal       INTEGER,
13>     CreditTotal        INTEGER
14>
15> );
16> GO
1>
2> INSERT INTO Billings VALUES (1, 1, '2005-01-22', 165, 1,'2005-04-22',123,321);
3> GO

(1 rows affected)
1> INSERT INTO Billings VALUES (2, 2, '2001-02-21', 165, 1,'2002-02-22',123,321);
2> GO

(1 rows affected)
1> INSERT INTO Billings VALUES (3, 3, '2003-05-02', 165, 1,'2005-04-12',123,321);
2> GO

(1 rows affected)
1> INSERT INTO Billings VALUES (4, 4, '1999-03-12', 165, 1,'2005-04-18',123,321);
2> GO

(1 rows affected)
1> INSERT INTO Billings VALUES (5, 5, '2000-04-23', 165, 1,'2005-04-17',123,321);
2> GO

(1 rows affected)
1> INSERT INTO Billings VALUES (6, 6, '2001-06-14', 165, 1,'2005-04-18',123,321);
2> GO

(1 rows affected)
1> INSERT INTO Billings VALUES (7, 7, '2002-07-15', 165, 1,'2005-04-19',123,321);
2> GO

(1 rows affected)
1> INSERT INTO Billings VALUES (8, 8, '2003-08-16', 165, 1,'2005-04-20',123,321);
2> GO

(1 rows affected)
1> INSERT INTO Billings VALUES (9, 9, '2004-09-17', 165, 1,'2005-04-21',123,321);
2> GO

(1 rows affected)
1> INSERT INTO Billings VALUES (0, 0, '2005-10-18', 165, 1,'2005-04-22',123,321);
2> GO

(1 rows affected)
1>
2>
3> SELECT 'After 1/1/2002' AS SelectionDate, COUNT(*) AS NumberOfBillings,
4>     MAX(BillingTotal) AS HighestBillingTotal,
5>     MIN(BillingTotal) AS LowestBillingTotal
6> FROM Billings
7> WHERE BillingDate > '2002-01-01'
8> GO
SelectionDate  NumberOfBillings HighestBillingTotal LowestBillingTotal
-------------- ---------------- ------------------- ------------------
After 1/1/2002                6                 165                165

(1 rows affected)
1>
2> drop table Billings;
3> GO








9.5.Min
9.5.1.MIN returns the smallest value in a column of all selected records by the query of any char, number, or datetime datatype.
9.5.2.A summary query that uses the MIN and MAX functions
9.5.3.Query Using MIN() and MAX() on Same Column
9.5.4.id = (SELECT MIN(id) FROM employee)
9.5.5.Finding the Earliest Date Using MIN()
9.5.6.Finding the First Order for Each Customer Using MIN()