数据排序与分组

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 |
+---------------+--------------+-------------+---------------+
时间: 2024-11-24 15:52:58

数据排序与分组的相关文章

Atitit &#160;数据存储的分组聚合 groupby的实现attilax总结

Atitit  数据存储的分组聚合 groupby的实现attilax总结 1. 聚合操作1 1.1. a.标量聚合 流聚合1 1.2. b.哈希聚合2 1.3. 所有的最优计划的选择都是基于现有统计信息来评估3 1.4. 参考资料3 1. 聚合操作 聚合也是我们在写T-SQL语句的时候经常遇到的,我们来分析一下一些常用的聚合操作运算符的特性和可优化项. 1.1. a.标量聚合 流聚合 标量聚合是一种常用的数据聚合方式,比如我们写的语句中利用的以下聚合函数:MAX().MIN().AVG().C

Hadoop学习笔记—11.MapReduce中的排序和分组

一.写在之前的 1.1 回顾Map阶段四大步凑 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出,在Step1.4也就是第四步中,需要对不同分区中的数据进行排序和分组,默认情况下,是按照key进行排序和分组. 1.2 实验场景数据文件 在一些特定的数据文件中,不一定都是类似于WordCount单次统计这种规范的数据,比如下面这类数据,它虽然只有两列,但是却有一定的实践意义. 3 3 3 2 3 1 2 2 2 1 1 1 (1)如果按照第一列升序排列,当

最常用的SQL排序、分组与统计的使用方法

以一种有意义的方式组织数据可能是一项挑战.有时你需要的可能是一个简单的排序,但是通常你需要做更多,你需要分组来进行分析和统计.幸运的是,SQL提供了大量语句和操作来进行排序,分组和摘要.下面的一些技巧将会帮助你识别什么时候排序,什么时候分组,什么时候以及如何统计.对要每条语句和操作的详细信息请查看Books Online. 1. 使用排序使数据有序 通常,你的所有数据真正需要的仅仅是按某种顺序排列.SQL的ORDER BY语句可以以字母或数字顺序组织数据.因此,相似的值按组排序在一起.然而,这个

ListView中排序和分组(GroupTemplate)的使用实例演示

.aspx代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="8_Group_Sort.aspx.cs" Inherits="Group_Sort" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3

MapReduce框架排序和分组

前言: Mapreduce框架就是map->reduce,其中Map中的<key,value>是偏移量和行值,在其之前会使用job.setInputFormatClass定义的InputFormat将输入的数据集分割成小数据块splites,同时InputFormat提供一个RecordReder的实现.本例子中使用的是TextInputFormat,他提供的RecordReder会将文本的一行的行号作为key,这一行的文本作为value.这就是自定义Map的输入是<LongWri

c#中关于udp实现可靠地传输(数据包的分组发送) 升级版

在c#中关于udp实现可靠地传输(数据包的分组发送)中我们讨论了,UDP包的发送,但是上一个程序有一个问题,就是数据比较大,一个Message类序列化后都有2048B,而实际的数据量也就不过 50B罢了,这就说明其中数据有效的很少,这样当传送的数据包过多后,效率会极大的降低.因此我们只有想办法减少冗余数据. 此项目中借用了飞鸽传书中的一个<FSLib.IPMessager>项目中的思想,并加以改善,感谢此项目作者,让我对此有了深刻的理解 我们需要自己定义数据的传输结构    我们可以定义一个数

(转)最常用的SQL排序、分组与统计的使用方法 Order by/Group by/having等的使用

以一种有意义的方式组织数据可能是一项挑战.有时你需要的可能是一个简单的排序,但是通常你需要做更多,你需要分组来进行分析和统计.幸运的 是,SQL提供了大量语句和操作来进行排序,分组和摘要.下面的一些技巧将会帮助你识别什么时候排序,什么时候分组,什么时候以及如何统计.对要每条语句 和操作的详细信息请查看Books Online. 1. 使用排序使数据有序 通常,你的所有数据真正需要的仅仅是按某种顺序排列.SQL的ORDER BY语句可以以字母或数字顺序组织数据.因此,相似的值按组排序在一起.然而,

MySQL的外键,修改表,基本数据类型,表级别操作,其他(条件,通配符,分页,排序,分组,联合,连表操作)

MySQL的外键,修改表,基本数据类型,表级别操作,其他(条件,通配符,分页,排序,分组,联合,连表操作): a.创建2张表 create table userinfo(nid int not null auto_increment primary key, name varchar(10), age int, part_nid int )engine=innodb default charset=utf8; create table part( nid int not null auto_in

PHP二维数据排序,二维数据模糊查询

一.因为项目中的一个报表需要合并三个表的数据,所以分表查询再合并数据,利用PHP数组函数进行排序,搜索.三表合并后的数组结构如下: Array ( [0] => Array ( [history_id] => 12 [sla_group_id] => 1 [sla_id] => -1 [create_time] => 1513057695 [tasklog_id] => 12 [tasklog_time] => 2017-12-12 13:48:15 [taskl