简体   繁体   English

合并交易但保留所有人口统计列

[英]merging transactions but keeping all demography columns

I have multiple transactions for the same people and i want to merge them all and get the total spent for each but i want to keep all of their demography variables in the same table, when i have tried some codes like SELECT DISTINCT it will just delete some duplicates instead of merging, my target is to put the customers into 2 groups Low and High value. 我有同一个人的多个交易,我想将它们全部合并,并获得每个的总花费,但我想将所有人口统计变量保存在同一个表中,当我尝试了一些代码,如SELECT DISTINCT,它只会删除一些重复而不是合并,我的目标是将客户分为两组低价和高价值。 Either someone who bought 1 item for $950 or who bought multiple items and spent more than $2500. 无论是以950美元购买1件商品还是购买多件商品并花费超过2500美元的人。 (each transaction is just a single item no transaction has more than 1 item. (每笔交易只是一个单项,没有交易超过1项。

Here is my code so far. 到目前为止,这是我的代码。 (i am preparing it for SAS) (我正准备为SAS)

Select        
CUS.FirstName        
,CUS.LastName        
,CUS.NumberChildrenAtHome        
,CUS.CommuteDistance        
,CUS.CustomerKey        
,FIS.SalesAmount        
,CUS.Gender        
,CUS.MaritalStatus        
,CUS.HouseOwnerFlag        
,CUS.NumberCarsOwned        
,CUS.YearlyIncome        
,CUS.TotalChildren        
,CUS.EnglishEducation AS Education        
,floor(DATEDIFF(DAY,BirthDate,getdate()))/365.25 AS AGE
,CASE        
WHEN FIS.UnitPrice >=950 OR FIS.SalesAmount >=2500 THEN 'High Value'        
ELSE 'Low Value'        
END AS 'Customer Value'        
From dbo.FactInternetSales AS FIS        
LEFT JOIN DBO.DimCustomer AS CUS        
ON FIS.CustomerKey = CUS.CustomerKey        
LEFT JOIN dbo.DimSalesTerritory AS DST        
ON FIS.SalesTerritoryKey = DST.SalesTerritoryKey

This is my first time using this type of question/answer site so sorry if i did something wrong. 这是我第一次使用这种类型的问答网站,对不起,如果我做错了。

Thank you 谢谢

You should first SUM BY the amounts, then you can categorise based on the totals. 你应该先SUM BY的数额,那么你可以根据分类的总数。 The inner query will give you the sum for each person. 内部查询将为您提供每个人的总和。 The outer query will set the category based on your condition. 外部查询将根据您的条件设置类别。

-- EDITED -- - 已编辑 -

SELECT
    innerQuery.*,
    CASE WHEN Total_Unit_Price >= 950
    OR Total_Sales_Amount >= 2500 THEN 'High Value' ELSE 'Low Value' END AS 'Customer Value'
FROM
    (
        Select
            CUS.FirstName,
            CUS.LastName,
            CUS.NumberChildrenAtHome,
            CUS.CommuteDistance,
            CUS.CustomerKey,
            FIS.SalesAmount,
            CUS.Gender,
            CUS.MaritalStatus,
            CUS.HouseOwnerFlag,
            CUS.NumberCarsOwned,
            CUS.YearlyIncome,
            CUS.TotalChildren,
            CUS.EnglishEducation AS Education,
            floor(DATEDIFF(DAY, BirthDate, getdate())) / 365.25 AS AGE,
            SUM(FIS.UnitPrice) AS Total_Unit_Price,
            SUM(FIS.SalesAmount) AS Total_Sales_Amount
        From
            dbo.FactInternetSales AS FIS
            LEFT JOIN DBO.DimCustomer AS CUS ON FIS.CustomerKey = CUS.CustomerKey
            LEFT JOIN dbo.DimSalesTerritory AS DST ON FIS.SalesTerritoryKey = DST.SalesTerritoryKey
        GROUP BY
            CUS.FirstName,
            CUS.LastName,
            CUS.NumberChildrenAtHome,
            CUS.CommuteDistance,
            CUS.CustomerKey,
            FIS.SalesAmount,
            CUS.Gender,
            CUS.MaritalStatus,
            CUS.HouseOwnerFlag,
            CUS.NumberCarsOwned,
            CUS.YearlyIncome,
            CUS.TotalChildren,
            CUS.EnglishEducation,
            floor(DATEDIFF(DAY, BirthDate, getdate())) / 365.25
    ) innerQuery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM