-
2018-11-08 11:12:52
如下表table1:
日期(exportDate) 数量(amount)
-------------- -----------
14-2月 -08 20
10-3月 -08 2
14-4月 -08 6
14-6月 -08 75
24-10月-09 23
14-11月-09 45
04-8月 -10 5
04-9月 -10 44
04-10月-10 882.按月份分组
select to_char(exportDate,‘yyyy-mm’),sum(amount) from table1 group by to_char(exportDate,‘yyyy-mm’)
order by to_char(exportDate,‘yyyy-mm’);
月份 数量
-----------------------------
2008-02 20
2008-03 2
2008-04 6
2008-06 75
2009-10 23
2009-11 45
2010-08 5
2010-09 44
2010-10 88
1.按年份分组
select to_char(exportDate,‘yyyy’),sum(amount) from table1 group by to_char(exportDate,‘yyyy’);
年份 数量
-----------------------------
2009 68
2010 137
2008 103
3.按季度分组
select to_char(exportDate,‘yyyy-Q’),sum(amount) from table1 group by to_char(exportDate,‘yyyy-Q’)
order by to_char(exportDate,‘yyyy-Q’);
季度 数量
------------------------------
2008-1 22
2008-2 81
2009-4 68
2010-3 49
2010-4 88
4.按周分组
select to_char(exportDate,‘yyyy-IW’),sum(amount) from table1 group by to_char(exportDate,‘yyyy-IW’)
order by to_char(exportDate,‘yyyy-IW’);
周 数量
------------------------------
2008-07 20
2008-11 2
2008-16 6
2008-24 75
2009-43 23
2009-46 45
2010-31 5
2010-35 44
2010-40 88
补充:
按季度分组还有个比较笨的方法(参考网络资源)
select to_char(exportDate,‘yyyy’),
sum(decode(to_char(exportDate,‘mm’),‘01’,amount,‘02’,amount,‘03’,amount,0)) as 第一季,
sum(decode(to_char(exportDate,‘mm’),‘04’,amount,‘05’,amount,‘06’,amount,0)) as 第二季,
sum(decode(to_char(exportDate,‘mm’),‘07’,amount,‘08’,amount,‘09’,amount,0)) as 第三季,
sum(decode(to_char(exportDate,‘mm’),‘10’,amount,‘11’,amount,‘12’,amount,0)) as 第四季
from table1
group by to_char(exportDate,‘yyyy’);
年份 第一季 第二季 第三季 第四季
--------------------------------------------------
2009 0 0 0 68
2010 0 0 49 88
2008 22 81 0 0更多相关内容 -
JAVA快速排序(2020年十二月整理)_快速排序算法java实现
2020-12-15 12:25:55PAGE PAGE 1 冒泡排序 publicclassA{ publicstaticvoidmain(String[]args){ int[]values={3,1,6,2,9,0,7,4,5}; sort(values; for(inti=0;i;++i){ System.out.println"Index"+i+"Value"+values[i]; } } pu -
nodeGitAnalytics:该程序可在本地克隆存储库,并按过去6个月中周末的提交次数将排序的人员列表打印到控制台...
2021-05-02 23:25:19该程序可在本地克隆存储库,并根据过去6个月存储库上周末的提交次数,在控制台上显示按人员排序的列表-https: 语言-nodeJS,JavaScript,JS ,ES6框架-nodeGit需要2次运行脚本-第一次-从存储库下载。 第二分析 该... -
插入排序算法实验报告.docx
2020-08-24 21:53:54算法设计与分析基础 实验报告 应用数学学院 二零一六年六月 实验一 插入排序算法 一实验性质 设计 二实验学时 14学时 三实验目的 1掌握插入排序的方法和原理 2掌握java语言实现该算法的一般流程 四实验内容 1数组的... -
SORTBREAK(v1.0,2008 年 11 月):在断点之间对向量的元素进行排序-matlab开发
2021-05-31 01:08:03Y = SORTBREAK(X,BP) 对向量 X 的部分内的元素进行排序按升序排列。 这些部分由断点定义指数 BP。 每个部分都是独立排序的。 如果 BP 是单个索引,则这两个部分是 X(1:BP) 和 X((BP+1):end)。 例子: Y = sortbreak... -
实验二-快速排序的mpi并行程序.docx
2020-09-03 00:18:14实验二 快速排序的MPI并行程序 实验时间2015年11月5日 实验名称 快速排序的mpi并行化设计 内容与要求 1 内容快速排序(以下简称快排)是对冒泡排序的一种改进它的思想是通 过一趟排序将要排序的数据分割成独立的两部分... -
[Spark的二次排序的实现]
2018-06-05 20:59:56二次排序技术 假设对应的Key = K有如下值: (K,V1), (K,V2),…,(K,Vn) 另外假设每个Vi是包含m个属性的一个元组,如下所示: (Ai1,Ai2,…,Aim)在这里我们希望按Ai1对归约器的元组的值...二次排序原理
二次排序就是首先按照第一字段排序,然后再对第一字段相同的行按照第二字段排序,注意不能破坏第一次排序的结果。
二次排序技术
假设对应的Key = K有如下值:
(K,V1), (K,V2),…,(K,Vn)
另外假设每个Vi是包含m个属性的一个元组,如下所示:
(Ai1,Ai2,…,Aim)
在这里我们希望按Ai1对归约器的元组的值进行排序。我们将用R表示元组其余的属性:(Ai2,…,Aim),因此,可以把归约器的值表示为:
(K,(A1,R1)),(K,(A2,R2)),…,(K,(An,Rn))
要按Ai对归约器的值进行排序,那么需要创建一个组合键:(K,Ai),新映射器将发出对应的Key=K的键值对,如下表所示:
键
值
(K,A1)
(A1,R1)
(K,A2)
(A2,R2)
…
…
(K,An)
(An,Rn)
从上表中不难理解,定义组合键为(K,Ai),自然键为K,通过定义组合键(即为自然键增加属性Ai)。假设二次排序使用MapReduce框架对归约器的值进行排序,按照自然键(K)来完成分区。则自然键和组合键排序图如下所示:
假设有这样的一个二次排序问题的例子:考虑一个科学试验得到的温度数据,这样温度的数据如下所示(各列分别为年,月,日,温度):
2015 1 1 10
2015 1 2 11
2015 1 3 12
2015 1 4 13
…
2015 2 1 22
2015 2 2 23
2015 2 3 24
2015 2 4 25
…
2015 3 1 20
2015 3 2 21
2015 3 3 22
2015 3 4 23
假设我们希望输出每一个[年-月]的温度,并且值按升序排序。
Spark的二次排序代码实现
1、自定义排序分区,代码如下:
/** * 自定义排序分区 **/ class SortPartitioner(partitions: Int) extends Partitioner { require(partitions > 0, s"分区的数量($partitions)必须大于零。") def numPartitions: Int = partitions def getPartition(key: Any): Int = key match { case (k: String, v: Int) => math.abs(k.hashCode % numPartitions) case null => 0 case _ => math.abs(key.hashCode % numPartitions) } override def equals(other: Any): Boolean = other match { case o: SortPartitioner => o.numPartitions == numPartitions case _ => false } override def hashCode: Int = numPartitions }
2、二次排序代码实现
/** * Spark的二次排序 **/ object SparkSecondarySort { def main(args: Array[String]): Unit = { if (args.length != 3) { println("输入参数<分区数> <输入路径> <输出路径>不正确") sys.exit(1) } //分区数量 val partitions: Int = args(0).toInt //文件输入路径 val inputPath: String = args(1) //文件输出路径 val outputPath: String = args(2) val config: SparkConf = new SparkConf() config.setMaster("local[1]").setAppName("SparkSecondarySort") //创建Spark上下文 val sc: SparkContext = SparkSession.builder().config(config).getOrCreate().sparkContext //读取文件内容 val input: RDD[String] = sc.textFile(inputPath) val valueToKey: RDD[((String, Int), Int)] = input.map(x => { val line: Array[String] = x.split("\t") ((line(0) + "-" + line(1), line(3).toInt), line(3).toInt) }) implicit def tupleOrderingDesc = new Ordering[Tuple2[String, Int]] { override def compare(x: Tuple2[String, Int], y: Tuple2[String, Int]): Int = { if (y._1.compare(x._1) == 0) -y._2.compare(x._2) else -y._1.compare(x._1) } } val sorted: RDD[((String, Int), Int)] = valueToKey.repartitionAndSortWithinPartitions(new SortPartitioner(partitions)) val result = sorted.map { case (k, v) => (k._1, v.toString()) }.reduceByKey(_ + "," + _) result.saveAsTextFile(outputPath) // done sc.stop() } }
运行结果:
(2015-1,5,6,7,8,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,22,22) (2015-3,18,19,20,20,20,21,21,21,22,22,22,23,23,23,24,24,24,25,25,25,26,26,26,27,27,27,28,28,28,29,30) (2015-2,12,13,14,15,16,17,18,19,20,21,22,22,23,23,24,24,25,25,26,26,27,28,29,30,30,30,31,32)
-
hadoop的二级排序——实例
2018-05-31 11:08:14下面是一个hadoop的二级排序案例:编写mapReduce程序,将下面的左边内容进行排序,排序后变成右边的样子。mapReduce的程序如下:第一步:定义一个组合键的beanpackage com.bigdata.demo15_two_class_paixu; import ...想弄懂hadoop的二级排序,需要对hadoop的shufle过程非常清楚。
(注:在reduce接受数据阶段,setSortComparatorClass是对key进行的排序处理,setGroupingComparatorClass是对相同值的value进行排序处理)
下面是一个hadoop的二级排序案例:
编写mapReduce程序,将下面的左边内容进行排序,排序后变成右边的样子。
mapReduce的程序如下:
第一步:定义一个组合键的bean
第二步:自定义一个比较器,对key进行排序时使用package com.bigdata.demo15_two_class_paixu; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; import org.apache.hadoop.io.WritableComparable; /** * 自定义组合键,用于map阶段的sort小阶段 * @author Administrator * 2018年5月31日上午8:16:38 */ public class CombinationKey implements WritableComparable<CombinationKey>{ private String firstKey; private Integer secondKey; public String getFirstKey() { return firstKey; } public void setFirstKey(String firstKey) { this.firstKey = firstKey; } public Integer getSecondKey() { return secondKey; } public void setSecondKey(Integer secondKey) { this.secondKey = secondKey; } public void write(DataOutput out) throws IOException { out.writeUTF(this.firstKey); out.writeInt(this.secondKey); } public void readFields(DataInput in) throws IOException { this.firstKey=in.readUTF(); this.secondKey=in.readInt(); } public int compareTo(CombinationKey o) { return this.firstKey.compareTo(o.getFirstKey()); } }
package com.bigdata.demo15_two_class_paixu; import org.apache.hadoop.io.WritableComparable; import org.apache.hadoop.io.WritableComparator; /** * 自定义比较器 * @author Administrator * 2018年5月31日上午8:40:58 */ public class DefineCompparator extends WritableComparator{ protected DefineCompparator() { super(CombinationKey.class,true); } @Override public int compare(WritableComparable a, WritableComparable b) { CombinationKey ck1=(CombinationKey) a; CombinationKey ck2=(CombinationKey) b; int cp1 = ck1.getFirstKey().compareTo(ck2.getFirstKey()); if(cp1!=0) { //结束排序 return cp1; }else { return ck1.getSecondKey()-ck2.getSecondKey(); } } }
第三步:自定义一个分区器,在shuffle阶段使用
package com.bigdata.demo15_two_class_paixu; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.mapreduce.Partitioner; /** * 自定义分区 * @author Administrator * 2018年5月31日上午8:20:58 */ public class DefinedPartition extends Partitioner<CombinationKey, IntWritable>{ /** * @param key map输出,这里根据组合键的第一个值进行分区 * @param value map输出的key * @param numPartitions 分区总数,即reduce的个数 */ @Override public int getPartition(CombinationKey key, IntWritable value, int numPartitions) { return (key.getFirstKey().hashCode()&Integer.MAX_VALUE)%numPartitions; } }
第四步:自定义一个分区器,在shuffle阶段使用
package com.bigdata.demo15_two_class_paixu; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.mapreduce.Partitioner; /** * 自定义分区 * @author Administrator * 2018年5月31日上午8:20:58 */ public class DefinedPartition extends Partitioner<CombinationKey, IntWritable>{ /** * @param key map输出,这里根据组合键的第一个值进行分区 * @param value map输出的key * @param numPartitions 分区总数,即reduce的个数 */ @Override public int getPartition(CombinationKey key, IntWritable value, int numPartitions) { return (key.getFirstKey().hashCode()&Integer.MAX_VALUE)%numPartitions; } }
第五步:编写mapReduce程序
package com.bigdata.demo15_two_class_paixu; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class SecondSortMapReduce { /** * 使用内部类的形式,定义mapper程序 * @author Administrator * 2018年5月31日上午11:06:30 */ static class SecondSortMapper extends Mapper<LongWritable, Text, CombinationKey, IntWritable>{ String[] split=null; CombinationKey kv=new CombinationKey(); IntWritable v=new IntWritable(); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { split = value.toString().split(" "); kv.setFirstKey(split[0]); int vv = Integer.parseInt(split[1]); v.set(vv); kv.setSecondKey(vv); context.write(kv, v); } } /** * 使用内部类的形式,定义reduce程序 * @author Administrator * 2018年5月31日上午11:06:51 */ static class SecondSortReducer extends Reducer<CombinationKey, IntWritable, Text, Text>{ Text k=new Text(); Text v=new Text(); @Override protected void reduce(CombinationKey first_second, Iterable<IntWritable> seconds, Context context) throws IOException, InterruptedException { StringBuilder sb=new StringBuilder(); for(IntWritable second:seconds) { sb.append(second.get()+","); } k.set(first_second.getFirstKey()); v.set(sb.toString().substring(0, sb.toString().length()-1)); context.write(k, v); } } /** * 主函数 * @param args * @throws IOException * @throws ClassNotFoundException * @throws InterruptedException */ public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Configuration conf=new Configuration(); Job job = Job.getInstance(conf); job.setJarByClass(SecondSortMapReduce.class); job.setMapperClass(SecondSortMapper.class); job.setReducerClass(SecondSortReducer.class); //设置分区和reduce数目 job.setPartitionerClass(DefinedPartition.class); job.setNumReduceTasks(1); //设置自定义的分组策略 job.setGroupingComparatorClass(DefinedGroupSort.class); //设置自定义的比较策略 job.setSortComparatorClass(DefineCompparator.class); job.setMapOutputKeyClass(CombinationKey.class); job.setMapOutputValueClass(IntWritable.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); //设置输入数据 FileInputFormat.setInputPaths(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); boolean res = job.waitForCompletion(true); System.exit(res?0:1); } }
第六步:在hadoop集群上运行
将程序打包,放到hadoop集群,运行
查看排序结果:[hadoop@mini02 ~]$ hadoop jar \ > ./jars/Review06_hdfs-0.0.1-SNAPSHOT.jar \ > com.bigdata.demo15_two_class_paixu.SecondSortMapReduce \ > /paixu/input01 \ > /paixu/output03
[hadoop@mini03 ~]$ hdfs dfs -cat /paixu/output02/part-r-00000 hadoop 23,32,342 hive 12,42,204,2345 spark 3,16,349
-
excel时间排序之按年和月排序方法
2016-07-28 17:29:24内容提要:文章介绍excel时间排序中的分别按年和月讲解时间排序方法。对Excel感兴趣的朋友可加Excel学习交流群:284029260(www.itblw.com) 有时我们因工作原因需要进行excel时间排序。 第一,excel时间排序...内容提要:文章介绍excel时间排序中的分别按年和月讲解时间排序方法。对Excel感兴趣的朋友可加 Excel学习交流群:284029260(www.itblw.com)有时我们因工作原因需要进行excel时间排序。
第一,excel时间排序:按年排序
如下面的截图,需要对B列的时间按年排序。
方法很简单,单击数据——排序,弹出“排序”对话框,按“出生日期”升序排序就完成了excel时间排序:按年排序。
第二,excel时间排序:按月排序
还是以上面的截图为例,如果需要完成excel时间排序:按月排序,我们在C列添加一个辅助列,C2输入公式:=TEXT(B2,"mmdd"),下拉提取B列日期的月和日,然后再以“辅助列”为主要关键字排序即可完成excel时间排序:按月排序。
-
基于MapReduce的二次排序实现
2018-10-14 13:37:23二次排序综述 Mapreduce框架会自动对映射器生成的键完成排序.再启动归约器之前,映射器中生成的所有中间键-值对必然是按键有序的(而不是按值有序)。传入各个归约器的值并不是有序的,它们可能有任意的顺序。如果... -
选择排序&二分搜索
2020-01-24 16:18:49C语言实现选择排序和二分搜索 -
排序算法之二——选择排序
2019-08-19 22:21:20前言 选择排序是一种简单直观的排序方法,每次寻找排序中最小值,然后放在最末尾的位置。 -
C语言实例——四种排序算法(冒泡排序、选择排序、插入排序、快速排序)
2020-10-12 22:01:10C 语言排序算法BB Time一、冒泡排序1、原理2、代码二、选择排序1、原理2、代码三、插入排序1、原理2、代码四、快速排序1、原理2、代码 代码均以按从小到大排序为例 只写出来了排序的函数,减少博客冗余内容 BB ... -
根据时间按年、月、日、分组、排序
2016-06-04 10:47:38看到这个大家都知道吧,其实就是一个相册的显示效果,他说不会分组排序,然后让我给他看看,ok,废话不多说,开始解说。 二、题外话 在写代码之前一定搞清楚原理,这是个很重的过程,现在好多写代码的初学者都是看到... -
power bi排序_如何在Power BI中按时间顺序对月份进行排序
2020-07-23 19:23:56power bi 排序 In this article, I’m going to demonstrate how to sort months chronologically in Power BI. Visualizing sales data in a time period is one of the most influential ways of reporting. Oft... -
二路归并排序c语言实现
2017-10-16 16:07:05二路归并排序 c 语言实现。 -
MapReduce二次排序
2015-01-19 17:23:44我们知道MapReduce是按照key来进行排序的,那么如果有个需求就是先按照第一个字段排序,在第一个字段相等的情况下,按照第二个字段排序,这就是传说中的二次排序。 下面就具体说一下二次排序的实现方式 主要就是... -
如何按值对字典排序?
2019-12-01 14:35:32我正在写这个详细的说明,以说明人们通常所说的“我可以很容易地按键对字典进行排序,但是如何按值对字典进行排序”的意思-我认为OP试图解决这一问题。 解决方案是根据值对键列表进行排序,如上所示。 #13楼 ... -
MapReduce排序之 二次排序
2015-01-18 20:25:54一:背景 Hadoop中虽然有自动排序和分组,由于自带的排序是按照Key进行排序的,有些时候,我们希望...#需求1: 首先按照第一列数字升序排列,当第一列数字相同时,第二列数字也升序排列(列之间用制表符\t隔开) 3 3 3 -
SQL数据表中按中文数字一二三四五排序
2013-06-27 09:08:581.按月份排序 SELECT * FROM 表 ORDER BY CAST(月份列 as int) ...ORDER BY CHARINDEX (月份列,N'一月,二月,三月,四月,五月,六月,七月,八月,九月,十月,十一月,十二月') 是汉字的话不可能按ASCII码排序。 -
数据结构二叉排序树课程设计报告.docx
2020-11-09 01:05:56数据结构二叉排序 树课程设计报告 课程设计报告 数据结构 题目二叉排序树 姓名 学号 专业 班级 指导老师: 年 月曰 目录 TOC \o "1-5" \h \z 一 课程设计简介 4 二 原理分析及流程 3 2.1 原理分析 3 2.2 流程图 4 1... -
【Excel 教程系列第 15 篇】Excel 中的简单排序(升序 / 降序)、多条件排序、按颜色排序、自定义排序、...
2019-12-15 22:27:17排序功能在 Excel 中可谓是经常会用到的,今天就来说一下在 Excel 中的简单排序(升序 / 降序)、多条件排序、按颜色排序、自定义排序、以及巧用"升序"制作工资条。 -
【历史上的今天】3 月 1 日:个人计算机鼻祖问世;天涯社区上线;Shell 排序算法作者诞生
2022-03-01 00:09:193 月 1 日,历史上的今天,Shell 排序算法的创造者 Donald Shell 出生;个人计算机鼻祖 Xerox Alto 诞生;天涯社区上线。 -
按日期进行排序(结构体)
2019-05-15 08:47:46小明希望将自己的通讯录按好友的生日排序排序,这样就查看起来方便多了,也避免错过好友的生日。为了小明的美好愿望,你帮帮他吧。小明的好友信息包含姓名、出生日期。其中出生日期又包含年、月、日三部分信息。输入... -
Mysql 分别按月, 日为组group,进行统计排序order
2014-07-01 20:06:17在数据库中我们经常用sql语句对表进行简单的统计排序,对于日期字段,我们可以分别提出来日期中的年、月、日,分别进行group,order。 -
php将二维数组按日期(支持Ymd和Ynj格式日期)排序
2019-02-19 14:06:29TP5.1框架自带排序无法满足查询数据的Ynj格式日期排序,所以写了一个如下“将二维数组按日期(支持Ymd和Ynj格式日期)排序”。 思路: 将所有日期转化成时间戳保存在新数组里面(新数组1和新数组2), 将新数组2... -
链表排序
2020-12-09 10:39:30选择排序就是每次选一个最小的,放到前面 为了方便操作,创造一个头结点 /** * Definition of singly-linked-list: * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { * ... -
CCF系列题解--2015年3月第二题 数字排序
2021-03-26 15:56:31第二行包含n个整数,相邻的整数之间用一个空格分隔,表示所给定的整数。 输出格式 输出多行,每行包含两个整数,分别表示一个给定的整数和它出现的次数。按出现次数递减的顺序输出。如果两个整数出现的次数... -
巧记稳定的排序(附带判断排序稳定排序例题)
2020-10-28 20:05:10不知道有没有童鞋和我一样,总是记不住哪些排序方法是...1 北京理工大学2005一、10 (1分)】排序算法的稳定性是指( )。A.经过排序之后,能使值相同的数据保持原顺序中的相对位置不变 B.经过排序之后,能使值相同的数据保持 -
二维数组每一行单独排序
2016-06-15 14:57:47* Function : 二维数组每一行单独排序 C++实现 * Created on : 2016年6月15日 * Author : beijiwei@qq.com * Copyright : 欢迎大家和我一起交流学习,转载请保持源文件的完整性。 任何单位和个人不经本人允许不得...