Как внутреннее объединить подмножество поля, назначить значения для двух случаев и сбросить остальные - PullRequest
0 голосов
/ 20 сентября 2019

Попытка выяснить, как это сделать, не объединяя три отдельных запроса.В исходном наборе данных есть в основном 4 подмножества продуктов,

  1. , когда я хочу установить поле "Продукт" как "CPE",
  2. , когда яхочу установить в поле «Продукт» значение «Обслуживание CPE»,
  3. Те, у которых есть совпадения в другой таблице «Dom_product_buckets», я хочу получить совпадения из этой таблицы как поле «Продукт»,
  4. Остатки записей, которые не вписываются в случаи 1,2,3, которые я не хочу.
SELECT customer, 'CPE Maintenance' as Product, sum(amt_eop) as revenue 
   FROM table1
   where pr2 in ('Core CPE' , 'Strategic CPE' ) and pr6 like '%Maintenance%'
   group by product, customer
  UNION
SELECT customer, 'CPE' as Product, sum(amt_eop) as revenue
    FROM table1 where pr2 in ('Core CPE' , 'Strategic CPE' ) and pr6 not like '%Maintenance%'
    group by seg_lvl5_2, product, customer
  UNION
SELECT a.customer, b.product_group as product, sum(amt_eop) as revenue
    FROM table1 A
    inner join dom_product_buckets B
    on A.pr2=b.l2_prod_desc and A.pr3=b.l3_prod_desc
    group by b.product_group,a.customer

Есть NULLS (продукты, которые не являются CPE или CPE Maintenance).и у меня нет совпадений в dom_product_buckets), которые меня не волнуют и которые не нужны в окончательном наборе результатов.

Таблица 1:

+------------+-----------------------+------------------------------+-------------------------------------------+-----------+
|  customer  |          pr2          |             pr3              |                    pr6                    |  amt_eop  |
+------------+-----------------------+------------------------------+-------------------------------------------+-----------+
| Customer a | Strategic Networking  | Internet                     | Internet Dedicated Ethernet (IDE)         | 810010.48 |
| customer b | Strategic Networking  | Internet                     | Internet Ethernet                         | 17.7399   |
| Customer c | Strategic Networking  | Internet                     | Internet Dedicated Access                 | 0         |
| customer d | Strategic Networking  | Ethernet Services            | EVPL - National CPA US                    | 11.99     |
| Customer a | Strategic Networking  | Internet                     | Internet Dedicated Access                 | 500       |
| customer b | Strategic Networking  | Private IP (MPLS)            | Private IP Ethernet - US                  | 0         |
| Customer c | Core Voice Services   | Switched Access              | SWA Flat - Interstate CCL                 | 0         |
| customer d | Other & Miscellaneous | Other Core Svcs              | PICC                                      | 250       |
| Customer a | Strategic Networking  | Private IP (MPLS)            | Private IP Port Ethernet                  | 3000      |
| customer b | Core Voice Services   | Local Voice                  | EUCL                                      | 85        |
| customer d | Core Voice Services   | Domestic LD                  | LD - Monthly Minimum Charge / Spend Level | 0         |
| customer b | Core Voice Services   | Local Voice                  | DTL Basic                                 | 11.99     |
| customer x | Other & Miscellaneous | Other Core Svcs              | Carrier Cost Recovery Charge              | -1189.07  |
| Customer a | Core CPE              | CPE Core Sales               | CPE - Core Sales & Installations          | 164.48    |
| customer b | Core CPE              | CPE Core Sales               | CPE - Core Sales & Installations          | 2.76      |
| Customer a | Core CPE              | CPE Core Maintenance         | CPE - Core Maintenance                    | 0         |
| customer z | Core CPE              | CPE Core Sales               | CPE - Core Sales & Installations          | 2.76      |
| customer b | Strategic CPE         | CPE Strategic Networking     | CPE - Private IP  Equipment Rental        | 0         |
| Customer c | Strategic CPE         | CPE Strategic Networking     | MRP - CPE Branded Maintenance             | 0         |
| customer d | Strategic CPE         | CPE Strategic Communications | CPE - VCE Rental                          | 151.94    |
+------------+-----------------------+------------------------------+-------------------------------------------+-----------+

dom_product_buckets:

+--------------------------+------------------------------+-------------------+
|       l2_prod_desc       |         l3_prod_desc         |   Product_Group   |
+--------------------------+------------------------------+-------------------+
| Professional Services    | Advanced Communications      | Advanced Comms PS |
| Professional Services    | Unified communications       | Advanced Comms PS |
| Professional Services    | Advanced Comm Pro Serv       | Advanced Comms PS |
| Strategic Communications | Contact Center Services - IP | CCS IP            |
| Strategic Communications | IP Contact Center            | CCS IP            |
| Strategic Communications | Contact Centers Western      | CCS IP            |
| Strategic Networking     | Internet                     | Internet          |
| Strategic Networking     | Video services               | Internet          |
| Strategic Networking     | Digital Voice                | Internet          |
| Strategic Networking     | Ethernet Services            | Ethernet          |
| Strategic Communications | IP Communications            | IP Comms          |
| Managed Network          | Managed Network Services     | MNS_VNS           |
| Managed Network          | Virtual Network Services     | MNS_VNS           |
+--------------------------+------------------------------+-------------------+

1 Ответ

1 голос
/ 21 сентября 2019

Вы можете исключить значения NULL, выполнив исключающее соединение.Поэтому вместо использования inner join в третьем запросе попробуйте использовать left join, например, так:

SELECT a.customer, b.product_group as product, sum(amt_eop) as revenue
    FROM table1 A
    left join dom_product_buckets B
    on A.pr2=b.l2_prod_desc and A.pr3=b.l3_prod_desc
    where b.product_group is not null
    group by b.product_group,a.customer

Два других оператора должны подойти, поскольку они указывают, что вы хотите CPE или * 1008.*.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...