对一列求和并对结果执行更多计算?

时间:2019-05-06 19:37:22

标签: sql sql-server

在下面的查询中,我正在基于Status列的表格中统计出现的次数。我也想根据我要返回的计数执行计算。例如,假设我想将100添加到Snoozed值...我该怎么做?以下是我认为可以做到的:

SELECT 
    pu.ID Id, pu.Name Name,
    COUNT(*) LeadCount, 
    SUM(CASE WHEN Status = 'Working' THEN 1 ELSE 0 END) AS Working,
    SUM(CASE WHEN Status = 'Uninterested' THEN 1 ELSE 0 END) AS Uninterested,
    SUM(CASE WHEN Status = 'Converted' THEN 1 ELSE 0 END) AS Converted,
    SUM(CASE WHEN SnoozedId > 0 THEN 1 ELSE 0 END) AS Snoozed,
    Snoozed + 100 AS Test
FROM
    Prospects p
INNER JOIN 
    ProspectsUsers pu on p.OwnerId = pu.SalesForceId
WHERE 
    p.Store = '108'
GROUP BY 
    pu.Name, pu.Id
ORDER BY 
    Name

我收到此错误:

  

无效的列名称“已延后”。

如何获取上一个SUM语句的值,将其加100,然后将其作为另一列返回?我的目标是在另一列名为Test的列中添加贪睡计数+ 100。

4 个答案:

答案 0 :(得分:1)

您不能使用与尝试使用相同的方式来创建另一列。您有2个选择:

  1. 进行完整的计算(如@forpas在上面的评论中所述)
  2. 使用临时表或表变量存储数据,这样您可以获取前5列,然后可以添加最后一列,或者可以从临时表中选择并从那里进行最后一列的计算。

答案 1 :(得分:1)

在同一查询中,不能将别名用作列引用。正确的脚本是:

SELECT 
    pu.ID Id, pu.Name Name,
    COUNT(*) LeadCount, 
    SUM(CASE WHEN Status = 'Working' THEN 1 ELSE 0 END) AS Working,
    SUM(CASE WHEN Status = 'Uninterested' THEN 1 ELSE 0 END) AS Uninterested,
    SUM(CASE WHEN Status = 'Converted' THEN 1 ELSE 0 END) AS Converted,
    SUM(CASE WHEN SnoozedId > 0 THEN 1 ELSE 0 END)+100 AS Snoozed
FROM
    Prospects p
INNER JOIN 
    ProspectsUsers pu on p.OwnerId = pu.SalesForceId
WHERE 
    p.Store = '108'
GROUP BY 
    pu.Name, pu.Id
ORDER BY 
    Name

答案 2 :(得分:0)

您不能在同一选择中使用列别名。列别名不优先/顺序;它们都是在选择结果评估之后,分组依据和顺序依据之前创建的。

您必须重复输入代码:

SELECT 
    pu.ID Id,pu.Name Name,
    COUNT(*) LeadCount, 
    SUM(CASE WHEN Status = 'Working' THEN 1 ELSE 0 END) AS Working,
    SUM(CASE WHEN Status = 'Uninterested' THEN 1 ELSE 0 END) AS Uninterested,
    SUM(CASE WHEN Status = 'Converted' THEN 1 ELSE 0 END) AS Converted,
    SUM(CASE WHEN SnoozedId > 0 THEN 1 ELSE 0 END) AS Snoozed,
    SUM(CASE WHEN SnoozedId > 0 THEN 1 ELSE 0 END)+ 100 AS Test
FROM 
    Prospects p
INNER JOIN 
    ProspectsUsers pu on p.OwnerId = pu.SalesForceId
WHERE 
    p.Store = '108'
GROUP BY 
    pu.Name, pu.Id
ORDER BY    
    Name

如果您不想重复代码,请使用子查询

SELECT
    ID, Name, LeadCount, Working, Uninterested,Converted, Snoozed, Snoozed +100 AS test  
FROM
    (SELECT 
          pu.ID Id,pu.Name Name,
            COUNT(*) LeadCount, 
            SUM(CASE WHEN Status = 'Working' THEN 1 ELSE 0 END) AS Working,
            SUM(CASE WHEN Status = 'Uninterested' THEN 1 ELSE 0 END) AS Uninterested,
            SUM(CASE WHEN Status = 'Converted' THEN 1 ELSE 0 END) AS Converted,
            SUM(CASE WHEN SnoozedId > 0 THEN 1 ELSE 0 END) AS Snoozed

      FROM Prospects p
      INNER JOIN ProspectsUsers pu on p.OwnerId = pu.SalesForceId
      WHERE p.Store = '108'
      GROUP BY pu.Name, pu.Id) t 
   ORDER BY Name

或视图

答案 3 :(得分:0)

MSSQL不允许您从同一SELECT语句中引用SELECT语句中的字段(或别名)。

要解决此问题:

  1. Use a CTE。在CTE中定义要从中选择的列,然后在CTE之外从中选择。
;WITH OurCte AS (
SELECT
    5 + 5 - 3 AS OurInitialValue
)

SELECT
    OurInitialValue / 2 AS OurFinalValue
FROM OurCte
  1. 使用临时表。这在功能上与使用CTE非常相似,但是它确实具有不同的性能含义。
SELECT
    5 + 5 - 3 AS OurInitialValue
INTO #OurTempTable

SELECT
    OurInitialValue / 2 AS OurFinalValue
FROM #OurTempTable
  1. 使用子查询。与上述相比,这往往更难以阅读。我不确定这样做有什么好处-也许评论中的人可以启发我。
SELECT
    5 + 5 - 3 AS OurInitialValue
FROM (
    SELECT
        OurInitialValue / 2 AS OurFinalValue
) OurSubquery
  1. 嵌入您的计算。 警告警告这确实很草率,而且不是很好的方法,因为您最终不得不复制代码,并且如果在一个位置而不是另一个位置更新计算,则很容易使列不同步。 。
SELECT
    5 + 5 - 3 AS OurInitialValue
    , (5 + 5 - 3) / 2 AS OurFinalValue