Сначала вы должны 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