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

时间:2019-04-26 18:20:27

标签: sql merge transactions duplicates

我为同一个人进行了多次交易,我想将它们合并并获取每个人的总支出,但是当我尝试了一些类似SELECT DISTINCT的代码时,我想将其所有人口统计学变量保存在同一张表中只会删除一些重复项而不是合并,我的目标是将客户分为低价值和高价值两类。 有人以950美元的价格购买了一件商品,或者购买了多件商品并花费了2500多美元。 (每笔交易只是一个项目,没有一个交易项目超过一个。

到目前为止,这是我的代码。 (我正在为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

这是我第一次使用这种类型的问题/答案网站,如果我做错了事,那么抱歉。

谢谢

1 个答案:

答案 0 :(得分:0)

您应该首先SUM BY金额,然后可以基于总计进行分类。内部查询将为您提供每个人的总和。外部查询将根据您的条件设置类别。

-编辑-

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