需要帮助识别sql中的匹配记录集

时间:2015-03-02 08:36:25

标签: sql oracle matching recordset

我正在重新映射已安装的业务,需要在SQL for Oracle 11中执行以下操作。

这是一次"一次性活动" (不是我的客户的可交付成果)因此我打算不使用编程语言来实现它。 我有下表。用于设置的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。

由于pete和jack具有相同的RatePerUnit,我计划为它们存储相同的QoS_3。 所以我需要找到匹配RatePerUnit的记录集,以便我可以将它们合并为一个QoS_3。

在上表中,我可以合并pete&和jack的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        |

当用户现在不必为pete,chris,jack和dave输入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

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您希望根据相同的QOS_3更新rateperunit。这个合并是这样的:

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