SQL Server/T-SQL/Select Query/Order Columns

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

Identify the columns in the ORDER BY clause by the ordinal position of the column in the SELECT list

   <source lang="sql">

1> CREATE TABLE employee (emp_no INTEGER NOT NULL, 2> emp_fname CHAR(20) NOT NULL, 3> emp_lname CHAR(20) NOT NULL, 4> dept_no CHAR(4) NULL) 5> GO 1> insert into employee values(1, "Matthew", "Smith", "d3") 2> insert into employee values(2, "Ann", "Jones", "d3") 3> insert into employee values(3, "John", "Barrimore","d1") 4> insert into employee values(4, "James", "James", "d2") 5> insert into employee values(5, "Elsa", "Bertoni", "d2") 6> insert into employee values(6, "Elke", "Hansel", "d2") 7> insert into employee values(7, "Sybill", "Moser", "d1") 8> GO (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) 1> select * from employee 2> GO emp_no emp_fname emp_lname dept_no


-------------------- -------------------- -------
         1 Matthew              Smith                d3
         2 Ann                  Jones                d3
         3 John                 Barrimore            d1
         4 James                James                d2
         5 Elsa                 Bertoni              d2
         6 Elke                 Hansel               d2
         7 Sybill               Moser                d1

(7 rows affected) 1> -- Identify the columns in the ORDER BY clause by the ordinal position of the column in the SELECT list. 2> 3> SELECT emp_fname, emp_lname, dept_no FROM employee WHERE emp_no < 20000 4> ORDER BY 2, 1 5> GO emp_fname emp_lname dept_no


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

John Barrimore d1 Elsa Bertoni d2 Elke Hansel d2 James James d2 Ann Jones d3 Sybill Moser d1 Matthew Smith d3 (7 rows affected) 1> drop table employee 2> GO 1>

      </source>
   
  


Order by two columns

   <source lang="sql">

1> create table employee( 2> ID int, 3> name nvarchar (10), 4> salary int, 5> start_date datetime, 6> city nvarchar (10), 7> region char (1)) 8> GO 1> 2> insert into employee (ID, name, salary, start_date, city, region) 3> values (1, "Jason", 40420, "02/01/94", "New York", "W") 4> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (2, "Robert",14420, "01/02/95", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (4, "Linda", 40620, "11/04/97", "New York", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (5, "David", 80026, "10/05/98", "Vancouver","W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (6, "James", 70060, "09/06/99", "Toronto", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (7, "Alison",90620, "08/07/00", "New York", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W") 3> GO (1 rows affected) 1> 2> select * from employee 3> GO ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         1 Jason            40420 1994-02-01 00:00:00.000 New York   W
         2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
         3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
         4 Linda            40620 1997-11-04 00:00:00.000 New York   N
         5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
         6 James            70060 1999-09-06 00:00:00.000 Toronto    N
         7 Alison           90620 2000-08-07 00:00:00.000 New York   W
         8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
         9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W

(9 rows affected) 1> 2> 3> SELECT Name, City 4> FROM Employee ORDER BY Name, City 5> GO Name City


----------

Alison New York Celia Toronto Chris Vancouver David Vancouver James Toronto Jason New York Linda New York Mary Toronto Robert Vancouver (9 rows affected) 1> 2> 3> 4> 5> drop table employee 6> GO 1>

      </source>
   
  


Order criterion may contain more than one column

   <source lang="sql">

1> CREATE TABLE employee (emp_no INTEGER NOT NULL, 2> emp_fname CHAR(20) NOT NULL, 3> emp_lname CHAR(20) NOT NULL, 4> dept_no CHAR(4) NULL) 5> GO 1> insert into employee values(1, "Matthew", "Smith", "d3") 2> insert into employee values(2, "Ann", "Jones", "d3") 3> insert into employee values(3, "John", "Barrimore","d1") 4> insert into employee values(4, "James", "James", "d2") 5> insert into employee values(5, "Elsa", "Bertoni", "d2") 6> insert into employee values(6, "Elke", "Hansel", "d2") 7> insert into employee values(7, "Sybill", "Moser", "d1") 8> GO (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) 1> select * from employee 2> GO emp_no emp_fname emp_lname dept_no


-------------------- -------------------- -------
         1 Matthew              Smith                d3
         2 Ann                  Jones                d3
         3 John                 Barrimore            d1
         4 James                James                d2
         5 Elsa                 Bertoni              d2
         6 Elke                 Hansel               d2
         7 Sybill               Moser                d1

(7 rows affected) 1> -- Order criterion may contain more than one column. 2> 3> SELECT emp_fname, emp_lname, dept_no FROM employee WHERE emp_no < 20000 4> ORDER BY emp_lname, emp_fname 5> GO emp_fname emp_lname dept_no


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

John Barrimore d1 Elsa Bertoni d2 Elke Hansel d2 James James d2 Ann Jones d3 Sybill Moser d1 Matthew Smith d3 (7 rows affected) 1> drop table employee 2> GO 1>

      </source>
   
  


Using a CASE Expression in an ORDER BY Clause

   <source lang="sql">

4> 5> CREATE TABLE employee( 6> id INTEGER NOT NULL PRIMARY KEY, 7> first_name VARCHAR(10), 8> last_name VARCHAR(10), 9> salary DECIMAL(10,2), 10> start_Date DATETIME, 11> region VARCHAR(10), 12> city VARCHAR(20), 13> managerid INTEGER 14> ); 15> GO 1> INSERT INTO employee VALUES (1, "Jason" , "Martin", 5890,"2005-03-22","North","Vancouver",3); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (2, "Alison", "Mathews",4789,"2003-07-21","South","Utown",4); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (3, "James" , "Smith", 6678,"2001-12-01","North","Paris",5); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (4, "Celia" , "Rice", 5567,"2006-03-03","South","London",6); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (5, "Robert", "Black", 4467,"2004-07-02","East","Newton",7); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (6, "Linda" , "Green" , 6456,"2002-05-19","East","Calgary",8); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (7, "David" , "Larry", 5345,"2008-03-18","West","New York",9); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (8, "James" , "Cat", 4234,"2007-07-17","West","Regina",9); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (9, "Joan" , "Act", 6123,"2001-04-16","North","Toronto",10); 2> GO (1 rows affected) 1> 2> select * from employee; 3> GO id first_name last_name salary start_Date region city managerid


---------- ---------- ------------ ----------------------- ---------- -------------------- -----------
         1 Jason      Martin          5890.00 2005-03-22 00:00:00.000 North      Vancouver                      3
         2 Alison     Mathews         4789.00 2003-07-21 00:00:00.000 South      Utown                          4
         3 James      Smith           6678.00 2001-12-01 00:00:00.000 North      Paris                          5
         4 Celia      Rice            5567.00 2006-03-03 00:00:00.000 South      London                         6
         5 Robert     Black           4467.00 2004-07-02 00:00:00.000 East       Newton                         7
         6 Linda      Green           6456.00 2002-05-19 00:00:00.000 East       Calgary                        8
         7 David      Larry           5345.00 2008-03-18 00:00:00.000 West       New York                       9
         8 James      Cat             4234.00 2007-07-17 00:00:00.000 West       Regina                         9
         9 Joan       Act             6123.00 2001-04-16 00:00:00.000 North      Toronto                       10

(9 rows affected) 1> 2> 3> 4> SELECT 5> * 6> FROM 7> Employee 8> ORDER BY 9> CASE City 10> WHEN "New York" THEN char (1) 11> WHEN "Vancouver" THEN char (2) 12> WHEN "Mexico" THEN char (3) 13> ELSE City 14> END 15> GO id first_name last_name salary start_Date region city managerid


---------- ---------- ------------ ----------------------- ---------- -------------------- -----------
         7 David      Larry           5345.00 2008-03-18 00:00:00.000 West       New York                       9
         1 Jason      Martin          5890.00 2005-03-22 00:00:00.000 North      Vancouver                      3
         6 Linda      Green           6456.00 2002-05-19 00:00:00.000 East       Calgary                        8
         4 Celia      Rice            5567.00 2006-03-03 00:00:00.000 South      London                         6
         5 Robert     Black           4467.00 2004-07-02 00:00:00.000 East       Newton                         7
         3 James      Smith           6678.00 2001-12-01 00:00:00.000 North      Paris                          5
         8 James      Cat             4234.00 2007-07-17 00:00:00.000 West       Regina                         9
         9 Joan       Act             6123.00 2001-04-16 00:00:00.000 North      Toronto                       10
         2 Alison     Mathews         4789.00 2003-07-21 00:00:00.000 South      Utown                          4

(9 rows affected) 1> 2> 3> 4> drop table employee; 5> GO

</source>
   
  


Using a CASE Expression in an ORDER BY Clause (Alternative)

   <source lang="sql">

3> 4> CREATE TABLE employee( 5> id INTEGER NOT NULL PRIMARY KEY, 6> first_name VARCHAR(10), 7> last_name VARCHAR(10), 8> salary DECIMAL(10,2), 9> start_Date DATETIME, 10> region VARCHAR(10), 11> city VARCHAR(20), 12> managerid INTEGER 13> ); 14> GO 1> INSERT INTO employee VALUES (1, "Jason" , "Martin", 5890,"2005-03-22","North","Vancouver",3); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (2, "Alison", "Mathews",4789,"2003-07-21","South","Utown",4); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (3, "James" , "Smith", 6678,"2001-12-01","North","Paris",5); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (4, "Celia" , "Rice", 5567,"2006-03-03","South","London",6); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (5, "Robert", "Black", 4467,"2004-07-02","East","Newton",7); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (6, "Linda" , "Green" , 6456,"2002-05-19","East","Calgary",8); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (7, "David" , "Larry", 5345,"2008-03-18","West","New York",9); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (8, "James" , "Cat", 4234,"2007-07-17","West","Regina",9); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (9, "Joan" , "Act", 6123,"2001-04-16","North","Toronto",10); 2> GO (1 rows affected) 1> 2> select * from employee; 3> GO id first_name last_name salary start_Date region city managerid


---------- ---------- ------------ ----------------------- ---------- -------------------- -----------
         1 Jason      Martin          5890.00 2005-03-22 00:00:00.000 North      Vancouver                      3
         2 Alison     Mathews         4789.00 2003-07-21 00:00:00.000 South      Utown                          4
         3 James      Smith           6678.00 2001-12-01 00:00:00.000 North      Paris                          5
         4 Celia      Rice            5567.00 2006-03-03 00:00:00.000 South      London                         6
         5 Robert     Black           4467.00 2004-07-02 00:00:00.000 East       Newton                         7
         6 Linda      Green           6456.00 2002-05-19 00:00:00.000 East       Calgary                        8
         7 David      Larry           5345.00 2008-03-18 00:00:00.000 West       New York                       9
         8 James      Cat             4234.00 2007-07-17 00:00:00.000 West       Regina                         9
         9 Joan       Act             6123.00 2001-04-16 00:00:00.000 North      Toronto                       10

(9 rows affected) 1> 2> 3> SELECT 4> * 5> FROM 6> Employee 7> ORDER BY 8> CASE City 9> WHEN "Vancouver" THEN 1 10> WHEN "Toronto" THEN 2 11> WHEN "Mexico" THEN 3 12> ELSE 4 13> END, -- Dummy NAFTA Region 14> City, 15> Region 16> GO id first_name last_name salary start_Date region city managerid


---------- ---------- ------------ ----------------------- ---------- -------------------- -----------
         1 Jason      Martin          5890.00 2005-03-22 00:00:00.000 North      Vancouver                      3
         9 Joan       Act             6123.00 2001-04-16 00:00:00.000 North      Toronto                       10
         6 Linda      Green           6456.00 2002-05-19 00:00:00.000 East       Calgary                        8
         4 Celia      Rice            5567.00 2006-03-03 00:00:00.000 South      London                         6
         7 David      Larry           5345.00 2008-03-18 00:00:00.000 West       New York                       9
         5 Robert     Black           4467.00 2004-07-02 00:00:00.000 East       Newton                         7
         3 James      Smith           6678.00 2001-12-01 00:00:00.000 North      Paris                          5
         8 James      Cat             4234.00 2007-07-17 00:00:00.000 West       Regina                         9
         2 Alison     Mathews         4789.00 2003-07-21 00:00:00.000 South      Utown                          4

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

</source>