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;
然而,這確實需要爲每列不同的屬性納入考慮。我需要按部分爲每個分區添加某種過濾器。
任何人都可以協助我如何去做到這一點?