我想使用 sql select 陳述句將下表與下面給出的邏輯結合起來:
表:植物
| ID | 識別符號 | 代碼 |
|---|---|---|
| cbf2 | 5200 | 持續的 |
| b3aa | 5201 | 持續的 |
表:供應商
| ID | 識別符號 | 代碼 |
|---|---|---|
| 3527 | SD501 | 持續的 |
表:材料
| ID | 識別符號 | 代碼 | PC_ID | wxyz_代碼 |
|---|---|---|---|---|
| b4a1 | B50003 | 持續的 | 一個 | 1 |
| 97bb | B50004 | 持續的 | b | 1 |
| e189 | B50005 | 持續的 | C | 0 |
- 派生表的formattedID列是Plants、Material、Supplier表中存在的識別符號列的組合。
- 僅考慮材料表中wxyz_Code列= 1 的行。
- Material表中的 wxyz_Code 在DerivedTable表中重命名為abcd_Code。
- DerivedTable中的Material_ID、Plant_ID、Supplier_ID列分別從Material、Plant、Supplier的ID列填充。
- DerivedTable中的PC_ID由 Material 的PC_ID列填充
結果表:DerivedTable
| ID | 格式化ID | abcd_Code | Material_ID | 供應商_ID | Plant_ID | PC_ID |
|---|---|---|---|---|---|---|
| 自動生成 | B50003/5201 | 1 | b4a1 | '無效的' | cbf2 | 一個 |
| 自動生成 | B50003/5200 | 1 | b4a1 | '無效的' | b3aa | 一個 |
| 自動生成 | B50003/5201/SD501 | 1 | b4a1 | 3527 | cbf2 | 一個 |
| 自動生成 | B50003/5200/SD501 | 1 | b4a1 | 3527 | b3aa | 一個 |
| 自動生成 | B50004/5201 | 1 | 97bb | '無效的' | cbf2 | b |
| 自動生成 | B50004/5202 | 1 | 97bb | '無效的' | b3aa | b |
| 自動生成 | B50004/5201/SD501 | 1 | 97bb | 3527 | cbf2 | b |
| 自動生成 | B50004/5200/SD501 | 1 | 97bb | 3527 | b3aa | b |
uj5u.com熱心網友回復:
雖然此代碼適用于 postgreSQL,但將適用于許多稍作更改的 SQL 后端(即:|| 與 ):
select 'Automatically Generated' as Id,
m.identifier || '/' || p.identifier as formattedId,
m.wxyz_code as abcd_code,
m.id as Material_Id,
null as Supplier_Id,
p.Id as Plant_id,
m.pc_id
from Plant p,
Material m
where m.wxyz_code = 1
union all
select 'Automatically Generated' as Id,
m.identifier || '/' || p.identifier || '/' || s.Identifier as formattedId,
m.wxyz_code as abcd_code,
m.id as Material_Id,
s.id as Supplier_Id,
p.Id as Plant_id,
m.pc_id
from Plant p,
Supplier s,
Material m
where m.wxyz_code = 1
order by formattedId;
DBFiddle 演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/504053.html
上一篇:如何將3列合并為一列
