PGSQL查询最近N天的数据及SQL语句实现替换字段内容(sql查询连续三天活跃用户)这都可以

随心笔谈2年前发布 编辑
173 0
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买



目录1、用SQL查询最近N天的数据2、用SQL语句实现替换字段里的内容3、SQL将查询结果赋值4、SQL查询字段内容为 NULL值时候赋值总结

思路:先获取当前的时间,然后更具当前时间去减去对应的天数,就可以得到需要的天数了。然后用这些天数作为条件,然后即可查询出需要多少天的数据了。

— 获取当前时间
SELECT CURRENT_TIMESTAMP;
— 或
SELECT now();
select CURRENT_TIMESTAMP :: DATE AS “当前日期”;
SELECT CURRENT_DATE;
select CURRENT_TIMESTAMP :: TIME AS “当前时间”;
SELECT CURRENT_TIME;

最近N天

— 最近7天
SELECT DATE( T ) AS DAY
FROM
generate_series ( ( CURRENT_TIMESTAMP – INTERVAL ‘6 day’ ) :: DATE,CURRENT_TIMESTAMP :: DATE, ‘1 days’ ) AS T ;

当然6 day、1 day这些也是可修改的;

–统计最近30、7、昨天、今天的数据
SELECT
count(*) AS AllData,
count(*) filter ( WHERE create_time BETWEEN ( SELECT CURRENT_DATE – INTERVAL ’30 day’ ) AND CURRENT_DATE ) AS Last30DayData,
count(*) filter ( WHERE create_time BETWEEN ( SELECT CURRENT_DATE – INTERVAL ‘7 day’ ) AND CURRENT_DATE ) AS Last7DayData,
count(*) filter ( WHERE create_time BETWEEN ( SELECT CURRENT_DATE – INTERVAL ‘1 day’ ) AND CURRENT_DATE ) AS YesterdayData,
count(*) filter ( WHERE create_time >=CURRENT_DATE ) AS todayAddData
FROM
table_name;

实例如下:查询lic_plate字段不为空和为空的最近7天的数据

— 查询lic_plate字段不为空的最近7天的数据
select count(tn.lic_plate) AS number,tn.create_time AS percentage
from table_name tn
where tn.create_time in (SELECT DATE(T) AS DAY
FROM generate_series((CURRENT_TIMESTAMP – INTERVAL ‘6 day’) :: DATE, CURRENT_TIMESTAMP :: DATE,’1 days’) AS T) AND lic_plate is not null
group by tn.create_time order by tn.create_time;

— 查询lic_plate字段为空的最近7天的数据
select count(id) AS number,CSQV.create_time AS percentage
from (select * from table_name tn where lic_plate is null) AS CSQV where CSQV.create_time in (SELECT DATE(T) AS DAY
FROM generate_series((CURRENT_TIMESTAMP – INTERVAL ‘6 day’) :: DATE, CURRENT_TIMESTAMP :: DATE,’1 days’) AS T)
group by CSQV.create_time;

思路:使用替换函数替换即可

— 替换语句
update 表名 set 替换字段名=replace(替换字段名,’被替换内容’,’替换内容’);

实例如下:将 my_test 表的 test_content 字段内容进行替换

update my_test set test_content=replace(test_content,’1′,’18’);

替换函数:如果没有可以创建,创建函数如下:

— replace all occurrences in string of old_substr with new_substr
— auto-generated definition
create function replace(text, text, text) returns text
immutable
strict
cost 1
language internal
as
$$begin
— missing source code
end;$$;

comment on function replace(text, text, text) is ‘replace all occurrences in string of old_substr with new_substr’;

–查询表数据,给test_content字段并赋值
select test_id,test_code,’测试数据’ test_content from my_test;
–查询表数据给自定义 ABCD字段 并赋值
select test_id,test_code,’测试数据’ ABCD from my_test;
select test_id,test_code,COALESCE(test_content,’测试数据’) ABCD from my_test;
— ||
select test_id,test_code,
CASE
WHEN test_content IS NOT NULL THEN test_content
ELSE ‘测试数据’
END ABCD from my_test;

到此这篇关于PGSQL查询最近N天的数据及SQL语句实现替换字段内容的文章就介绍到这了,更多相关PGSQL查询最近N天数据内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:PGSQL 实现查询今天,昨天的数据,一个月之内的数据postgresql 实现查询出的数据为空,则设为0的操作postgresql数据库根据年月查询出本月的所有数据操作PostgreSQL数据库的基本查询操作

© 版权声明

相关文章