postgreSql分組統(tǒng)計(jì)數(shù)據(jù)的實(shí)現(xiàn)代碼
1. 背景
比如氣象臺(tái)的氣溫監(jiān)控,每半小時(shí)上報(bào)一條數(shù)據(jù),有很多個(gè)地方的氣溫監(jiān)控,這樣數(shù)據(jù)表里就會(huì)有很多地方的不同時(shí)間的氣溫?cái)?shù)據(jù)
2. 需求:
每次查詢只查最新的氣溫?cái)?shù)據(jù)按照不同的溫度區(qū)間來分組查出,比如:高溫有多少地方,正常有多少地方,低溫有多少地方
3. 構(gòu)建數(shù)據(jù)
3.1 創(chuàng)建表結(jié)構(gòu):
-- DROP TABLE public.t_temperature CREATE TABLE public.t_temperature ( id int4 NOT NULL GENERATED ALWAYS AS IDENTITY, place_name varchar NOT NULL, value float8 NOT NULL, up_time timestamp NOT NULL, CONSTRAINT t_temperature_pk PRIMARY KEY (id) ); -- Permissions ALTER TABLE public.t_temperature OWNER TO postgres; GRANT ALL ON TABLE public.t_temperature TO postgres;
3.2 造數(shù)據(jù)
INSERT INTO public.t_temperature (place_name,value,up_time) VALUES
('廣州',35,'2020-07-12 15:00:00.000')
,('廣州',35.9,'2020-07-12 15:30:00.000')
,('深圳',30,'2020-07-12 15:30:00.000')
,('深圳',31,'2020-07-12 16:30:00.000')
,('三亞',23,'2020-07-12 16:30:00.000')
,('三亞',21,'2020-07-12 17:30:00.000')
,('北極',-1,'2020-07-12 17:30:00.000')
,('北極',-10,'2020-07-12 19:30:00.000')
;
4. 需求實(shí)現(xiàn)
4.1 需求1的SQL語(yǔ)句
利用了postgreSql的一個(gè)函數(shù):ROW_NUMBER() OVER( [ PRITITION BY col1] ORDER BY col2[ DESC ] )
select * from ( select tt.place_name, tt.value, tt.up_time, row_number() over ( partition by tt.place_name order by tt.up_time desc) as row_num from t_temperature tt) aaa where aaa.row_num = 1
效果如下,查出的都是最新的數(shù)據(jù):

4.2 需求2的SQL語(yǔ)句
利用了一個(gè)case when then else end 用法來統(tǒng)計(jì)數(shù)量
select dd.place_name, sum(case when dd.value <= 0 then 1 else 0 end) as 低溫天氣, sum(case when dd.value > 0 and dd.value < 25 then 1 else 0 end) as 正常天氣, sum(case when dd.value >= 25 then 1 else 0 end) as 高溫天氣 from t_temperature dd group by dd.place_name
效果如下,因?yàn)闆]有過濾每個(gè)地方的最新數(shù)據(jù),查出的是所有數(shù)據(jù):

用需求1的結(jié)果來查詢統(tǒng)計(jì):
select dd.place_name, sum(case when dd.value <= 0 then 1 else 0 end) as 低溫天氣, sum(case when dd.value > 0 and dd.value < 25 then 1 else 0 end) as 正常天氣, sum(case when dd.value >= 25 then 1 else 0 end) as 高溫天氣 from ( select * from ( select tt.place_name, tt.value, tt.up_time, row_number() over ( partition by tt.place_name order by tt.up_time desc) as row_num from t_temperature tt) aaa where aaa.row_num = 1) dd group by dd.place_name
效果如下:

假如再嵌套一個(gè)sum統(tǒng)計(jì),就能查出低溫天氣,正常天氣,高溫天氣分別合計(jì)數(shù)量是多少了。
over,enjoy!
到此這篇關(guān)于postgreSql分組統(tǒng)計(jì)數(shù)據(jù)的文章就介紹到這了,更多相關(guān)postgreSql分組數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Postgresql根據(jù)響應(yīng)數(shù)據(jù)反向?qū)崿F(xiàn)建表語(yǔ)句與insert語(yǔ)句的過程
根據(jù)已有數(shù)據(jù),可構(gòu)建名為products的表,包含id(自增主鍵)、title(非空字符串)、progress(非空整數(shù))三個(gè)字段,建表后,可通過insert語(yǔ)句插入數(shù)據(jù),這種反向操作有助于從現(xiàn)有數(shù)據(jù)結(jié)構(gòu)出發(fā),快速構(gòu)建數(shù)據(jù)庫(kù)表,并進(jìn)行數(shù)據(jù)填充,感興趣的朋友跟隨小編一起看看吧2022-02-02
Postgresql數(shù)據(jù)庫(kù)character?varying和character的區(qū)別說明
這篇文章主要介紹了Postgresql數(shù)據(jù)庫(kù)character?varying和character的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Docker環(huán)境下升級(jí)PostgreSQL的步驟方法詳解
這篇文章主要介紹了Docker環(huán)境下升級(jí)PostgreSQL的步驟方法詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
解決postgresql表中的字段名稱包含特殊符號(hào)的問題
這篇文章主要介紹了解決postgresql表中的字段名稱包含特殊符號(hào)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
PostgreSQL有效地處理數(shù)據(jù)序列化和反序列化的方法
在 PostgreSQL 中,處理數(shù)據(jù)的序列化和反序列化是確保數(shù)據(jù)在存儲(chǔ)、傳輸和處理過程中的一致性和可用性的重要任務(wù),這涉及到選擇合適的數(shù)據(jù)類型、轉(zhuǎn)換函數(shù)以及在應(yīng)用程序與數(shù)據(jù)庫(kù)之間進(jìn)行數(shù)據(jù)交互的策略,需要的朋友可以參考下2024-07-07

