我有3个数据扩展(数据表)
Subscription
CustomerInfo
Survey
所有三个表都将CustomerId作为字段。
以下是我的标准:
- 检查Bencriptids of Subscripton中的B13或B26
- EmailInptin在CustomerInfo中为True
- 订阅中的CreateDt应该超过30天
- 他们在调查表中,具有以下条件:
醇>QuestionId ='purchaseIntentQuestion'和AnswerId =' 3' OR
QuestionId =' goldenQuestion' AND(AnswerId =' 1' OR AnswerId =' 2')
确保您没有Customerids的重复记录
我写了下面的查询,但它没有按预期返回结果。请指教
SELECT distinct sub.CustomerId, Cus.EmailOptin, srv.QuestionID,srv.AnswerId
FROM [Subscription] sub
JOIN [Survey Questions] srv ON sub.CustomerId = srv.CustomerId
JOIN [CustomerInfo] cus ON sub.CustomerId = cus.CustomerId
WHERE 1 = 1
AND CAST(sub.CreateDt AS DATE) < CAST(GETDATE() -30 AS DATE)
AND Cus.EmailOptin = 'True'
AND
(
sub.BenefitIds LIKE '%B13%'
OR
sub.BenefitIds LIKE '%B26%'
)
AND
(
(srv.QuestionId = 'purchaseIntentQuestion' AND srv.AnswerId = '3')
OR
(srv.QuestionId = 'goldenQuestion' AND (srv.AnswerId = '1' OR srv.AnswerId = '2' ))
)
对于上述查询,即使EmailOptin也不是真的。 请指教。
答案 0 :(得分:1)
你说&#34;它没有按预期返回结果&#34;所以我真的不知道需要修复什么。我觉得CAST(GETDATE() -30 AS DATE)
是个问题。为了便于阅读,我还将OR
替换为IN()
。试试这个:
SELECT distinct sub.CustomerId,
Cus.EmailOptin,
srv.QuestionID,
srv.AnswerId
FROM [ Subscription ] sub
JOIN [ Survey Questions ] srv
ON sub.CustomerId = srv.CustomerId
JOIN [ CustomerInfo ] cus
ON sub.CustomerId = cus.CustomerId
WHERE 1 = 1
AND CAST(sub.CreateDt AS DATE) < DATE_ADD(NOW(), INTERVAL 30 DAY)
AND Cus.EmailOptin = 'True'
AND (sub.BenefitIds = '%B13%' OR sub.BenefitIds LIKE '%B26%')
AND ((srv.QuestionId = 'purchaseIntentQuestion' AND srv.AnswerId = '3') OR
(srv.QuestionId = 'goldenQuestion' AND
srv.AnswerId IN('1','2')))