2015-03-02 62 views
1

我做的已經建立業務的一些重映射和需要做下面的SQL中爲Oracle 11需要幫助識別匹配記錄在SQL

這是一個「一次性活動」(而不是交付給我客戶端),所以我打算不使用編程語言來實現它。 我有下表。下面是DDL和DML的設置。

subsriber_cd | prod_id | rate plan | charge_ID | QoS_1 | QoS_2 | QoS_3 | RatePerUnit | 
-------------+---------+-----------+-----------+--------+--------+--------+-------------| 
pete   | 1  | RP1  | 100  | Credit | Analog |  | 0.1   | 
pete   | 1  | RP1  | 100  | Credit | GSM |  | 0.2   | 
pete   | 1  | RP1  | 100  | Credit | LTE |  | 0.3   | 
pete   | 1  | RP1  | 100  | Debit | Analog |  | 1.1   | 
pete   | 1  | RP1  | 100  | Debit | GSM |  | 1.2   | 
pete   | 1  | RP1  | 100  | Debit | LTE |  | 1.3   | 
pete   | 1  | RP1  | 101  | Credit | Analog |  | 2.1   | 
pete   | 1  | RP1  | 101  | Credit | GSM |  | 2.2   | 
pete   | 1  | RP1  | 101  | Credit | LTE |  | 2.3   | 
pete   | 1  | RP1  | 101  | Debit | Analog |  | 3.1   | 
pete   | 1  | RP1  | 101  | Debit | GSM |  | 3.2   | 
pete   | 1  | RP1  | 101  | Debit | LTE |  | 3.3   | 

chris  | 2  | RP2  | 100  | Credit | Analog |  | 5.1   | 
chris  | 2  | RP2  | 100  | Credit | GSM |  | 5.2   | 
chris  | 2  | RP2  | 100  | Credit | LTE |  | 5.3   | 
chris  | 2  | RP2  | 100  | Debit | Analog |  | 6.1   | 
chris  | 2  | RP2  | 100  | Debit | GSM |  | 6.2   | 
chris  | 2  | RP2  | 100  | Debit | LTE |  | 6.3   | 
chris  | 2  | RP2  | 101  | Credit | Analog |  | 7.1   | 
chris  | 2  | RP2  | 101  | Credit | GSM |  | 7.2   | 
chris  | 2  | RP2  | 101  | Credit | LTE |  | 7.3   | 
chris  | 2  | RP2  | 101  | Debit | Analog |  | 8.1   | 
chris  | 2  | RP2  | 101  | Debit | GSM |  | 8.2   | 
chris  | 2  | RP2  | 101  | Debit | LTE |  | 8.3   | 

jack   | 3  | RP3  | 100  | Credit | Analog |  | 0.1   | 
jack   | 3  | RP3  | 100  | Credit | GSM |  | 0.2   | 
jack   | 3  | RP3  | 100  | Credit | LTE |  | 0.3   | 
jack   | 3  | RP3  | 100  | Debit | Analog |  | 1.1   | 
jack   | 3  | RP3  | 100  | Debit | GSM |  | 1.2   | 
jack   | 3  | RP3  | 100  | Debit | LTE |  | 1.3   | 
jack   | 3  | RP3  | 101  | Credit | Analog |  | 2.1   | 
jack   | 3  | RP3  | 101  | Credit | GSM |  | 2.2   | 
jack   | 3  | RP3  | 101  | Credit | LTE |  | 2.3   | 
jack   | 3  | RP3  | 101  | Debit | Analog |  | 3.1   | 
jack   | 3  | RP3  | 101  | Debit | GSM |  | 3.2   | 
jack   | 3  | RP3  | 101  | Debit | LTE |  | 3.3   | 

...                      

dave   | 4  | RP4  | 100  | Credit | Analog |  | 10.1  | 
dave   | 4  | RP4  | 100  | Credit | GSM |  | 10.2  | 
dave   | 4  | RP4  | 100  | Credit | LTE |  | 10.3  | 
dave   | 4  | RP4  | 100  | Debit | Analog |  | 10.1  | 
dave   | 4  | RP4  | 100  | Debit | GSM |  | 10.2  | 
dave   | 4  | RP4  | 100  | Debit | LTE |  | 10.3  | 
dave   | 4  | RP4  | 101  | Credit | Analog |  | 10.1  | 
dave   | 4  | RP4  | 101  | Credit | GSM |  | 10.2  | 
dave   | 4  | RP4  | 101  | Credit | LTE |  | 10.3  | 
dave   | 4  | RP4  | 101  | Debit | Analog |  | 10.1  | 
dave   | 4  | RP4  | 101  | Debit | GSM |  | 10.2  | 
dave   | 4  | RP4  | 101  | Debit | LTE |  | 10.3  | 

設計中有一個錯誤,即每個訂戶都被分配了一個獨特的產品。 現在,這導致了數千的產品在我的系統。

由於重新設計的,我不打算創建每個用戶單獨的產品,而是創造一個產品和費率計劃,而是添加一個新的QoS_3,這將有助於我給出不同的RatePerUnits。

由於皮特和插孔具有相同的RatePerUnit,我正打算存儲相同QoS_3對他們倆的。 所以,我需要找到匹配的RatePerUnit設置,這樣我可以將它們合併爲一個QoS_3記錄。

在上表中我可以合併皮特的和傑克的QoS_3,因爲他們有其他領域的所有組合同樣RatePerUnit,使我的表。

subsriber_cd | prod_id | rate plan | charge_ID | QoS_1 | QoS_2 | QoS_3 | RatePerUnit | 
-------------+---------+-----------+-----------+--------+--------+--------+-------------| 
pete   | 5  | RP5  | 100  | Credit | Analog | Q_1 | 0.1   | 
pete   | 5  | RP5  | 100  | Credit | GSM | Q_1 | 0.2   | 
pete   | 5  | RP5  | 100  | Credit | LTE | Q_1 | 0.3   | 
pete   | 5  | RP5  | 100  | Debit | Analog | Q_1 | 1.1   | 
pete   | 5  | RP5  | 100  | Debit | GSM | Q_1 | 1.2   | 
pete   | 5  | RP5  | 100  | Debit | LTE | Q_1 | 1.3   | 
pete   | 5  | RP5  | 101  | Credit | Analog | Q_1 | 2.1   | 
pete   | 5  | RP5  | 101  | Credit | GSM | Q_1 | 2.2   | 
pete   | 5  | RP5  | 101  | Credit | LTE | Q_1 | 2.3   | 
pete   | 5  | RP5  | 101  | Debit | Analog | Q_1 | 3.1   | 
pete   | 5  | RP5  | 101  | Debit | GSM | Q_1 | 3.2   | 
pete   | 5  | RP5  | 101  | Debit | LTE | Q_1 | 3.3   | 

chris  | 5  | RP5  | 100  | Credit | Analog | Q_2 | 5.1   | 
chris  | 5  | RP5  | 100  | Credit | GSM | Q_2 | 5.2   | 
chris  | 5  | RP5  | 100  | Credit | LTE | Q_2 | 5.3   | 
chris  | 5  | RP5  | 100  | Debit | Analog | Q_2 | 6.1   | 
chris  | 5  | RP5  | 100  | Debit | GSM | Q_2 | 6.2   | 
chris  | 5  | RP5  | 100  | Debit | LTE | Q_2 | 6.3   | 
chris  | 5  | RP5  | 101  | Credit | Analog | Q_2 | 7.1   | 
chris  | 5  | RP5  | 101  | Credit | GSM | Q_2 | 7.2   | 
chris  | 5  | RP5  | 101  | Credit | LTE | Q_2 | 7.3   | 
chris  | 5  | RP5  | 101  | Debit | Analog | Q_2 | 8.1   | 
chris  | 5  | RP5  | 101  | Debit | GSM | Q_2 | 8.2   | 
chris  | 5  | RP5  | 101  | Debit | LTE | Q_2 | 8.3   | 

jack   | 5  | RP5  | 100  | Credit | Analog | Q_1 | 0.1   | 
jack   | 5  | RP5  | 100  | Credit | GSM | Q_1 | 0.2   | 
jack   | 5  | RP5  | 100  | Credit | LTE | Q_1 | 0.3   | 
jack   | 5  | RP5  | 100  | Debit | Analog | Q_1 | 1.1   | 
jack   | 5  | RP5  | 100  | Debit | GSM | Q_1 | 1.2   | 
jack   | 5  | RP5  | 100  | Debit | LTE | Q_1 | 1.3   | 
jack   | 5  | RP5  | 101  | Credit | Analog | Q_1 | 2.1   | 
jack   | 5  | RP5  | 101  | Credit | GSM | Q_1 | 2.2   | 
jack   | 5  | RP5  | 101  | Credit | LTE | Q_1 | 2.3   | 
jack   | 5  | RP5  | 101  | Debit | Analog | Q_1 | 3.1   | 
jack   | 5  | RP5  | 101  | Debit | GSM | Q_1 | 3.2   | 
jack   | 5  | RP5  | 101  | Debit | LTE | Q_1 | 3.3   | 

...                     

dave   | 5  | RP5  | 100  | Credit | Analog | Q_4 | 10.1  | 
dave   | 5  | RP5  | 100  | Credit | GSM | Q_4 | 10.2  | 
dave   | 5  | RP5  | 100  | Credit | LTE | Q_4 | 10.3  | 
dave   | 5  | RP5  | 100  | Debit | Analog | Q_4 | 10.1  | 
dave   | 5  | RP5  | 100  | Debit | GSM | Q_4 | 10.2  | 
dave   | 5  | RP5  | 100  | Debit | LTE | Q_4 | 10.3  | 
dave   | 5  | RP5  | 101  | Credit | Analog | Q_4 | 10.1  | 
dave   | 5  | RP5  | 101  | Credit | GSM | Q_4 | 10.2  | 
dave   | 5  | RP5  | 101  | Credit | LTE | Q_4 | 10.3  | 
dave   | 5  | RP5  | 101  | Debit | Analog | Q_4 | 10.1  | 
dave   | 5  | RP5  | 101  | Debit | GSM | Q_4 | 10.2  | 
dave   | 5  | RP5  | 101  | Debit | LTE | Q_4 | 10.3  | 

你可以看到好處,當用戶將現在不必輸入12 * 4 RatePerUnit皮特,克里斯,傑克和戴維,而是輸入12 * 3 RatePerUnit。

這會讓我重新設計的巨大差異,因爲有〜40K記錄我的所有訂戶。

DDL和DML的

CREATE TABLE combination (
    subsriber_cd VARCHAR2(20), 
    prod_id NUMBER(5), 
    rate_plan VARCHAR2(20), 
    charge_id NUMBER(5), 
    qos_1 VARCHAR2(20), 
    qos_2 VARCHAR2(20), 
    qos_3 VARCHAR2(20), 
    rateperunit NUMBER(10,2) 
); 

INSERT INTO combination 
    VALUES ('pete', 1, 'RP1', 100, 'Credit', 'Analog', '', 0.1); 
INSERT INTO combination 
    VALUES ('pete', 1, 'RP1', 100, 'Credit', 'GSM', '', 0.2); 
INSERT INTO combination 
    VALUES ('pete', 1, 'RP1', 100, 'Credit', 'LTE', '', 0.3); 
INSERT INTO combination 
    VALUES ('pete', 1, 'RP1', 100, 'Debit', 'Analog', '', 1.1); 
INSERT INTO combination 
    VALUES ('pete', 1, 'RP1', 100, 'Debit', 'GSM', '', 1.2); 
INSERT INTO combination 
    VALUES ('pete', 1, 'RP1', 100, 'Debit', 'LTE', '', 1.3); 
INSERT INTO combination 
    VALUES ('pete', 1, 'RP1', 101, 'Credit', 'Analog', '', 2.1); 
INSERT INTO combination 
    VALUES ('pete', 1, 'RP1', 101, 'Credit', 'GSM', '', 2.2); 
INSERT INTO combination 
    VALUES ('pete', 1, 'RP1', 101, 'Credit', 'LTE', '', 2.3); 
INSERT INTO combination 
    VALUES ('pete', 1, 'RP1', 101, 'Debit', 'Analog', '', 3.1); 
INSERT INTO combination 
    VALUES ('pete', 1, 'RP1', 101, 'Debit', 'GSM', '', 3.2); 
INSERT INTO combination 
    VALUES ('pete', 1, 'RP1', 101, 'Debit', 'LTE', '', 3.3); 
INSERT INTO combination 
    VALUES ('chris', 2, 'RP2', 100, 'Credit', 'Analog', '', 5.1); 
INSERT INTO combination 
    VALUES ('chris', 2, 'RP2', 100, 'Credit', 'GSM', '', 5.2); 
INSERT INTO combination 
    VALUES ('chris', 2, 'RP2', 100, 'Credit', 'LTE', '', 5.3); 
INSERT INTO combination 
    VALUES ('chris', 2, 'RP2', 100, 'Debit', 'Analog', '', 6.1); 
INSERT INTO combination 
    VALUES ('chris', 2, 'RP2', 100, 'Debit', 'GSM', '', 6.2); 
INSERT INTO combination 
    VALUES ('chris', 2, 'RP2', 100, 'Debit', 'LTE', '', 6.3); 
INSERT INTO combination 
    VALUES ('chris', 2, 'RP2', 101, 'Credit', 'Analog', '', 7.1); 
INSERT INTO combination 
    VALUES ('chris', 2, 'RP2', 101, 'Credit', 'GSM', '', 7.2); 
INSERT INTO combination 
    VALUES ('chris', 2, 'RP2', 101, 'Credit', 'LTE', '', 7.3); 
INSERT INTO combination 
    VALUES ('chris', 2, 'RP2', 101, 'Debit', 'Analog', '', 8.1); 
INSERT INTO combination 
    VALUES ('chris', 2, 'RP2', 101, 'Debit', 'GSM', '', 8.2); 
INSERT INTO combination 
    VALUES ('chris', 2, 'RP2', 101, 'Debit', 'LTE', '', 8.3); 
INSERT INTO combination 
    VALUES ('jack', 3, 'RP3', 100, 'Credit', 'Analog', '', 0.1); 
INSERT INTO combination 
    VALUES ('jack', 3, 'RP3', 100, 'Credit', 'GSM', '', 0.2); 
INSERT INTO combination 
    VALUES ('jack', 3, 'RP3', 100, 'Credit', 'LTE', '', 0.3); 
INSERT INTO combination 
    VALUES ('jack', 3, 'RP3', 100, 'Debit', 'Analog', '', 1.1); 
INSERT INTO combination 
    VALUES ('jack', 3, 'RP3', 100, 'Debit', 'GSM', '', 1.2); 
INSERT INTO combination 
    VALUES ('jack', 3, 'RP3', 100, 'Debit', 'LTE', '', 1.3); 
INSERT INTO combination 
    VALUES ('jack', 3, 'RP3', 101, 'Credit', 'Analog', '', 2.1); 
INSERT INTO combination 
    VALUES ('jack', 3, 'RP3', 101, 'Credit', 'GSM', '', 2.2); 
INSERT INTO combination 
    VALUES ('jack', 3, 'RP3', 101, 'Credit', 'LTE', '', 2.3); 
INSERT INTO combination 
    VALUES ('jack', 3, 'RP3', 101, 'Debit', 'Analog', '', 3.1); 
INSERT INTO combination 
    VALUES ('jack', 3, 'RP3', 101, 'Debit', 'GSM', '', 3.2); 
INSERT INTO combination 
    VALUES ('jack', 3, 'RP3', 101, 'Debit', 'LTE', '', 3.3); 
INSERT INTO combination 
    VALUES ('dave', 4, 'RP4', 100, 'Credit', 'Analog', '', 10.1); 
INSERT INTO combination 
    VALUES ('dave', 4, 'RP4', 100, 'Credit', 'GSM', '', 10.2); 
INSERT INTO combination 
    VALUES ('dave', 4, 'RP4', 100, 'Credit', 'LTE', '', 10.3); 
INSERT INTO combination 
    VALUES ('dave', 4, 'RP4', 100, 'Debit', 'Analog', '', 10.1); 
INSERT INTO combination 
    VALUES ('dave', 4, 'RP4', 100, 'Debit', 'GSM', '', 10.2); 
INSERT INTO combination 
    VALUES ('dave', 4, 'RP4', 100, 'Debit', 'LTE', '', 10.3); 
INSERT INTO combination 
    VALUES ('dave', 4, 'RP4', 101, 'Credit', 'Analog', '', 10.1); 
INSERT INTO combination 
    VALUES ('dave', 4, 'RP4', 101, 'Credit', 'GSM', '', 10.2); 
INSERT INTO combination 
    VALUES ('dave', 4, 'RP4', 101, 'Credit', 'LTE', '', 10.3); 
INSERT INTO combination 
    VALUES ('dave', 4, 'RP4', 101, 'Debit', 'Analog', '', 10.1); 
INSERT INTO combination 
    VALUES ('dave', 4, 'RP4', 101, 'Debit', 'GSM', '', 10.2); 
INSERT INTO combination 

回答

0

如果我理解正確你想要基於同rateperunit更新QOS_3。此合併時它:

merge into combination c1 
using (
    select row_number() over (order by rpu) rn, rpu 
    from (
     select rateperunit rpu 
     from combination 
     group by rateperunit) 
) c2 
on (c1.rateperunit = c2.rpu) 
when matched then update set c1.qos_3 = 'Q_'||lpad(c2.rn, 4, '0') 

結果:

select * from combination order by qos_3 

pete 1 RP1 100  Credit Analog Q_0001 0.1 
jack 3 RP3 100  Credit Analog Q_0001 0.1 
pete 1 RP1 100  Credit GSM  Q_0002 0.2 
jack 3 RP3 100  Credit GSM  Q_0002 0.2 
pete 1 RP1 100  Credit LTE  Q_0003 0.3 
jack 3 RP3 100  Credit LTE  Q_0003 0.3 
... 
dave 4 RP4 101 Credit  GSM  Q_0026 10.2 
dave 4 RP4 101 Credit  LTE  Q_0027 10.3 
dave 4 RP4 100 Credit  LTE  Q_0027 10.3 
dave 4 RP4 100 Debit  LTE  Q_0027 10.3