您好,登錄后才能下訂單哦!
這篇“PGSQL怎么查詢最近N天的數(shù)據(jù)及SQL語(yǔ)句實(shí)現(xiàn)替換字段內(nèi)容的方法”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“PGSQL怎么查詢最近N天的數(shù)據(jù)及SQL語(yǔ)句實(shí)現(xiàn)替換字段內(nèi)容的方法”文章吧。
思路:先獲取當(dāng)前的時(shí)間,然后更具當(dāng)前時(shí)間去減去對(duì)應(yīng)的天數(shù),就可以得到需要的天數(shù)了。然后用這些天數(shù)作為條件,然后即可查詢出需要多少天的數(shù)據(jù)了。
-- 獲取當(dāng)前時(shí)間 SELECT CURRENT_TIMESTAMP; -- 或 SELECT now(); select CURRENT_TIMESTAMP :: DATE AS "當(dāng)前日期"; SELECT CURRENT_DATE; select CURRENT_TIMESTAMP :: TIME AS "當(dāng)前時(shí)間"; 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 ;
當(dāng)然6 day、1 day這些也是可修改的;
--統(tǒng)計(jì)最近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;
實(shí)例如下:查詢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;
思路:使用替換函數(shù)替換即可
-- 替換語(yǔ)句 update 表名 set 替換字段名 = replace(替換字段名,'被替換內(nèi)容','替換內(nèi)容');
實(shí)例如下:將 my_test 表的 test_content 字段內(nèi)容進(jìn)行替換
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';
--查詢表數(shù)據(jù),給test_content字段并賦值 select test_id,test_code,'測(cè)試數(shù)據(jù)' test_content from my_test; --查詢表數(shù)據(jù)給自定義 ABCD字段 并賦值 select test_id,test_code,'測(cè)試數(shù)據(jù)' ABCD from my_test;
select test_id,test_code,COALESCE(test_content,'測(cè)試數(shù)據(jù)') ABCD from my_test; -- || select test_id,test_code, CASE WHEN test_content IS NOT NULL THEN test_content ELSE '測(cè)試數(shù)據(jù)' END ABCD from my_test;
以上就是關(guān)于“PGSQL怎么查詢最近N天的數(shù)據(jù)及SQL語(yǔ)句實(shí)現(xiàn)替換字段內(nèi)容的方法”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。