期望:我想得到所有从年初以来从特定类别(即无人机)购买文章的客户。
问题:我得到重复;即客户在不同时间再次购买(我不在乎)
表的关系:
- [Master_CustomerData] has_many
- [Master_OrderHeader] has_many
- [Master_OrderedArticles]属于_to
- [Master_ArticleBase]
- [Master_OrderedArticles]属于_to
- [Master_OrderHeader] has_many
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