2017-07-21 82 views
1

我有兩個表。一個是Reference表,用於排序優先級,一個是Customer表。 Reference表用於優先考慮表Customer中的每個列,以便爲單個客戶的單個列給出不同的順序。在'PARTITION BY'上使用過濾條件

參考表

--------------------------------------- 
| Priority | Attribute | sourceID | 
--------------------------------------- 
| 1  |  EMAIL |  1  | 
| 2  |  EMAIL |  2  | 
| 3  |  EMAIL |  3  | 
| 2  |  NAME |  1  | 
| 1  |  NAME |  2  | 
| 3  |  NAME |  3  | 
--------------------------------------- 

客戶表

----------------------------------------------------------------------- 
| CustomerID | Name |  Email  | SourceID |  Date | 
----------------------------------------------------------------------- 
| 1  | John |  NULL   |  1  | 03/01/2017 | 
| 1  | NULL | [email protected] |  3  | 01/01/2017 | 
| 1  | J  | [email protected] |  2  | 02/01/2017 | 
----------------------------------------------------------------------- 

結果

--------------------------------------------- 
| CustomerID | Name |  Email  | 
--------------------------------------------- 
|  1  | John | [email protected] | 
--------------------------------------------- 

在t他目前我使用下面的查詢做到這一點:

SELECT DISTINCT 
     FIRST_VALUE(c.Name IGNORE NULLS) 
      OVER (PARTITION BY p.customerID 
       ORDER BY r.PRIORITY, c.DATE 
       ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS EMAIL, 
     FIRST_VALUE(c.Email IGNORE NULLS) 
      OVER (PARTITION BY c.customerID 
       ORDER BY r.PRIORITY, c.DATE 
       ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS EMAIL 
FROM Customer c 
    JOIN reference r ON c.sourceID = r.sourceID; 

然而,這確實需要爲每列不同的屬性納入考慮。我需要按部分爲每個分區添加某種過濾器。

任何人都可以協助我如何去做到這一點?

回答

2

一種方法是把屬性爲客戶在一列,然後重新組合它們:

SELECT DISTINCT customerId 
     first_value(CASE WHEN ca.attribute = 'NAME' THEN ca.val end) OVER 
      (PARTITION BY ca.customerId, attribute ORDER BY r.priority, ca.date) AS name, 
     first_value(CASE WHEN ca.attribute = 'EMAIL' THEN ca.val END) OVER 
      (PARTITION BY ca.customerId, attribute ORDER BY r.priority, ca.date) AS email 
FROM ((SELECT customerId, 'NAME' AS attribute, name AS val, sourceId, date 
     FROM customer c 
    ) UNION ALL 
     (SELECT customerId, 'EMAIL' AS attribute, email AS val, sourceId, date 
     FROM customer c 
    ) 
    ) ca JOIN 
    reference r 
    ON r.sourceId = ca.sourceId AND r.attribute = ca.attribute; 

注意,這裏使用SELECT DISTINCT而不是GROUP BY。我不認爲Netezza具有聚合函數first_value(),所以這個構造解決了這個問題。

相關問題