按日,周,年统计,无的数据补充0

news2025/2/24 4:02:54

需求:按日-周-年统计。统计涉及到3张表数据。
在这里插入图片描述在这里插入图片描述

写sql。先把3张表数据摘取出来,只需对3张表的时间做分组统计即可。
在这里插入图片描述
按日统计

select DAY(dateff) as time,IFNULL(count(id),0)as num
from(select create_time as dateff,id as id from cz_taxi_orders UNION ALL
select create_time as dateff,id as id from ctky_passenger_transport_order UNION ALL
select pay_time as dateff,id as id from bus_order) as a
GROUP BY time

按年统计

#按年 需求:统计最近十年数据
select YEAR(dateff) as time,IFNULL(count(id),0)as num
from(select create_time as dateff,id as id from cz_taxi_orders UNION ALL
select create_time as dateff,id as id from ctky_passenger_transport_order UNION ALL
select pay_time as dateff,id as id from bus_order) as a
WHERE YEAR(dateff)>year(now())-10
GROUP BY time

按周统计

select 
CASE temp.time 
WHEN 1 THEN '星期一' 
WHEN 2 THEN '星期二' 
WHEN 3 THEN '星期三' 
WHEN 4 THEN '星期四' 
WHEN 5 THEN '星期五' 
WHEN 6 THEN '星期六' 
WHEN 7 THEN '星期天' 
end as time,
IFNULL(temp2.num,0) as num 
from(
select 1 as time UNION all
SELECT 2 UNION all
SELECT 3 UNION all
SELECT 4 UNION all
SELECT 5 UNION all
SELECT 6 UNION all
SELECT 7) as temp
LEFT JOIN 
(select ifNULL(count(id),0) as num, 
CASE WEEKDAY(dateff) 
WHEN 0 THEN '1' 
WHEN 1 THEN '2' 
WHEN 2 THEN '3' 
WHEN 3 THEN '4' 
WHEN 4 THEN '5' 
WHEN 5 THEN '6' 
WHEN 6 THEN '7' 
END as `time` 
from(
SELECT create_time as dateff,id as id FROM cz_taxi_orders UNION ALL
SELECT create_time as dateff,id as id FROM ctky_passenger_transport_order UNION ALL
SELECT pay_time as dateff,id as id FROM bus_order) as a 
GROUP BY `time`) as temp2
ON temp.time = temp2.time 
ORDER BY temp.time asc;

组装动态sql。

<select id="queryPassengerFlow" resultType="com.xxxCockpitPassengerFlowChartVo">
        <if test="type=='WEEK'">
            select
            CASE temp.time
            WHEN 1 THEN '星期一'
            WHEN 2 THEN '星期二'
            WHEN 3 THEN '星期三'
            WHEN 4 THEN '星期四'
            WHEN 5 THEN '星期五'
            WHEN 6 THEN '星期六'
            WHEN 7 THEN '星期天'
            end as time,IFNULL(temp2.num,0) as num
            from(select 1 as time UNION all select 2 UNION all
            select 3 UNION all select 4 UNION all
            select 5 UNION all select 6 UNION all
            select 7) as temp
            LEFT JOIN
            (select ifNULL(count(id),0) as num,
            CASE WEEKDAY(dateff)
            WHEN 0 THEN '1'
            WHEN 1 THEN '2'
            WHEN 2 THEN '3'
            WHEN 3 THEN '4'
            WHEN 4 THEN '5'
            WHEN 5 THEN '6'
            WHEN 6 THEN '7'
            END as `time`
        </if>
        <if test="type=='DAY'">
            select count(id) as num,
            DAY(dateff) as `time`
        </if>
        <if test="type=='YEAR'">
            select YEAR(dateff) as `time`,
            count(id) as num
        </if>
        from(select create_time as dateff,id as id from cz_taxi_orders UNION ALL
        select create_time as dateff,id as id from ctky_passenger_transport_order UNION ALL
        select pay_time as dateff,id as id from bus_order) as a
        <if test="type=='WEEK'">
            GROUP BY `time`) as temp2
            on temp.time = temp2.time
            ORDER BY temp.time asc
        </if>
        <if test="type=='DAY'">
            GROUP BY `time`
        </if>
        <if test="type=='YEAR'">
            WHERE (YEAR(dateff))>year(now())-10
            GROUP BY `time`
        </if>
    </select>

剩余的年,日,用java代码补充。

@Override
    public R queryPassengerFlow(String type) {
    //一开始加载,不输入默认按天
        if (type==null){
            type="DAY";
        }
        List<CockpitPassengerFlowChartVo> list = cockpitChartMapper.queryPassengerFlow(type);
        List<CockpitPassengerFlowChartVo> list1 = new ArrayList<>();
        //年查询递推十年
        List<CockpitPassengerFlowChartVo> list2 = new ArrayList<>();
        if ("YEAR".equals(type)){
            Calendar cal = Calendar.getInstance();
            int currentYear = cal.get(Calendar.YEAR);
            cal.set(Calendar.YEAR,currentYear-9);
            int toYear = cal.get(Calendar.YEAR);
            for (int i = currentYear; i >= toYear; i--) {
                CockpitPassengerFlowChartVo vos = new CockpitPassengerFlowChartVo();
                boolean flag = false;
                for (CockpitPassengerFlowChartVo vo : list) {
                    if (vo.getTime().equals(String.valueOf(i))) {
                        flag = true;
                        //月份
                        vos.setTime(vo.getTime());
                        //数量
                        vos.setNum(vo.getNum());
                    }
                }
                //月份不存在 赋值0
                if (!flag) {
                    vos.setTime(String.valueOf(i));
                    vos.setNum(0);
                }
                list1.add(vos);
            }
            return R.ok(list1);
        }
        if ("DAY".equals(type)){
            for (int i = 1; i <=30 ; i++) {
                CockpitPassengerFlowChartVo vos = new CockpitPassengerFlowChartVo();
                boolean flag = false;
                for (CockpitPassengerFlowChartVo vo : list) {
                    if (vo.getTime().equals(String.valueOf(i))) {
                        flag = true;
                        //月份
                        vos.setTime(vo.getTime());
                        //数量
                        vos.setNum(vo.getNum());
                    }
                }
                //月份不存在 赋值0
                if (!flag) {
                    vos.setTime(String.valueOf(i));
                    vos.setNum(0);
                }
                list2.add(vos);
            }
            return R.ok(list2);
        }
        return R.ok(list);
    }

成功,接下来就是和前端规定type按日传DAY,按周传WEEK,按年传YEAR。
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/743622.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

go语言开发 三种容器类型:数组、切片、map

目录 go语言中的三种容器类型&#xff1a;数组&#xff0c;切片&#xff0c;map go 与 python的容器类型对比 python go&#xff1a; 数组&#xff1a; 数组的定义&#xff1a;&#xff08;定义数组的格式、二维数组、初始化、任意长度初始化、取值&#xff09; 数组的…

swager异常_Failed to load API definition.

现象 Fetch error Internal Server Error /swagger/base/swagger.json 排查 按照以往的经历基本都是因为Conroller中的类中有非对外调用的函数&#xff0c;但是用了public修饰函数&#xff0c;会导致这个问题。这个问题通常通过查看git/svn中最近的提交记录&#xff0c;找出…

hadoop -Unable to start failover controller. Parent znode does not exist

Unable to start failover controller. Parent znode does not exist 问题描述 今天使用星环的TDH集群时&#xff0c;HDFS服务宕掉&#xff0c;在后台查看namenode 始终起不来 kubectl get pod -o wide | grep hdfs 如上图&#xff0c;k8s pod 起来又crash 掉&#xff0c;然后…

10 thymeleaf模版引擎使用

10.1 原理 首先&#xff0c;在idea搜索thymeleafProperties这个配置类。 通过源代码可以发现&#xff0c;使用的文件后缀是html&#xff0c;文件应该放在templates路径下&#xff1a; 10.1 依赖 直接加入启动器的thymeleaf依赖。 <dependency><groupId>org.sprin…

814. 复制数组

链接&#xff1a; 链接 题目&#xff1a; 给定两个数组 aa 和 bb 以及一个整数 sizesize&#xff0c;请你编写一个函数&#xff0c;void copy(int a[], int b[], int size)&#xff0c;将 aa 数组中的前 sizesize 个数字&#xff0c;复制到 bb 数组中。 复制完成后&#xff0c;…

AR 技术应用与管理:施工建造、机柜扫描、办公室导航

随着科技的不断革新和创新&#xff0c;越来越多的行业开始迎来数字化时代的变革。建筑行业作为人类历史上最重要的产业之一&#xff0c;在数字化转型方面同样也在不断推进。图扑软件结合 AR 技术的应用&#xff0c;为建筑行业带来了更加便捷高效的建筑施工过程管理。 传统的建…

娇滴滴的一朵花(Python实现)

目录 1 娇滴滴的她 2 Python代码实现 1 娇滴滴的她 娇滴滴。双眉敛破春山色。春山色。 为君含笑,为君愁蹙。多情别後无消息。 此时更有谁知得。谁知得。夜深无寐&#xff0c;度江横笛。 2 Python代码实现 import turtle from turtle import * turtle.title(春天送她一朵小花) #…

cf卡中毒了?别担心,这2种方法帮助你恢复数据

在现代数字化生活中&#xff0c;数据的重要性日益突出&#xff0c;而CF卡作为一种常见的存储设备&#xff0c;其安全性问题也备受关注。然而&#xff0c;由于各种原因&#xff0c;包括但不限于操作失误、设备故障和病毒攻击&#xff0c;我们可能会遇到数据丢失的情况。在这篇文…

OSS-Fuzz----OSS-Fuzz简介

【原文链接】OSS-Fuzz----OSS-Fuzz简介 一、OSS-Fuzz 简介 OSS-Fuzz是由Google开发和维护的一个开源项目&#xff0c;旨在帮助开发者改善软件的安全性和稳定性。它是一个自动化的模糊测试工具&#xff0c;可以在大规模的测试环境中发现软件中的漏洞和错误。 OSS-Fuzz使用模糊…

基于SpringBoot+vue的体质测试数据分析及可视化设计与实现

博主介绍&#xff1a; 大家好&#xff0c;我是一名在Java圈混迹十余年的程序员&#xff0c;精通Java编程语言&#xff0c;同时也熟练掌握微信小程序、Python和Android等技术&#xff0c;能够为大家提供全方位的技术支持和交流。 我擅长在JavaWeb、SSH、SSM、SpringBoot等框架…

go-zero系列:接入Prometheus

参考文档&#xff1a;https://zhuanlan.zhihu.com/p/463418864 1、下载Prometheus&#xff08;普罗米修斯&#xff09; https://prometheus.io/download/ 进入下载文件夹&#xff0c;比如prometheus-2.44.0.windows-amd64。 然后双击Prometheus.exe启动软件。 启动后&#xff0…

AIGC浪潮下,鹅厂新一代前端人的真实工作感受

&#x1f449;腾小云导读 AIGC 这一时代潮流已然不可阻挡&#xff0c;我们要做的不是慌乱&#xff0c;而是把握住这个时代的机会。本文就和大家一起来探索在 AIGC 下&#xff0c;前端工程师即将面临的挑战和机遇。聊聊从以前到现在&#xff0c;AIGC 给我们带来了怎么样的变化&a…

4.9 x64dbg 内存处理与差异对比

LyScript 插件中针对内存读写函数的封装功能并不多&#xff0c;只提供了最基本的内存读取和内存写入系列函数的封装&#xff0c;本章将继续对API接口进行封装&#xff0c;实现一些在软件逆向分析中非常实用的功能&#xff0c;例如ShellCode代码写出与置入&#xff0c;内存交换&…

2 Alice的果园

2 Alice的果园 作者: 赵晓鹏时间限制: 1S章节: 动态规划与贪心 输入说明 : 见题目描述。 输出说明 : 见题目描述。 输入范例 : 1 28 输出范例 : 28 Online Judge 1.0 #include<iostream> #include<vector> using namespace std; int rob(vector<int&…

数学建模学习之简单设备分配问题

简单的设备分配问题 某公司新购置了某种设备 6台&#xff0c;欲分配给下属的4 个企业&#xff0c;已知各企业获得这种设备后年创利润如表 1.1 所示&#xff0c;单位为千万元。问应如何分配这些设备能使年创总利润最大&#xff0c;最大利润是多少? 表1.1的数据为&#xff1a; 对…

Unreal 5 游戏框架

之前&#xff0c;只是简单的实现了一些特定的功能&#xff0c;这几天一直在学习官方的案例&#xff0c;学习了Lyra初学者项目和Action RPG的项目&#xff0c;也从中学习到了很多功能的使用&#xff0c;并对基础的架构设计有了初步的认识。 接下来&#xff0c;将对基础的一些设置…

elementui自定义loading图标

效果图如下&#xff1a; 一、在assets下新建一个mycss.css文件夹&#xff08;图片大小以及文字样式&#xff0c;可以根据自己的需求进行微调&#xff09; .el-loading-spinner {/*这个是自己想设置的 gif 加载动图*/background-image: url(../gif2.gif); background-repeat: n…

ASP.NET版本泄露【原理扫描】

如果想屏蔽 Server&#xff0c;X-AspNet-Version&#xff0c;X-AspNetMvc-Version 和 X-Powered-By&#xff0c;需要增加&#xff1a; <httpProtocol><customerHeaders><remove name"Server" /><remove name"X-AspNet-Version" />…

云计算之OpenStack核心

云计算之OpenStack核心 一、OpenStack架构1.1 OpenStack概念架构1.2 OpenStack逻辑架构1.3 拓扑部署1.4 使用OpenStack CLI1.4.1 OpenStack 服务都有自己的 CLI 二、OpenStack核心服务2.1 认证服务Keystone2.1.1 基本功能2.1.2 基本概念2.1.3 举例说明&#xff1a;admin用户查看…