统计合计函数

--统计 小计 合计

create table trade_flow (user_code varchar2(12),trans_time date,trans_amount number);

insert into trade_flow values(‘321000‘,to_date(‘2015-09-08 11:50:37‘,‘yyyy-mm-dd hh24:mi:ss‘),300);
insert into trade_flow values(‘321000‘,to_date(‘2015-09-08 18:47:20‘,‘yyyy-mm-dd hh24:mi:ss‘),210);
insert into trade_flow values(‘321002‘,to_date(‘2015-09-09 16:41:05‘,‘yyyy-mm-dd hh24:mi:ss‘),190);
insert into trade_flow values(‘321002‘,to_date(‘2015-09-09 20:11:15‘,‘yyyy-mm-dd hh24:mi:ss‘),90);
insert into trade_flow values(‘321000‘,to_date(‘2015-09-10 09:00:07‘,‘yyyy-mm-dd hh24:mi:ss‘),100);
insert into trade_flow values(‘321000‘,to_date(‘2015-09-10 10:30:25‘,‘yyyy-mm-dd hh24:mi:ss‘),270);
insert into trade_flow values(‘321000‘,to_date(‘2015-09-10 15:06:32‘,‘yyyy-mm-dd hh24:mi:ss‘),50);
insert into trade_flow values(‘321002‘,to_date(‘2015-09-10 12:33:45‘,‘yyyy-mm-dd hh24:mi:ss‘),150);
insert into trade_flow values(‘321002‘,to_date(‘2015-09-10 17:23:55‘,‘yyyy-mm-dd hh24:mi:ss‘),350);

select * from trade_flow order by 2;

select user_code,TRUNC(trans_time),SUM(trans_amount),
grouping(user_code),grouping(TRUNC(trans_time)) from trade_flow 
GROUP BY ROLLUP(user_code,TRUNC(trans_time))
ORDER by user_code,TRUNC(trans_time);

select CASE WHEN (grouping(TRUNC(trans_time))=1 AND grouping(user_code)=1) THEN ‘总计‘
            WHEN grouping(TRUNC(trans_time))=1 
              THEN ‘小计‘                
           ELSE     user_code END user_code,
TRUNC(trans_time),SUM(trans_amount),
grouping(user_code),grouping(TRUNC(trans_time)) from trade_flow 
GROUP BY ROLLUP(user_code,TRUNC(trans_time))
ORDER by TRUNC(trans_time);

select CASE WHEN (grouping(TRUNC(trans_time))=1 AND grouping(user_code)=1) THEN ‘总计‘
            WHEN grouping(TRUNC(trans_time))=1 
              THEN ‘小计‘                
           ELSE     user_code END user_code,
TRUNC(trans_time),SUM(trans_amount),
grouping(user_code),grouping(TRUNC(trans_time)), 
GROUPING_id(user_code,TRUNC(trans_time)),--是否都为空
GROUPING_id(user_code,TRUNC(trans_time))
from trade_flow 
GROUP BY ROLLUP(user_code,TRUNC(trans_time))
ORDER by user_code,TRUNC(trans_time);

select CASE WHEN (grouping(trans_amount)=1 ) THEN ‘小计‘
            --WHEN (grouping(TRUNC(trans_time))=0 AND grouping(user_code)=1) THEN user_code
            /*WHEN grouping(TRUNC(trans_time))=1 
              THEN ‘小计‘ */               
           ELSE     user_code END user_code,
TRUNC(trans_time),SUM(trans_amount),
grouping(user_code),grouping(TRUNC(trans_time)),grouping(trans_amount) from trade_flow 
GROUP BY GROUPING SETS((user_code,TRUNC(trans_time),trans_amount), (user_code,TRUNC(trans_time)));

select decode(grouping(trans_amount),1,‘小计‘,0,user_code) user_code,
TRUNC(trans_time),SUM(trans_amount),
grouping(user_code),grouping(TRUNC(trans_time)),grouping(trans_amount),
GROUPING_id(user_code,TRUNC(trans_time)),--是否都为空
GROUPING_id(trans_amount,TRUNC(trans_time))
from trade_flow 
GROUP BY GROUPING SETS((user_code,TRUNC(trans_time),trans_amount), (user_code,TRUNC(trans_time)));

select * from trade_flow order by 2;
select decode(grouping(trans_amount),1,‘小计‘,0,user_code) user_code,
TRUNC(trans_time),SUM(trans_amount),
grouping(user_code),grouping(TRUNC(trans_time)),grouping(trans_amount) from trade_flow 
GROUP BY GROUPING SETS((user_code,TRUNC(trans_time),trans_amount), (user_code,TRUNC(trans_time)));

select t.user_code,t.trans_time,t.trans_amount from trade_flow t WHERE t.user_code=321000;

SELECT * FROM (
       select t.user_code,t.trans_time,t.trans_amount from trade_flow t WHERE t.user_code=321000)
PIVOT (
      MAX(t.trans_time) FOR fieldname IN (
          ‘300‘,
          ‘210‘,
          ‘100‘,
          ‘270‘,
          ‘50‘
          )
) ORDER BY 1
时间: 2024-08-25 16:20:42

统计合计函数的相关文章

Extjs GridPanel在有分页的情况下服务端统计合计实现方式

1.应用场景 在有分页的时候计算合计就不能采用Extjs gridpanel 提供的ftype: summary方式解决,否则只计算当前页的合计(因为数据只提取了当前页): 2.解决办法 重写Ext.grid.feature.Summary中的generateSummaryData函数 //方法一,临时重写 Ext.grid.feature.Summary.override({ generateSummaryData:function(){ return this.view.store.getP

1113: 递归调用的次数统计(函数专题)

题目描述 如下程序的功能是计算 Fibonacci数列的第n项.函数fib()是一个递归函数.请你改写该程序,计算第n项的同时,统计调用了多少次函数fib(包括main()对fib()的调用). #include<stdio.h> int fib(int k); int main(void ) { int n; scanf("%d", &n); printf("%d\n", fib(n)); return 0; } int fib(int k)

SQL合计函数(avg、count、first、last、max、min、sum)

Aggregate 函数的操作面向一系列的值,并返回一个单一的值. 注释:如果在 SELECT 语句的项目列表中的众多其它表达式中使用 SELECT 语句,则这个 SELECT 必须使用 GROUP BY 语句! 首先所有的SQL实例使用到下面的一张表 O_Id OrderDate OrderPrice Customer 1 2008/12/29 1000 Bush 2 2008/11/23 1600 Carter 3 2008/10/05 700 Bush 4 2008/09/28 300 B

装饰器1_统计时间函数装饰欢迎登录函数

import time def timmer(func): #这里是要把需要装饰的函数名传递进来,方便后面调用.如果不传函数名而是直接调用,在调用的位置会报:RecursionError: maximum recursion depth exceeded while calling a Python object def wrapper(*args,**kwargs): time_start = time.time() res=func(*args,**kwargs) #1.这里要使用func传

python 装饰器统计某个函数的运行时间

import datetime def count_time(func): def int_time(*args, **kwargs): start_time = datetime.datetime.now() # 程序开始时间 func() over_time = datetime.datetime.now() # 程序结束时间 total_time = (over_time-start_time).total_seconds() print('程序共计%s秒' % total_time) r

GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组

1 drop table orders; 2 create table orders ( 3 o_id int auto_increment primary key, 4 orderdate date, 5 orderprice int, 6 customer varchar(100) 7 ); 8 9 insert into orders(orderdate, orderprice, customer) values('2008-12-29', 1000, 'Bush'); 10 insert

php统计字数函数

function countWords($str){ echo (mb_strlen($str, 'utf8') + strlen($str))/2; }

六、聚合函数、数据分组

1.AVG函数:求平均值. 需要注意的是:每个字段都必须使用一个AVG函数: select avg(id) as id_avg,avg(price) as price_avg from tb2; 如上,这样才能分别求出id和price的平均值. AVG函数忽略NULL. 2.COUNT函数:统计数量. 使用 COUNT(*) 对表中行的数目进行计数,不管表列中包含的是空值( NULL )还是非空值. 使用 COUNT(column) 对特定列中具有值的行进行计数,忽略NULL值. select

用于 SELECT 和 WHERE 子句的函数

1 一个 SQL 语句中的 select_expression 或 where_definition 可由任何使用了下面所描述函数的表达式组成. 2 3 包含 NULL 的表达式总是得出一个 NULL 值结果,除非表达式中的操作和函数在文档中有另外的说明. 4 5 注意:在一个函数名和跟随它的括号之间必须不存在空格.这有助于 MySQL 语法分析程序区分函数调用和对恰巧与函数同名表或列的引用.然而,参数左右两边的空格却是允许的. 6 7 你可以强制 MySQL 接受函数名后存在空格的形式,这需要