尝试更新单行中的多个字段,但创建重复的行

时间:2019-02-06 21:52:48

标签: sql sql-server join group-by case

编辑: Salesforce Marketing Cloud uses a set of SQL Server

我在Salesforce Marketing Cloud中使用SQL查询,这意味着我仅限于执行“选择”语句作为对表[Distribution]的覆盖。我正在尝试执行以下操作:

select * from [Subscribers] A
inner join [Master Table] B on A.MemberID = B.MemberID

这部分工作正常。

接下来,我有两个字段Reward1和Reward2,每个字段对应[Offers]中的特定RewardCode。因此,我想做的(我认为)应该是在RewardCode = 111111的MemberID上与[Subscribers]和[Offers]进行左联接,并在RewardCode = 222222的情况下与[Subscribers]和[Offers]进行左联接。这似乎是我创建重复记录的地方。

模式示例如下:

主成员表

memberID | email | fname | lname
1111 | 1111@email.com | One | Name
2222 | 2222@email.com | Two | Name
3333 | 3333@email.com | Three | Name
4444 | 4444@email.com | Four | Name

订户

memberID | status | subscriberkey
1111 | active | 1111@email.com
2222 | active | 2222@email.com
3333 | active | 3333@email.com
4444 | active | 4444@email.com

主奖励表

memberID | rewardcode
1111 | 111111
1111 | 111111
1111 | 111111
2222 | 111111
2222 | 222222
2222 | 222222
3333 | 222222
3333 | 111111
4444 | 222222
4444 | 333333
4444 | 222222

理想情况下,结果表将类似于此:

[distribution]
subscriberkey | memberId | status| reward1 | reward2
1111@email.com| 1111 | active | TRUE | FALSE
2222@email.com| 2222 | active TRUE | TRUE
3333@email.com| 3333 | active TRUE | TRUE
4444@email.com| 4444 | active FALSE | TRUE

完整查询:

SELECT A.SubscriberKey AS SubscriberKey, A.EmailAddress AS EmailAddress,
A.MemberID AS MemberId, A.Status AS Status,
A.Decile AS Decile, A.Segment AS Segment, M.Firstname AS FirstName,
M.LastName AS LastName, M.Points AS PointsAvailable,
M.Experience AS Experience,
    CASE
        WHEN M.[Preferred Delivery Method for Offers] = 2 THEN 'SMS'
        ELSE 'Email'
    END AS PreferredDeliveryOffers,
    CASE
        WHEN M.[Preferred Delivery Method for Non-Offers] = 2 THEN 'SMS'
        ELSE 'Email'
    END AS PreferredDeliveryNonOffers,
    CASE
        WHEN R.MemberRewardBarcode IS NOT NULL AND
        R.[Member ID] = M.MemberId AND
        R.MemberRewardBarcode = 1111111 THEN 'Reward15'
        ELSE ''
    END AS Reward15,
    CASE
        WHEN B.MemberRewardBarcode IS NOT NULL AND
        B.[Member ID] = M.MemberId AND
        B.MemberRewardBarcode = 2222222 THEN 'RewardBDay'
        ELSE ''
    END AS RewardBDay
FROM Subscribers A
INNER JOIN [Master Member Table] M
    ON A.MemberID = M.MemberId
LEFT JOIN (
    SELECT DISTINCT MemberRewardBarcode, [Member ID]
    FROM [master rewards table] R
    WHERE R.MemberRewardBarcode = 1111111 AND
    R.CancelTimestamp IS NULL AND
    R.ExpireTimestamp IS NULL AND
    R.RedeemTimestamp IS NULL
  ) R ON A.MemberID = R.[Member ID]
LEFT JOIN (
    SELECT DISTINCT MemberRewardBarcode, [Member ID]
    FROM [master rewards table] R
    WHERE R.MemberRewardBarcode = 2222222 AND
    R.CancelTimestamp IS NULL AND
    R.ExpireTimestamp IS NULL AND
    R.RedeemTimestamp IS NULL
    GROUP BY MemberRewardBarcode, [Member ID]
  ) B ON A.MemberID = R.[Member ID]

1 个答案:

答案 0 :(得分:1)

您应该可以使用条件聚合来解决此问题:

SELECT 
    s.subscriberkey,
    s.memberID,
    s.status,
    MAX(CASE WHEN r.rewardcode = 111111 THEN 1 END) reward1,
    MAX(CASE WHEN r.rewardcode = 222222 THEN 1 END) reward2
FROM 
    subscribers s
    LEFT JOIN reward r ON r.memberID = s.memberID
GROUP BY 
    s.subscriberkey,
    s.memberID,
    s.status

此查询使用非常常见的SQL语法(聚合和CASE),据我在您提供的文档中可以看到,Salesforce Marketing Cloud应该支持该语法。

SQLServer 2017 demo 返回:

 subscriberkey  | memberID | status | reward1 | reward2
 :------------- | -------: | :----- | ------: | ------:
 1111@email.com |     1111 | active |       1 |    null
 2222@email.com |     2222 | active |       1 |       1
 3333@email.com |     3333 | active |       1 |       1
 4444@email.com |     4444 | active |    null |    null