-1

期望:我想得到所有从年初以来从特定类别(即无人机)购买文章的客户。

问题:我得到重复;即客户在不同时间再次购买(我不在乎)

表的关系

  • [Master_CustomerData] has_many
    • [Master_OrderHeader] has_many
      • [Master_OrderedArticles]属于_to
        • [Master_ArticleBase]

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 
4

2 回答 2

1


对于日期,您有最大日期,因此如果您使用 group by 您不需要 DISTINCT,则不应选择重复值

可能你也需要数量的总和(如果有人订购了两次相同的产品,你有重复的行)

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 
于 2018-08-24T18:45:52.807 回答
0

这是我的工作解决方案。我删除了“distinct”,将“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 
于 2018-08-27T16:53:19.193 回答