gaussdb查询今日以及之前的数据
如果表中数据是实时更新的,其实前后端联调的时候只需要传当前时间点就行,但是好多时候数据库的数据都是需求人员维护的演示数据,那会如果在不改动前后端功能的时候,就可以直接修改mapper文件的sql语句。前言
我们这边的需求是前端展示曲线,今日与自定义日期进行比较,那时候后端直接让前端传个日期,后端把全表的数据查询出来,但是演示的时候用的演示数据,导致页面显示了一整天的数据,所以我就只修改了一下sql。
一、参考示例
sql示例:
现有表结构如下:
date_data 日期
date_time 时间
value 相关值
point_type 相关类型
parent_type 父类型
device_name 具体名称
date_id id
select data_time,value,point_type,parent_type,device_name,date_id from (
select concat(date_data,' ',date_time) as data_time,date_data,date_time,value,point_type,parent_type,device_name,date_id from test
where
#不管是今日还是自定义日期都是从00:00:00开始
cast(date_data||' '|| date_time as timestamp) > cast( '2021-11-25' ||' '|| '00:00:00' as timestamp)
and
#判断传入的值是本日还是自定义日期,如果是本日就获取当前时间与表中数据进行对比
case when to_date(to_char(now(),'yyyy-mm-dd'),'yyyy-mm-dd') = to_date('2021-11-25','yyyy-mm-dd') then
cast('2021-11-25' ||' ' || to_char(now(),'HH24:MI:SS') as timestamp) >= cast(date_data||' '||date_time as timestamp)
else
#如果是自定义日期就取一天时间23:59:59进行比较
cast('2021-11-25' ||' ' || '23:59:59' as timestamp) >= cast(date_data||' '||date_time as timestamp)
end
and device_name = '具体名称'
and point_type = '具体类型'
group by date_data,date_time ,value,point_type,parent_type,device_name,date_id
) t
order by data_time asc