SQL2000 统计每周,每月,每季,每年的数据
表order(订单表)
- order_id client_id(客户ID) order_time(订单时间)
- 1 1 2007-1-5
- 2 1 2007-1-7
- 3 1 2007-6-5
- 4 3 2007-2-5
- 5 3 2007-2-18
表item(明细表)
- item_id order_id(明细表) pro_id(产品ID) pro_amount(数量) pro_price(单价)
- 1 1 1 10 10.00
- 2 1 3 5 15.00
- 3 2 1 5 12.00
- 4 3 2 10 8.00
- 5 4 3 2 15.00
- 6 5 2 6 10.00
如何汇总得到如下效果:
1.按年得到总金额
client_id 1月 2月 3月 4月 5月 6月 ... 12月
1 235.00 0.00 0.00 0.00 0.00 80.00 ... 0.00
3 0.00 90.00 0.00 0.00 0.00 0.00... 0.00
2.按周得到总金额:
client_id 周一 周二 ....周日
1
3
3.按月得到总金额:
client_id 1号 2号 3号.....31号
1
3
4.按季:
client_id 第一季度 第二季度 第二季度 第四季度
1
3
================================================================
数据准备:
- create table [order]
- (
- order_id int,
- client_id int,
- order_time datetime
- )
- create table item
- (
- item_idint,
- order_id int,
- pro_id int,
- pro_amount int,
- pro_price int
- )
- insert into [order]
- select 1,1,'2007-1-5'
- union all
- select 2,1,'2007-1-7'
- union all
- select 3,1,'2007-6-5'
- union all
- select 4,3,'2007-2-5'
- union all
- select 5,3,'2007-2-18'
- insert into item
- select 1,1,1,10,10
- union all
- select 2,1,3,5,15
- union all
- select 3,2,1,5,12
- union all
- select 4,3,2,10,8
- union all
- select 5,4,3,2,15
- union all
- select 6,5,2,6,10
按年统计:
- select client_id ,
- sum(case when datepart(month,order_time) = 1 then pro_amount*pro_price else 0 end) '1月',
- sum(case when datepart(month,order_time) = 2 then pro_amount*pro_price else 0 end) '2月',
- sum(case when datepart(month,order_time) = 3 then pro_amount*pro_price else 0 end) '3月',
- sum(case when datepart(month,order_time) = 4 then pro_amount*pro_price else 0 end) '4月',
- sum(case when datepart(month,order_time) = 5 then pro_amount*pro_price else 0 end) '5月',
- sum(case when datepart(month,order_time) = 6 then pro_amount*pro_price else 0 end) '6月',
- sum(case when datepart(month,order_time) = 7 then pro_amount*pro_price else 0 end) '7月',
- sum(case when datepart(month,order_time) = 8 then pro_amount*pro_price else 0 end) '8月',
- sum(case when datepart(month,order_time) = 9 then pro_amount*pro_price else 0 end) '9月',
- sum(case when datepart(month,order_time) = 10 then pro_amount*pro_price else 0 end) '10月',
- sum(case when datepart(month,order_time) = 11 then pro_amount*pro_price else 0 end) '11月',
- sum(case when datepart(month,order_time) = 12 then pro_amount*pro_price else 0 end) '12月'
- from order,item where order.order_id = item.order_id
- group by client_id
按季度统计:
- select client_id ,
- sum(case when datepart(quarter,order_time) = 1 then pro_amount*pro_price else 0 end) '第一季度',
- sum(case when datepart(quarter,order_time) = 2 then pro_amount*pro_price else 0 end) '第二季度',
- sum(case when datepart(quarter,order_time) = 3 then pro_amount*pro_price else 0 end) '第三季度',
- sum(case when datepart(quarter,order_time) = 4 then pro_amount*pro_price else 0 end) '第四季度'
- from order,item where order.order_id = item.order_id
- group by client_id
按周统计
- select client_id ,
- sum(case when datepart(week,order_time) = 1 then pro_amount*pro_price else 0 end) '第一周',
- sum(case when datepart(week,order_time) = 2 then pro_amount*pro_price else 0 end) '第二周',
- sum(case when datepart(week,order_time) = 3 then pro_amount*pro_price else 0 end) '第三周',
- sum(case when datepart(week,order_time) = 4 then pro_amount*pro_price else 0 end) '第四周',
- ......................
- from order,item where order.order_id = item.order_id
- group by client_id
按日统计:
- select client_id , convert(varchar(7),order_time,120) 月份,
- sum(case when datepart(day,order_time) = 1 then pro_amount*pro_price else 0 end) '1',
- sum(case when datepart(day,order_time) = 2 then pro_amount*pro_price else 0 end) '2',
- sum(case when datepart(day,order_time) = 3 then pro_amount*pro_price else 0 end) '3',
- sum(case when datepart(day,order_time) = 4 then pro_amount*pro_price else 0 end) '4',
- ......................
- sum(case when datepart(day,order_time) = 4 then pro_amount*pro_price else 0 end) '31'
- from order,item where order.order_id = item.order_id
- group by client_id,convert(varchar(7),order_time,120)
按周一、二计算(假设order_time为日期型数据,即不含有时,分,秒等):
- select client_id ,
- sum(case when order_time = DATEADD(wk, DATEDIFF(wk,0,getdate()), 0) - 1 then pro_amount*pro_price else 0 end) '周日',
- sum(case when order_time = DATEADD(wk, DATEDIFF(wk,0,getdate()), 0) then pro_amount*pro_price else 0 end) '周一',
- sum(case when order_time = DATEADD(wk, DATEDIFF(wk,0,getdate()), 0) + 1 then pro_amount*pro_price else 0 end) '周二',
- sum(case when order_time = DATEADD(wk, DATEDIFF(wk,0,getdate()), 0) + 2 then pro_amount*pro_price else 0 end) '周三',
- sum(case when order_time = DATEADD(wk, DATEDIFF(wk,0,getdate()), 0) + 3 then pro_amount*pro_price else 0 end) '周四',
- sum(case when order_time = DATEADD(wk, DATEDIFF(wk,0,getdate()), 0) + 4 then pro_amount*pro_price else 0 end) '周五',
- sum(case when order_time = DATEADD(wk, DATEDIFF(wk,0,getdate()), 0) + 5 then pro_amount*pro_price else 0 end) '周六'
- from order,item where order.order_id = item.order_id
- group by client_id
按周一、二计算(假设order_time为日期型数据,同时含有时,分,秒等,要转换一下。):
- select client_id ,
- sum(case when convert(varchar(10),order_time,120) = convert(varchar(10),DATEADD(wk,DATEDIFF(wk,0,getdate()),0)-1,120) then pro_amount*pro_price else 0 end) '周日',
- sum(case when convert(varchar(10),order_time,120) = convert(varchar(10),DATEADD(wk,DATEDIFF(wk,0,getdate()),0),120) then pro_amount*pro_price else 0 end) '周一',
- sum(case when convert(varchar(10),order_time,120) = convert(varchar(10),DATEADD(wk,DATEDIFF(wk,0,getdate()),0)+1,120) then pro_amount*pro_price else 0 end) '周二',
- sum(case when convert(varchar(10),order_time,120) = convert(varchar(10),DATEADD(wk,DATEDIFF(wk,0,getdate()),0)+2,120) then pro_amount*pro_price else 0 end) '周三',
- sum(case when convert(varchar(10),order_time,120) = convert(varchar(10),DATEADD(wk,DATEDIFF(wk,0,getdate()),0)+3,120) then pro_amount*pro_price else 0 end) '周四',
- sum(case when convert(varchar(10),order_time,120) = convert(varchar(10),DATEADD(wk,DATEDIFF(wk,0,getdate()),0)+4,120) then pro_amount*pro_price else 0 end) '周五',
- sum(case when convert(varchar(10),order_time,120) = convert(varchar(10),DATEADD(wk,DATEDIFF(wk,0,getdate()),0)+5,120) then pro_amount*pro_price else 0 end) '周六'
- from order,item where order.order_id = item.order_id
- group by client_id
- --按周一、二计算(假设order_time为日期型数据,同时含有时,分,秒等,要转换一下。)
- select client_id ,
- sum(case when convert(varchar(10),order_time,120) = convert(varchar(10),DATEADD(wk,DATEDIFF(wk,0,getdate()),0)-1,120) then pro_amount*pro_price else 0 end) '周日',
- sum(case when convert(varchar(10),order_time,120) = convert(varchar(10),DATEADD(wk,DATEDIFF(wk,0,getdate()),0),120) then pro_amount*pro_price else 0 end) '周一',
- sum(case when convert(varchar(10),order_time,120) = convert(varchar(10),DATEADD(wk,DATEDIFF(wk,0,getdate()),0)+1,120) then pro_amount*pro_price else 0 end) '周二',
- sum(case when convert(varchar(10),order_time,120) = convert(varchar(10),DATEADD(wk,DATEDIFF(wk,0,getdate()),0)+2,120) then pro_amount*pro_price else 0 end) '周三',
- sum(case when convert(varchar(10),order_time,120) = convert(varchar(10),DATEADD(wk,DATEDIFF(wk,0,getdate()),0)+3,120) then pro_amount*pro_price else 0 end) '周四',
- sum(case when convert(varchar(10),order_time,120) = convert(varchar(10),DATEADD(wk,DATEDIFF(wk,0,getdate()),0)+4,120) then pro_amount*pro_price else 0 end) '周五',
- sum(case when convert(varchar(10),order_time,120) = convert(varchar(10),DATEADD(wk,DATEDIFF(wk,0,getdate()),0)+5,120) then pro_amount*pro_price else 0 end) '周六'
- from order,item where order.order_id = item.order_id
- group by client_id
SQL2000 统计每周,每月,每季,每年的数据相关推荐
- MySQL查询每天每周每月每季每年的数据等常用统计查询
查询每天的数据: SELECT COUNT(1) AS countNumber, DATE_FORMAT(createTime,'%Y-%m-%d') AS dateTime FROM ...
- SQL语句统计每天、每月、每年的 数据
1.每年 select year(ordertime) 年, sum(Total) 销售合计 from 订单表 group by year(ordertime) 2.每月 select year(or ...
- SQL语句统计每天、每月、每年的数据
SQL语句统计每天.每月.每年的数据 1.每年 select year(ordertime) 年, sum(Total) 销售合计 from 订单表 group by year(ordertime) ...
- Mysql 统计每周,半个月的数据
1.统计每周的数据 select DATE_FORMAT(sgfssj,'%Y%u') weeks,count(sgbh) count from s group by weeks; 2.还有采用we ...
- 数据库专题——SQL语句统计每天、每月、每年的数据
SQL语句统计每天.每月.每年的数据 1.每年 select year(ordertime) 年, sum(Total) 销售合计 from 订单表 group by year(orderti ...
- Python数模笔记-StatsModels 统计回归(3)模型数据的准备
1.读取数据文件 回归分析问题所用的数据都是保存在数据文件中的,首先就要从数据文件读取数据. 数据文件的格式很多,最常用的是 .csv,.xls 和 .txt 文件,以及 sql 数据库文件的读取 . ...
- 【Excel VBA】逐个读取Excel文件并将内容复制到汇总表中——每月自动汇总考勤数据实例
纲举目张 说明 关键技术点:Workbooks.Open 代码code 运行效果图 使用说明 说明 上一讲[Excel VBA]利用数组.集合.循环等检查各单位报送情况--每月自动汇总考勤数据实例已经 ...
- 【Excel VBA】遍历获取文件夹下所有文件——每月自动汇总考勤数据实例
纲举目张 说明 关键技术点:Dir函数 代码code 运行效果图 使用说明 说明 本次所讲的遍历获取某文件夹下所有文件,其实是我的<每月自动汇总考勤数据>案例中的其中一个知识点,近期我将会 ...
- 友盟-统计不到线上应用数据的坑
原文链接: 友盟-统计不到线上应用数据的坑 简书主页:http://www.jianshu.com/users/37f2920f6848 Github主页:https://github.com/Maj ...
最新文章
- python编程试题定位列表元素的函数是_笨办法学Python 习题 34: 访问列表的元素
- kali linux有线连接不见网络图标不见(解决方案)
- C# 对象名无效 问题
- python 二叉树递归时明明已经得到了结果,但是返回None
- flutter listview 滚动到底部_flutter实战项目,教你使用flutter打造仿微信app页面!...
- js 去空格 和 获得字节数
- 软件行业各职位英文缩写
- HDU4282 A very hard mathematic problem 快速幂
- 什么是大数据技术架构
- 如何将PPT制成二维码?
- RK3399 Android7.1修改系统默认壁纸
- javaweb课程设计网上书店
- 不畏浮云遮望眼,自缘身在最高层
- 技术点:前端缓存分类及使用
- 视频按照bbox切割算法
- 针对英夫利昔单抗或阿达木单抗的抗体存在与否决定转用依那西普的疗效
- 摄像机Rtsp地址格式大全
- 流量累计程序 博途v15编写的西门子流量累计程序
- 【网络流】——搞搞dinic
- 【实习_面试全程辅导分享】海康威视_测开面经
热门文章
- 配置通过Apache(httpd)访问Subversion(SVN)1.7资源库
- eVC++就是eVC++啊
- 杭电1597_find the nth digit
- nyoj 720 项目安排(dp+二分优化)
- nyoj 791 Color the fence(贪心)
- nyoj36最长公共子序列 动态规划
- 关注,粉丝关系的数据库设计
- Kafka broker配置介绍 (四)
- android内部通信handler
- 项目后台的最新认识和对MVC封装性、可维护性的更深刻认识!