关于日期格式为yyyyMMddHHmmss的14位按日、月、年、季度以及去重、未去重查询
来源:互联网 发布:想要女生的身体 知乎 编辑:程序博客网 时间:2024/05/16 19:38
接触SQL不多,所以写的不好大家见谅。
关于oracle数据库中有date格式的日记在这不写,网上比较多。这varchar2格式或者char格式的日期网上较少,最近由于要使用到所以便写出来以便大家提供交流意见。关于去重的季度SQL由于本身接触SQL不多未能写出,望大家能够提供一些好的建议。
表名以及字段:ec_user_subscription
字段只是部分的
字段 类型
id number(20,0)//主键
user_id number(20,0) //用户i,user表中主键
refresh_date char(14)//日期格式:yyyyMMddHHmmss
未去重:
关键语句:
substr(u.refresh_date,0,8) 截取字符串,0位开始截取8个长度。
日统计
select substr(u.refresh_date,0,8) dateday ,count(*) from ec_user_subscription u
group by substr(u.refresh_date,0,8) order by dateday;
月统计
select substr(u.refresh_date,0,6) dateday ,count(*) from ec_user_subscription u
group by substr(u.refresh_date,0,6) order by dateday;
季度统计
select substr(refresh_date,0,4) as yeardate ,--年份
sum(decode(substr(refresh_date,5,2),'01',1,'02',1,'03',1,0)) as quarter1th ,--第一季度
sum(decode(substr(refresh_date,5,2),'04',1,'05',1,'06',1,0)) as quarter2nd ,--第二季度
sum(decode(substr(refresh_date,5,2),'07',1,'08',1,'09',1,0)) as quarter3rd ,--第三季度
sum(decode(substr(refresh_date,5,2),'10',1,'11',1,'12',1,0)) as quarter4th --第四季度
from ec_user_subscription u
group by substr(refresh_date,0,4) order by yeardate;
关于该季度统计的SQL,我查询100万条数据得出的是2S多,如图:
年度统计
select substr(u.refresh_date,0,4) dateday ,count(*) from ec_user_subscription u
group by substr(u.refresh_date,0,4) order by dateday;
去重SQL
--去重后查询出每日的用户量--
select u.refreshdate dateday ,count(*) from (
select distinct user_id ,substr(refresh_date,0,8) as refreshdate from ec_user_subscription
) u
group by u.refreshdate order by dateday;
--去重后查询出每月的用户量--
select u.refreshdate dateday ,count(*) from (
select distinct user_id ,substr(refresh_date,0,6) as refreshdate from ec_user_subscription
) u
group by u.refreshdate order by dateday;
--去重后查询出每年的用户量--
select u.refreshdate dateday ,count(*) from (
select distinct user_id ,substr(refresh_date,0,4) as refreshdate from ec_user_subscription
) u
group by u.refreshdate order by dateday;
- 关于日期格式为yyyyMMddHHmmss的14位按日、月、年、季度以及去重、未去重查询
- C# 14位日期型字符串yyyyMMddHHmmss转变为日期格式
- java 验证14位日期验证yyyyMMddhhmmss
- yyyyMMddHHmmss转变为日期格式
- yyyyMMddHHmmss转变为日期格式
- yyyyMMddHHmmss转变为日期格式
- yyyyMMddHHmmss转变为日期格式
- sqlserver 日期比较、日期查询常用语句:月的第一天,季度的第一天等
- mysql按日、月、周、季度分组
- 查询数据按日期排序 先排日期有值的 在排为null的
- javascript实现年、季度、月、时间段的查询条件切换
- java8时间 yyyyMMddHHmmss格式转为日期
- [MSSQL]日期集合-周、月、季度、年等计算以及自定义函数
- SQL日期集合-周、月、季度、年等计算以及自定义函数
- java 根据输入的日期返回日期中的年份月份,格式为“2009年3月”
- SQL 按周,月,季度,年查询统计数据
- SQL 按周,月,季度,年查询统计数据
- SQL 按周,月,季度,年查询统计数据
- 第四章 4.10节练习
- using thymeleaf之五条件表达式
- 个人项目:统计文本中的单词出现频率
- 点击率校准
- set、env、export的区别
- 关于日期格式为yyyyMMddHHmmss的14位按日、月、年、季度以及去重、未去重查询
- H3C文档NAT专题
- springMVC添加用户demo
- jsp页面杂记
- Sicily1121
- 堆和栈的区别以及衍生出来的栈的效率为什么比堆高
- sgu118:Digital root
- 你想创业吗?我们在寻找以下有慧眼的您:
- X86_64上没有写C函数声明导致的BUG(warning: initialization makes pointer from integer without a cast)