SQL 查詢表中各類點數量以及各類點滿足某條件數量

2021-10-12 09:53:38 字數 1200 閱讀 6184

select

totalcount.

type

, totalcount.count as total,

coalesce

( highlevel.count,0)

as highlevel

from

(select

type

,count

(type

)from geohazard_all_english group

bytype

) totalcount

left

join

(select

type

,count

(type

)from geohazard_all_english where risk_level like

'%高%'

select type, count ( type ) from geohazard_all_english where risk_level like'%高%' group by type

根據常理可以推斷,某一類災害點不一定存在高風險的點,所以可以使用join將兩個查詢結果進行拼接

將查詢一的結果作為a表,查詢表二的結果作為b表,a left join b,則結果為a表的所有資料都會出來,b表只有在滿足條件的時候才會出來,而條件就是a、b表災害點型別名相同。

SQL臨時表查詢所有子節點資料

方法一 create table 臨時表名 欄位1 約束條件,欄位2 約束條件,方法二 select into 臨時表名 from 你的表 方法三 利用with語句 sql server 2005 版本 with test id,name as select bucode,bunamech from...

sql中單錶查詢

use test 查詢大氣質量表中的全部內容。select from all 查詢北京的大氣質量資料。select from all where 城市名 北京 查詢不同月份pm2.5的最大值。select 月份 max pm2.5 from all group by 月份 降序查詢不同城市pm10的...

sql 中查詢表的外來鍵

一 查詢出所有表的鍵 select from information schema.key column usage where table name you tablename 二 查詢出所有的外來鍵 select from select from information schema.key c...