码迷,mamicode.com
首页 > 编程语言 > 详细

数据排序与分组

时间:2015-05-31 18:11:19      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

SELECT * FROM EMPLOYEE_TBL;
+-----------+-----------+------------+-------------+-----------------------+--------------+-------+-------+------------+------------+
| EMP_ID    | LAST_NAME | FIRST_NAME | MIDDLE_NAME | ADDRESS               | CITY         | STATE | ZIP   | PHONE      | PAGER      |
+-----------+-----------+------------+-------------+-----------------------+--------------+-------+-------+------------+------------+
| 213764555 | GLASS     | RBANDON    | SCOTT       | 1710 MAIN ST          | WHITELAND    | IN    | 47885 | 3178984321 | 3175709980 |
| 220984332 | WALLACE   | MARIAH     | NULL        | 7889 KEYSTON AVE      | INDIANAPOLIS | IN    | 46741 | 3173325986 | NULL       |
| 311549902 | STEPHENS  | TINA       | DAWN        | RR 3 BOX 17A          | GREENWOOD    | IN    | 47890 | 3178784465 | NULL       |
| 313782439 | GLASS     | JACOB      | NULL        | 3789 WHITE RIVER BLVD | INDIANAPOLIS | IN    | 45734 | 3175457676 | 8887345678 |
| 442346889 | PLEW      | LINDA      | CAROL       | 3301 BEACON           | INDIANAPOLIS | IN    | 46224 | 3172978990 | NULL       |
| 443679012 | SPURGEON  | TIFFANY    | NULL        | 5 GEORGE COURT        | INDIANAPOLIS | IN    | 46234 | 3175679007 | NULL       |
+-----------+-----------+------------+-------------+-----------------------+--------------+-------+-------+------------+------------+

SELECT EMP_ID, CITY FROM EMPLOYEE_TBL GROUP BY CITY, EMP_ID;
+-----------+--------------+
| EMP_ID    | CITY         |
+-----------+--------------+
| 311549902 | GREENWOOD    |
| 220984332 | INDIANAPOLIS |
| 313782439 | INDIANAPOLIS |
| 442346889 | INDIANAPOLIS |
| 443679012 | INDIANAPOLIS |
| 213764555 | WHITELAND    |
+-----------+--------------+

SELECT EMP_ID, CITY FROM EMPLOYEE_TBL GROUP BY CITY, EMP_ID ORDER BY EMP_ID DESC;
+-----------+--------------+
| EMP_ID    | CITY         |
+-----------+--------------+
| 443679012 | INDIANAPOLIS |
| 442346889 | INDIANAPOLIS |
| 313782439 | INDIANAPOLIS |
| 311549902 | GREENWOOD    |
| 220984332 | INDIANAPOLIS |
| 213764555 | WHITELAND    |
+-----------+--------------+

SELECT EMP_ID, CITY FROM EMPLOYEE_TBL GROUP BY CITY, EMP_ID ORDER BY CITY, EMP_ID DESC;
+-----------+--------------+
| EMP_ID    | CITY         |
+-----------+--------------+
| 311549902 | GREENWOOD    |
| 443679012 | INDIANAPOLIS |
| 442346889 | INDIANAPOLIS |
| 313782439 | INDIANAPOLIS |
| 220984332 | INDIANAPOLIS |
| 213764555 | WHITELAND    |
+-----------+--------------+


SELECT SUM(SALARY) AS TOTAL_SALARY FROM EMPLOYEE_PAY_TBL;
+--------------+
| TOTAL_SALARY |
+--------------+
|     90000.00 |
+--------------+

SELECT SUM(SALARY) FROM EMPLOYEE_PAY_TBL GROUP BY SALARY;
+-------------+
| SUM(SALARY) |
+-------------+
|        NULL |
|    20000.00 |
|    30000.00 |
|    40000.00 |
+-------------+


SELECT COUNT(*) FROM EMPLOYEE_TBL;
+----------+
| COUNT(*) |
+----------+
|        6 |
+----------+

SELECT CITY, COUNT(*) FROM EMPLOYEE_TBL GROUP BY CITY;
-- 这时会分别看到每个不同城市的记录总和,因为其中使用了GROUP BY子句
+--------------+----------+
| CITY         | COUNT(*) |
+--------------+----------+
| GREENWOOD    |        1 |
| INDIANAPOLIS |        4 |
| WHITELAND    |        1 |
+--------------+----------+

SELECT * FROM EMPLOYEE_PAY_TBL;
+-----------+---------------+------------+----------+-----------------+----------+---------+
| EMP_ID    | POSITION      | DATE_HIRE  | PAY_RATE | DATE_LAST_RAISE | SALARY   | BONUS   |
+-----------+---------------+------------+----------+-----------------+----------+---------+
| 213764555 | SALES MANAGER | 2004-08-14 |     NULL | 2009-08-01      | 30000.00 | 2000.00 |
| 220984332 | SHIPPER       | 2006-07-22 |    11.00 | 1999-07-01      |     NULL |    NULL |
| 311549902 | MARKETING     | 1999-05-23 |     NULL | 2009-05-01      | 40000.00 |    NULL |
| 313782439 | SALSEMAN      | 2007-06-28 |     NULL | NULL            | 20000.00 | 1000.00 |
| 442346889 | TEAM LEADER   | 2000-06-17 |    14.75 | 2009-06-01      |     NULL |    NULL |
| 443679012 | SHIPPER       | 2001-01-14 |    15.00 | 1999-01-01      |     NULL |    NULL |
+-----------+---------------+------------+----------+-----------------+----------+---------+

SELECT YEAR(DATE_HIRE) AS YEAR_HIRED, SUM(SALARY) FROM EMPLOYEE_PAY_TBL GROUP BY 1;
+------------+-------------+
| YEAR_HIRED | SUM(SALARY) |
+------------+-------------+
|       1999 |    40000.00 |
|       2000 |        NULL |
|       2001 |        NULL |
|       2004 |    30000.00 |
|       2006 |        NULL |
|       2007 |    20000.00 |
+------------+-------------+

 

SELECT * FROM EMPLOYEE_TBL;
+-----------+-----------+------------+-------------+-----------------------+--------------+-------+-------+------------+------------+
| EMP_ID    | LAST_NAME | FIRST_NAME | MIDDLE_NAME | ADDRESS               | CITY         | STATE | ZIP   | PHONE      | PAGER      |
+-----------+-----------+------------+-------------+-----------------------+--------------+-------+-------+------------+------------+
| 213764555 | GLASS     | RBANDON    | SCOTT       | 1710 MAIN ST          | WHITELAND    | IN    | 47885 | 3178984321 | 3175709980 |
| 220984332 | WALLACE   | MARIAH     | NULL        | 7889 KEYSTON AVE      | INDIANAPOLIS | IN    | 46741 | 3173325986 | NULL       |
| 311549902 | STEPHENS  | TINA       | DAWN        | RR 3 BOX 17A          | GREENWOOD    | IN    | 47890 | 3178784465 | NULL       |
| 313782439 | GLASS     | JACOB      | NULL        | 3789 WHITE RIVER BLVD | INDIANAPOLIS | IN    | 45734 | 3175457676 | 8887345678 |
| 442346889 | PLEW      | LINDA      | CAROL       | 3301 BEACON           | INDIANAPOLIS | IN    | 46224 | 3172978990 | NULL       |
| 443679012 | SPURGEON  | TIFFANY    | NULL        | 5 GEORGE COURT        | INDIANAPOLIS | IN    | 46234 | 3175679007 | NULL       |
+-----------+-----------+------------+-------------+-----------------------+--------------+-------+-------+------------+------------+

SELECT * FROM EMPLOYEE_PAY_TBL;
+-----------+---------------+------------+----------+-----------------+----------+---------+
| EMP_ID    | POSITION      | DATE_HIRE  | PAY_RATE | DATE_LAST_RAISE | SALARY   | BONUS   |
+-----------+---------------+------------+----------+-----------------+----------+---------+
| 213764555 | SALES MANAGER | 2004-08-14 |     NULL | 2009-08-01      | 30000.00 | 2000.00 |
| 220984332 | SHIPPER       | 2006-07-22 |    11.00 | 1999-07-01      |     NULL |    NULL |
| 311549902 | MARKETING     | 1999-05-23 |     NULL | 2009-05-01      | 40000.00 |    NULL |
| 313782439 | SALSEMAN      | 2007-06-28 |     NULL | NULL            | 20000.00 | 1000.00 |
| 442346889 | TEAM LEADER   | 2000-06-17 |    14.75 | 2009-06-01      |     NULL |    NULL |
| 443679012 | SHIPPER       | 2001-01-14 |    15.00 | 1999-01-01      |     NULL |    NULL |
+-----------+---------------+------------+----------+-----------------+----------+---------+

SELECT CITY, ZIP, AVG(PAY_RATE), AVG(SALARY) 
    -> FROM EMPLOYEE_TBL E
    -> INNER JOIN EMPLOYEE_PAY_TBL P
    -> ON E.EMP_ID = P.EMP_ID
    -> GROUP BY CITY, ZIP
    -> ORDER BY CITY, ZIP;
+--------------+-------+---------------+--------------+
| CITY         | ZIP   | AVG(PAY_RATE) | AVG(SALARY)  |
+--------------+-------+---------------+--------------+
| GREENWOOD    | 47890 |          NULL | 40000.000000 |
| INDIANAPOLIS | 45734 |          NULL | 20000.000000 |
| INDIANAPOLIS | 46224 |     14.750000 |         NULL |
| INDIANAPOLIS | 46234 |     15.000000 |         NULL |
| INDIANAPOLIS | 46741 |     11.000000 |         NULL |
| WHITELAND    | 47885 |          NULL | 30000.000000 |
+--------------+-------+---------------+--------------+

SELECT CITY, ZIP, AVG(PAY_RATE), AVG(SALARY) 
    -> FROM EMPLOYEE_TBL E
    -> INNER JOIN EMPLOYEE_PAY_TBL P
    -> ON E.EMP_ID = P.EMP_ID
    -> GROUP BY CITY, ZIP
    -> ORDER BY CITY, ZIP;
+--------------+-------+---------------+--------------+
| CITY         | ZIP   | AVG(PAY_RATE) | AVG(SALARY)  |
+--------------+-------+---------------+--------------+
| GREENWOOD    | 47890 |          NULL | 40000.000000 |
| INDIANAPOLIS | 45734 |          NULL | 20000.000000 |
| INDIANAPOLIS | 46224 |     14.750000 |         NULL |
| INDIANAPOLIS | 46234 |     15.000000 |         NULL |
| INDIANAPOLIS | 46741 |     11.000000 |         NULL |
| WHITELAND    | 47885 |          NULL | 30000.000000 |
+--------------+-------+---------------+--------------+

SELECT CITY, ZIP, AVG(PAY_RATE), AVG(SALARY)
    -> FROM EMPLOYEE_TBL E
    -> INNER JOIN EMPLOYEE_PAY_TBL P
    -> ON E.EMP_ID = P.EMP_ID
    -> GROUP BY CITY, ZIP WITH ROLLUP;
-- ROLLUP:完成基本的分组数据汇总后,按照从右到左的顺序,每次去掉字段列表中的最后一个字段,再对剩余的字段进行分组统计
+--------------+-------+---------------+--------------+
| CITY         | ZIP   | AVG(PAY_RATE) | AVG(SALARY)  |
+--------------+-------+---------------+--------------+
| GREENWOOD    | 47890 |          NULL | 40000.000000 |
| GREENWOOD    |  NULL |          NULL | 40000.000000 |
| INDIANAPOLIS | 45734 |          NULL | 20000.000000 |
| INDIANAPOLIS | 46224 |     14.750000 |         NULL |
| INDIANAPOLIS | 46234 |     15.000000 |         NULL |
| INDIANAPOLIS | 46741 |     11.000000 |         NULL |
| INDIANAPOLIS |  NULL |     13.583333 | 20000.000000 |
| WHITELAND    | 47885 |          NULL | 30000.000000 |
| WHITELAND    |  NULL |          NULL | 30000.000000 |
| NULL         |  NULL |     13.583333 | 30000.000000 |
+--------------+-------+---------------+--------------+

 

SELECT AVG(PAY_RATE), AVG(SALARY), AVG(BONUS), POSITION FROM EMPLOYEE_PAY_TBL WHERE POSITION <> SALESMAN GROUP BY POSITION ORDER BY 4;
+---------------+--------------+-------------+---------------+
| AVG(PAY_RATE) | AVG(SALARY)  | AVG(BONUS)  | POSITION      |
+---------------+--------------+-------------+---------------+
|          NULL | 40000.000000 |        NULL | MARKETING     |
|          NULL | 30000.000000 | 2000.000000 | SALES MANAGER |
|          NULL | 20000.000000 | 1000.000000 | SALSEMAN      |
|     13.000000 |         NULL |        NULL | SHIPPER       |
|     14.750000 |         NULL |        NULL | TEAM LEADER   |
+---------------+--------------+-------------+---------------+

SELECT AVG(PAY_RATE), AVG(SALARY), AVG(BONUS), POSITION FROM EMPLOYEE_PAY_TBL WHERE POSITION <> SALESMAN GROUP BY POSITION HAVING AVG(SALARY) > 20000 ORDER BY 4;
+---------------+--------------+-------------+---------------+
| AVG(PAY_RATE) | AVG(SALARY)  | AVG(BONUS)  | POSITION      |
+---------------+--------------+-------------+---------------+
|          NULL | 40000.000000 |        NULL | MARKETING     |
|          NULL | 30000.000000 | 2000.000000 | SALES MANAGER |
+---------------+--------------+-------------+---------------+

 

数据排序与分组

标签:

原文地址:http://www.cnblogs.com/fatoland/p/4542298.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!