期望: 我想吸引所有自今年年初以来从特定类别(例如,无人机)购买商品的客户。
问题: 我得到重复;一世。 e。客户在其他时间再次购买(我不在乎)
表的关系:
SELECT
DISTINCT CD.SubscriberKey AS MCD_SubscriberKey
, MAX(OH.OrderDate) AS OH_OrderDate
, MAX(OA.OrderNo) AS OA_OrderNo
, OA.ArticleNo AS OA_ArticleNo
, OA.QuantityOrdered AS OA_QuantityOrdered
, AB.Category AS AB_Category
, CD.EmailAddress AS EmailAddress
, CD.EmailLanguage AS EmailLanguage
, CD.Gender AS Gender
, CD.FirstName AS FirstName
, CD.LastName AS LastName
FROM [Master_CustomerData] AS CD
INNER JOIN [Master_OrderHeader] AS OH
ON CD.SubscriberKey = OH.SubscriberKey
INNER JOIN [Master_OrderedArticles] AS OA
ON OH.OrderNo = OA.OrderNo
INNER JOIN [Master_ArticleBase] AS AB
ON OA.ArticleNo = AB.ArticleNo
WHERE
/* Category group */
AB.Category IN (811000)
AND OA.QuantityCancelled = 0
AND OH.OrderDate > '2018-01-01'
GROUP BY
CD.SubscriberKey
, CD.EmailAddress
, OA.ArticleNo
, OA.QuantityOrdered
, AB.Category
, CD.EmailLanguage
, CD.Gender
, CD.FirstName
, CD.LastName
答案 0 :(得分:1)
对于日期,您具有最大日期,因此不应选择重复的值
如果您使用分组方式,则不需要DISTINCT
您是否也可能需要为quantityOrdered求和(如果某人两次订购同一产品,您就有重复的行)
SELECT CD.SubscriberKey AS MCD_SubscriberKey
, MAX(OH.OrderDate) AS OH_OrderDate
, MAX(OA.OrderNo) AS OA_OrderNo
, OA.ArticleNo AS OA_ArticleNo
, SUM(OA.QuantityOrdered) AS OA_QuantityOrdered
, AB.Category AS AB_Category
, CD.EmailAddress AS EmailAddress
, CD.EmailLanguage AS EmailLanguage
, CD.Gender AS Gender
, CD.FirstName AS FirstName
, CD.LastName AS LastName
FROM [Master_CustomerData] AS CD
INNER JOIN [Master_OrderHeader] AS OH ON CD.SubscriberKey = OH.SubscriberKey
INNER JOIN [Master_OrderedArticles] AS OA ON OH.OrderNo = OA.OrderNo
INNER JOIN [Master_ArticleBase] AS AB ON OA.ArticleNo = AB.ArticleNo
WHERE AB.Category = 811000
AND OA.QuantityCancelled = 0
AND OH.OrderDate > '2018-01-01'
GROUP BY
CD.SubscriberKey
, CD.EmailAddress
, OA.ArticleNo
, AB.Category
, CD.EmailLanguage
, CD.Gender
, CD.FirstName
, CD.LastName
答案 1 :(得分:0)
这是我的工作解决方案。我删除了“与众不同”,最大化了“ AB.Category”,并根据必须区分的内容进行分组。
谢谢,所有答案...
SELECT
CD.SubscriberKey AS MCD_SubscriberKey
, MAX(OH.OrderDate) AS OH_OrderDate
, MAX(OA.OrderNo) AS OA_OrderNo
, MAX(AB.Category) AS AB_Category
, CD.EmailAddress AS EmailAddress
, CD.EmailLanguage AS EmailLanguage
, CD.Gender AS Gender
, CD.FirstName AS FirstName
, CD.LastName AS LastName
FROM [Master_CustomerData] AS CD
INNER JOIN [Master_OrderHeader] AS OH
ON CD.SubscriberKey = OH.SubscriberKey
INNER JOIN [Master_OrderedArticles] AS OA
ON OH.OrderNo = OA.OrderNo
INNER JOIN [Master_ArticleBase] AS AB
ON OA.ArticleNo = AB.ArticleNo
WHERE
/* Category defined: Dornes, cameras, outdoor, consoles */
AB.Category IN (
211000,
212000,
...
791700
)
/* Article not Cancelled */
AND OA.QuantityCancelled = 0
/* OrderDate */
AND OH.OrderDate > '2017-08-01'
GROUP BY
CD.SubscriberKey
, CD.EmailAddress
, CD.EmailLanguage
, CD.Gender
, CD.FirstName
, CD.LastName