SQL Server/T-SQL/Select Query/Math Operator

Материал из SQL эксперт
Перейти к: навигация, поиск

A SELECT statement that uses the modulo operator

   <source lang="sql">

5> 6> 7> create table Billings ( 8> BankerID INTEGER, 9> BillingNumber INTEGER, 10> BillingDate datetime, 11> BillingTotal INTEGER, 12> TermsID INTEGER, 13> BillingDueDate datetime , 14> PaymentTotal INTEGER, 15> CreditTotal INTEGER 16> 17> ); 18> 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> 4> 5> SELECT BankerID, 6> BankerID / 10 AS Quotient, 7> BankerID % 10 AS Remainder 8> FROM Billings 9> GO BankerID Quotient Remainder


----------- -----------
         1           0           1
         2           0           2
         3           0           3
         4           0           4
         5           0           5
         6           0           6
         7           0           7
         8           0           8
         9           0           9
         0           0           0

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

</source>
   
  


A statement that names just the calculated column in its SELECT clause

   <source lang="sql">

8> 9> 10> create table Billings ( 11> BankerID INTEGER, 12> BillingNumber INTEGER, 13> BillingDate datetime, 14> BillingTotal INTEGER, 15> TermsID INTEGER, 16> BillingDueDate datetime , 17> PaymentTotal INTEGER, 18> CreditTotal INTEGER 19> 20> ); 21> 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> CREATE VIEW OutstandingBillings 3> AS 4> SELECT BillingNumber, BillingDate, BillingTotal, 5> BillingTotal - PaymentTotal - CreditTotal AS BalanceDue 6> FROM Billings 7> WHERE BillingTotal - PaymentTotal - CreditTotal > 0 8> GO 1> 2> drop view OutstandingBillings; 3> GO 1> 2> drop table Billings; 3> GO

</source>