我想知道,如何在一行中記錄來自不同行的資料。
如果客戶一排有多個店鋪,那么我想調出其相應的關稅、名稱和 cug,例如 id = 1。
如果 shop1 = 577 固定在客戶身上,則其對應的值應寫入 CUG 欄位,例如 id = 2。
這里有張桌子:
create table transaction (id, shop1, sho2,shop3,shop_name,tariff,cug) as
select 1, 789, null, 987, bundle2, 5, 0 from dual union all
select 1, 789, null, null , bundle1, 4, 0 from dual union all
select 1, null,null,987, bundle2, 6, 0 from dual union all
select 2, null,null,987, bundle2, 6, 0 from dual union all
select 2, null, 635,null, bundle1, 8, 0 from dual union all
select 2, 577, null, null, 0, 3, cug2 from dual union all
select 2, null, 635, 987, bundle2, 5, 0 from dual union all
select 3, null, 852, null, bundle1, 6, 0 dual union all
select 4, 753, 357, null, bundle2, 1.5, 0 from dual union all
select 5, 369, 147, 422, bundle1, 4.5, 0 from dual ;
我的預期表:
| ID | 店鋪1 | 店鋪2 | 店鋪3 | 店鋪名稱 | 關稅 | 杯子 |
|---|---|---|---|---|---|---|
| 1 | 789 | 空值 | 987 | 捆綁包2 | 5 | 0 |
| 2 | 空值 | 635 | 987 | 捆綁包2 | 5 | cug2 |
| 3 | 空值 | 852 | 空值 | 捆綁包1 | 6 | 0 |
| 4 | 753 | 357 | 空值 | 捆綁包2 | 1.5 | 0 |
| 5 | 369 | 147 | 422 | 捆綁包1 | 4.5 | 0 |
uj5u.com熱心網友回復:
這是你想要的嗎?
你的描述很難理解。
create table transaction (id, shop1, shop2,shop3,shop_name,tariff,cug) as select 1, 789, null, 987, 'bundle2', 5, '0' from dual union all select 1, 789, null, null , 'bundle1', 4, '0' from dual union all select 1, null,null,987, 'bundle2', 6, '0' from dual union all select 2, null,null,987, 'bundle2', 6, '0' from dual union all select 2, null, 635,null, 'bundle1', 8, '0' from dual union all select 2, 577, null, null, '0', 3, 'cug2' from dual union all select 2, null, 635, 987, 'bundle2', 5, '0' from dual union all select 3, null, 852, null, 'bundle1', 6, '0' from dual union all select 4, 753, 357, null, 'bundle2', 1.5, '0' from dual union all select 5, 369, 147, 422, 'bundle1', 4.5, '0' from dual ;
select id, max(shop1) shop1, max(shop2) shop2, max(shop3) shop3, /*max(shop_name)*/ shop_name, max(tariff) tarif, max(cug) cug from transaction t join (select id midd, max(shop_name) msn from transaction group by id) m on id = midd and shop_name = msn group by id,shop_name;身份證 | 店鋪1 | 店鋪2 | 店鋪3 | 店鋪名稱 | 關稅 | CUG -: | ----: | ----: | ----: | :-------- | ----: | :-- 1 | 第789章 空| 987 | 捆綁2 | 6 | 0 2 | 空| 第635章 987 | 捆綁2 | 6 | 0 3 | 空| 第852章 空| 捆綁1 | 6 | 0 4 | 第753章 第357章 空| 捆綁2 | 1.5 | 0 5 | 369 | 147 | 422 | 捆綁1 | 4.5 | 0
db<>在這里擺弄
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/448728.html
上一篇:Apex上的圖表過濾器
下一篇:如何拆分具有逗號和冒號的字串
