SQL如何在同乙個字段不同型別進行條件查詢統計總數

2022-02-17 11:07:11 字數 827 閱讀 1437

如 a 字段裡面包含  1 2 3三種型別的資料,但是三種型別的資料可能是不同的人錄入的,這時候要根據不同的人進行這三種型別進行數量統計

如下:

以下則是sql實現

select f_olp_man as '巡檢員' ,f_plan_count as '巡檢週期', count(*) as '巡檢數量',(select count(f_env_state)

from optic_linepat_rec where f_env_state='隱患' and f_olp_man=b.f_olp_man and f_plan_count=b.f_plan_count )as '隱患',

(select count(f_env_state)

from optic_linepat_rec where f_env_state='正常' and f_olp_man=b.f_olp_man and f_plan_count=b.f_plan_count )as '正常',

(select count(f_env_state)

from optic_linepat_rec where f_env_state='正常' and f_olp_man=b.f_olp_man and f_plan_count=b.f_plan_count )as '缺陷'

from optic_linepat_rec b where f_well_id is not null

group by f_olp_man,f_plan_count

order by f_olp_man

sql 同乙個字段按照不同條件更新字段值

專案中用到sql更新乙個欄位的值,同乙個字段根據不同條件來更新不同的值,記錄一下,也希望可以幫到其他人。公式 update 表名 set 字段 case when 條件1 then 值1 when 條件2 then 值2 else 預設值3 end例子 update sys anchor set i...

mybatis同乙個字段,多次模糊查詢

問題描述 在乙個欄位上執行多關鍵字模糊查詢,拼接多個 like 實現方式 and條件 方式一 直接使用like語句拼接。select from table where field like and field like 按照上述語句寫的sql的like 拼接的數量是固定的。方式二 mybatis f...

SQL 同乙個字段多個值,實現動態行轉列

用動態sql實現行轉列。因用到了row number,只適用於sql server 2005及以上版本 測試資料 with user id,name,roleid as select 1,bobo r1 union all select 2,coco r1 union all select 3,do...