分组统计SQL
Itpub上遇到一个求助写SQL的帖子,感觉很有意思,于是写出来看看,要求如下:
有个计划表1, 记录物料的年度计划量
有个实际使用情况表2,记录实际使用情况。
最后要出个统计表,把计划和实际的数据结合到一起进行统计,
有特殊的情况就是可能部分物料有计划但是没有实际使用,有的物料无计划但是有实际使用情况,
要把这些数据合并到一起按照季度进行统计并算差异量,能实现吗??

于是建表插数据,表结构如下:
create table plan(OBJ varchar2(10),YEAR number(4),PLAN_COUNT number(10));
create table actual(OBJ varchar2(10),YEAR number(4),SEASON number(10),USED number(10));
语句如下:
with t1 as (
select distinct a.obj,p.plan_count,a.year,
case when a.season=1 then sum(a.used) over (partition by a.obj,a.season) else null end as s1,
case when a.season=2 then sum(a.used) over (partition by a.obj,a.season) else null end as s2,
case when a.season=3 then sum(a.used) over (partition by a.obj,a.season) else null end as s3,
case when a.season=4 then sum(a.used) over (partition by a.obj,a.season) else null end as s4,
sum(a.used) over (partition by a.obj) as sum_count
from actual a left join plan p on a.obj=p.obj order by 1,4
)
select temp.obj,nvl(temp.plan_count,0),temp.year,temp.q1,temp.q2,temp.q3,temp.q4,nvl(temp.sum_count,0),(nvl(temp.plan_count,0)-nvl(temp.sum_count,0)) as diff
from(
select distinct t1.obj,t1.plan_count,t1.year,
sum(s1) over (partition by t1.obj) as Q1,
sum(s2) over (partition by t1.obj) as Q2,
sum(s3) over (partition by t1.obj) as Q3,
sum(s4) over (partition by t1.obj) as Q4,t1.sum_count from t1
union all
select pl.obj,pl.plan_count,pl.year,null,null,null,null,null from plan pl where pl.obj not in (select distinct obj from actual)
) temp
order by 1
用到了nvl,case when,not in,over partition,sum,union,left join,with as等写法,常年写SQL的应该有更加效率的写法,有待发现之后补充到此进行完善。
发现一个更简明的写法:
select
distinct case when a.obj is not null then a.obj else p.obj end as obj,
nvl(p.plan_count,0) as plan_count,
sum(case when a.season=1 then a.used end) as Q1,
sum(case when a.season=2 then a.used end) as Q2,
sum(case when a.season=3 then a.used end) as Q3,
sum(case when a.season=4 then a.used end) as Q4,
sum(nvl(a.used,0)) as sum_season,
nvl(p.plan_count,0)-sum(nvl(a.used,0)) as diff
from plan p full join actual a on p.obj=a.obj
group by case when a.obj is not null then a.obj else p.obj end,nvl(p.plan_count,0)
order by 1
分组统计SQL的更多相关文章
- mysql 分组统计SQL语句
1.按照特定字段: 2.在某一日期范围内: 3.按日.按月统计: 4.动态传入数据库表名称. select <if test="dateType=="d"" ...
- 分组统计SQL(mysql)
<select id="orderProductStatistics" resultMap="ProductStatisticsVOMap"> SE ...
- Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等)
Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等) 子查询 描述:查询订单数超过5的顾客信息 查询句法: var 子查询 = from c i ...
- SQL 分组统计 行转列 CASE WHEN 的使用
原文地址:http://blog.itpub.net/26451903/viewspace-733526 原文在分组统计部分 sql是有问题的 本文已将sql改正 已用红色标记 Cas ...
- SQL Fundamentals: 分组统计查询(FROM-WHERE-GROUPBY-HAVING-SELECT-ORDER BY)
SQL Fundamentals || Oracle SQL语言 统计函数 单字段分组统计(GROUP BY) 多字段分组统计 HAVING子句 控制操作的显示列:基本的SELECT语句 控制行:限定 ...
- 【SQL Server 2012】按倒序存储“分组统计”结果的临时表到新建表
程序预先说明: 本文访问的数据库是基于存有RDF三元组的开源数据库Localyago修改的库,其中只有一个表,表中有五个属性:主语subject.谓语predict.宾语object.主语的编号sub ...
- sql group by hour 按小时分组统计
Time字段以小时分组统计 select datepart(hour,time) hour,count(1) count from table where Similarity<75 group ...
- sql根据时间戳按年月日分组统计
sql根据时间戳按年月日分组统计,用于按日期分类: create_time为时间格式(字段名create_time 根据自己表字段修改,数据库中存为201610071202) SELECT DATE_ ...
- 010.简单查询、分组统计查询、多表连接查询(sql实例)
-------------------------------------day3------------ --添加多行数据:------INSERT [INTO] 表名 [(列的列表)] --SEL ...
随机推荐
- jvm详情——1、堆中存什么?栈中存什么?
数据类型 Java虚拟机中,数据类型可以分为两类:基本类型和引用类型.基本类型的变量保存原始值,即:他代表的值就是数值本身:而引用类型的变量保存引用值.“引用值”代表了某个对象的引用,而不是对象本身, ...
- 【Go】优雅的读取http请求或响应的数据
[Go]优雅的读取http请求或响应的数据 原文链接:https://blog.thinkeridea.com/201901/go/you_ya_de_du_qu_http_qing_qiu_huo_ ...
- JavaScript基础回顾一(类型、值和变量)
请看代码并思考输出结果 var scope = 'global'; function f(){ console.log(scope); var scope = 'local'; console.log ...
- React Fiber源码分析 第三篇(异步状态)
先附上流程图~ 调用setState时, 会调用classComponentUpdater的enqueueSetState方法, 同时将新的state作为payload参数传进 enqueueSetS ...
- List<T>集合的Sort自定义排序用法简单解析
List<T>集合的Sort自定义排序用法简单解析: 如下:一系列无序数字,如果想要他们倒序排列,则使用如下代码: 那么如何理解这段代码呢? (x,y)表示相邻的两个对象,如果满足条件:x ...
- 【转载】C#递归删除文件夹目录及文件
在C#文件操作过程中,有时候需要删除相应目录,如果文件夹中含有其他文件夹或者文件,也需要一并进行删除,此时可能就需要使用递归来删除文件夹目录以及文件,递归过程中,如果遍历的对象是文件夹,则删除文件夹, ...
- spring boot 使用第三方jar的方法
2018/02/02 更新 mvnrepository.com已经提供了ms jdbc 的jar URL: http://mvnrepository.com/artifact/com.microsof ...
- react学习(三)之生命周期/refs/受控组件 篇
挂载/卸载 //在类组件中 class Clock extends React.Component { constructor(props) { super(props); this.state = ...
- ASP.NET MVC 使用Jquery异步操作JS代码
$(function () { var ajaxFormSubmit = function () { var $form = $(this); var options = { url: $form.a ...
- 基于jquery二维码生成插件qrcode
1.首先在页面中加入jquery库文件和qrcode插件. ? 1 2 <script type="text/javascript" src="jquery.js& ...