PGSQL查詢最近N天的數(shù)據(jù)及SQL語句實現(xiàn)替換字段內(nèi)容
1、用SQL查詢最近N天的數(shù)據(jù)
思路:先獲取當前的時間,然后更具當前時間去減去對應的天數(shù),就可以得到需要的天數(shù)了。然后用這些天數(shù)作為條件,然后即可查詢出需要多少天的數(shù)據(jù)了。
-- 獲取當前時間 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這些也是可修改的;
--統(tǒng)計最近30、7、昨天、今天的數(shù)據(jù) 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天的數(shù)據(jù)
-- 查詢lic_plate字段不為空的最近7天的數(shù)據(jù)
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天的數(shù)據(jù)
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;2、用SQL語句實現(xiàn)替換字段里的內(nèi)容
思路:使用替換函數(shù)替換即可
-- 替換語句 update 表名 set 替換字段名 = replace(替換字段名,'被替換內(nèi)容','替換內(nèi)容');
實例如下:將 my_test 表的 test_content 字段內(nèi)容進行替換

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

替換函數(shù):如果沒有可以創(chuàng)建,創(chuàng)建函數(shù)如下:
-- 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';3、SQL將查詢結果賦值
--查詢表數(shù)據(jù),給test_content字段并賦值 select test_id,test_code,'測試數(shù)據(jù)' test_content from my_test; --查詢表數(shù)據(jù)給自定義 ABCD字段 并賦值 select test_id,test_code,'測試數(shù)據(jù)' ABCD from my_test;
4、SQL查詢字段內(nèi)容為 NULL值時候賦值
select test_id,test_code,COALESCE(test_content,'測試數(shù)據(jù)') ABCD from my_test;
-- ||
select test_id,test_code,
CASE
WHEN test_content IS NOT NULL THEN test_content
ELSE '測試數(shù)據(jù)'
END ABCD from my_test;總結
到此這篇關于PGSQL查詢最近N天的數(shù)據(jù)及SQL語句實現(xiàn)替換字段內(nèi)容的文章就介紹到這了,更多相關PGSQL查詢最近N天數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
PostgreSQL數(shù)據(jù)庫事務插入刪除及更新操作示例
這篇文章主要為大家介紹了PostgreSQL事務的插入刪除及更新操作示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2022-04-04
PostgreSQL數(shù)據(jù)庫中修改表字段的常用命令小結
這篇文章主要給大家介紹了PostgreSQL數(shù)據(jù)庫中修改表字段的常用命令操作,文中有詳細的代碼示例供大家參考,具有一定的參考價值,需要的朋友可以參考下2023-12-12
PostgreSQL教程(六):函數(shù)和操作符詳解(2)
這篇文章主要介紹了PostgreSQL教程(六):函數(shù)和操作符詳解(2),本文講解了模式匹配、數(shù)據(jù)類型格式化函數(shù)、時間/日期函數(shù)和操作符等內(nèi)容,需要的朋友可以參考下2015-05-05
Postgresql JSON對象和數(shù)組查詢功能實現(xiàn)
這篇文章主要介紹了Postgresql JSON對象和數(shù)組查詢功能實現(xiàn),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11

