使用group by子句时如何从连接中返回列?

时间:2018-12-14 21:01:31

标签: sql sql-server join group-by ssms

我正在尝试返回t1.firstname,t1.lastname,t1.city,t1.stateprovince,并且我想返回t2.amount,t2.restaurantname,t2.restaurantnum,t2.datekey和t2.amount也是

这是我的代码:

SELECT DISTINCT 
    t1.[FirstName] AS FirstName,
    t1.[LastName] AS LastName,
    t1.[City] AS City,
    t1.[StateProvince] AS StateProvince
FROM 
    Database.dbo.CustomerData t1
JOIN 
    (SELECT DISTINCT
         MAX(t2.[Amount]) AS Amount,
         MAX(t2.[Account]) AS Account,
         MAX(t2.[CardNumber]) AS CardNumber,
         MAX(t2.[RestaurantNum]) AS RestaurantNum,
         MAX(t2.[RestaurantName]) AS RestaurantName,
         MAX(t2.[DateKey]) AS DateKey,
         SUM([t2].[Amount]) AS Total_Spend
     FROM 
         gift.TransactionItems t2
     GROUP BY 
         t2.[Amount],
         t2.[RestaurantNum],
         t2.[RestaurantName],
         t2.[DateKey],
         t2.[AccountID],
         t2.[TransactionID]) t2 ON t1.[CardNumber] = t2.[CardNumber]
WHERE
    [FirstName] IS NOT NULL
    AND [LastName] IS NOT NULL
    AND [EmailFailed] IS NULL       
    AND [IsRegistered] = 'Yes'
    AND [DateKey] BETWEEN 20180601 AND 20181213
    AND t2.[Account] = 'Dollars_Spent_Accrued'

这可能吗?当前仅返回t1.firstname,t1.lastname,t1.city和t1.stateprovince。

2 个答案:

答案 0 :(得分:0)

如果在添加列时查询不起作用,那么我将把t2查询放入临时表中,然后进行联接,看看效果是否更好。经过一定程度的复杂性后,我通常会选择临时表并将其分解:

drop table if exists #t2
SELECT DISTINCT
         MAX(t2.[Amount]) AS Amount,
         MAX(t2.[Account]) AS Account,
         MAX(t2.[CardNumber]) AS CardNumber,
         MAX(t2.[RestaurantNum]) AS RestaurantNum,
         MAX(t2.[RestaurantName]) AS RestaurantName,
         MAX(t2.[DateKey]) AS DateKey,
         SUM([t2].[Amount]) AS Total_Spend
     INTO #t2 FROM 
         gift.TransactionItems t2
     GROUP BY 
         t2.[Amount],
         t2.[RestaurantNum],
         t2.[RestaurantName],
         t2.[DateKey],
         t2.[AccountID],
         t2.[TransactionID]

答案 1 :(得分:0)

查询的第一部分列出了将返回的值。尽管t1的列包含在查询的FROM段中,但您仅列出了t2的值。您需要将它们显式添加到查询的SELECT段中。让我知道这是否适合您。

SELECT DISTINCT 
    t1.[FirstName] AS FirstName,
    t1.[LastName] AS LastName,
    t1.[City] AS City,
    t1.[StateProvince] AS StateProvince
    t2.[amount], 
    t2.[restaurantname], 
    t2.[restaurantnum], 
    t2.[datekey]
FROM 
    Database.dbo.CustomerData t1
JOIN 
    (SELECT DISTINCT
         MAX(t2.[Amount]) AS Amount,
         MAX(t2.[Account]) AS Account,
         MAX(t2.[CardNumber]) AS CardNumber,
         MAX(t2.[RestaurantNum]) AS RestaurantNum,
         MAX(t2.[RestaurantName]) AS RestaurantName,
         MAX(t2.[DateKey]) AS DateKey,
         SUM([t2].[Amount]) AS Total_Spend
     FROM 
         gift.TransactionItems
     GROUP BY 
         t2.[Amount],
         t2.[RestaurantNum],
         t2.[RestaurantName],
         t2.[DateKey],
         t2.[AccountID],
         t2.[TransactionID]) t2 ON t1.[CardNumber] = t2.[CardNumber]
WHERE
    [FirstName] IS NOT NULL
    AND [LastName] IS NOT NULL
    AND [EmailFailed] IS NULL       
    AND [IsRegistered] = 'Yes'
    AND [DateKey] BETWEEN 20180601 AND 20181213
    AND t2.[Account] = 'Dollars_Spent_Accrued'