我Id在 PostgreSQL 表中有一個列ABC。
Id是一個bigint長度為 10(minLength=maxLength=10)
我必須撰寫一個 SQL 查詢并分類Id為:
- 任何以 3 個連續奇數數字開頭并以至少 2 個連續偶數數字結尾的 ID 被歸類為“A 類”;
- 任何第二個數字大于6或第三個字符小于等于4的Id都歸類為“TYPE B”;
- 除非以 7 結尾,否則任何奇數編號的 Id 都被歸類為“TYPE C”;
- 任何偶數編號的 Id 都被歸類為“TYPE D”,除非它以 2 結尾;
- 所有其他 Id 被歸類為“TYPE X”;
uj5u.com熱心網友回復:
每種型別都可以通過正則運算式找到。
create table ABC ( Id decimal(10) primary key check(length(Id::text)=10) ); insert into ABC (Id) values (1350000042) , (1640000000) , (1090000503) , (1294567890) , (1090000907) , (1090000902)
/* Type A: Any Id that commences with exactly 3 consecutive odd digits and concludes with at least 2 consecutive even digits; Type B: Any Id where the second digit is greater than 6 or the third character is less than or equal to 4; Type C: Any odd-numbered Id unless it ends in 7; Type D: Any even-numbered Id unless it ends in 2; Type X: All other Id; */ select Id , case when Id::text ~ '^[13579]{3}[02468].*[02468]{2}$' then 'A' when Id::text ~ '^(.[7-9]|..[0-4])' then 'B' when Id::text ~ '[1359]$' then 'C' when Id::text ~ '[0468]$' then 'D' else 'X' end as Type from ABC order by Type編號 | 型別 ---------: | :--- 1350000042 | 一個 1760000000 | 乙 1640000000 | 乙 1090000503 | C 1294567890 | D 1090000907 | X 1090000902 | X
db<>在這里擺弄
uj5u.com熱心網友回復:
將 ID 轉換為 char,然后使用 CASE 陳述句實作所需的邏輯
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/406869.html
標籤:
