你可以在MySQL中的两个别名列上使用数学计算吗?

问题描述 投票:0回答:4

我想做这个:

SELECT SUM(currentbalance) AS 'current', SUM(principal) AS 'prin', 'current' + 'prin' AS 'total'
FROM dbase

这行不通。我也尝试过:

SELECT sum(currentbalance) AS 'current', sum(principal) AS 'prin', (SELECT 'current') + (SELECT 'prin') AS 'total'
FROM dbase

@juergen-d @fqhv

两个答案都很完美。我认为这足以让我了解如何将其添加到我当前的查询中。我怎样才能在这个查询中实现它?我需要将“file_principal”除以“total_cash_cleared”?预先感谢。

SELECT a.portfolio
       ,a.dateplaced
       ,(SELECT SUM(chargedoffprincipal) FROM Dbase WHERE portfolio = a.portfolio ) AS 'file_principal'
       ,SUM(IF(b.paymentstatus IN ('POSTED','PROCCESED'), paymentamount, 0)) AS 'total_cash_cleared'
       ,SUM(IF(b.paymentstatus = 'PENDING', paymentamount, 0)) AS 'total_cash_pending'
       ,SUM(IF(b.paymentstatus = 'PENDING' AND YEAR(paymentdate) = YEAR(CURRENT_DATE + INTERVAL 1 MONTH) AND MONTH(paymentdate) = MONTH(CURRENT_DATE + INTERVAL 1 MONTH),paymentamount, 0)) AS '30_days'
       ,SUM(IF(b.paymentstatus = 'PENDING' AND YEAR(paymentdate) = YEAR(CURRENT_DATE + INTERVAL 1 MONTH) AND MONTH(paymentdate) = MONTH(CURRENT_DATE + INTERVAL 2 MONTH),paymentamount, 0)) AS '60_days'
       ,SUM(IF(b.paymentstatus = 'PENDING' AND YEAR(paymentdate) = YEAR(CURRENT_DATE + INTERVAL 1 MONTH) AND MONTH(paymentdate) = MONTH(CURRENT_DATE + INTERVAL 3 MONTH),paymentamount, 0)) AS '90_days'
FROM dbase a
LEFT JOIN payments b
ON a.filenumber = b.filenumber
GROUP BY a.portfolio
ORDER BY STR_TO_DATE(a.dateplaced, '%m/%d/%Y') DESC

我尝试在评论中添加此内容,但它太长了,所以我编辑了这篇文章,抱歉。

mysql
4个回答
0
投票

不能在同一个 SELECT 子句中重复使用别名

SELECT SUM(currentbalance) AS 'current', 
       SUM(principal) AS 'prin', 
       SUM(currentbalance + principal) AS 'total'
FROM dbase

0
投票

我更喜欢juergen的答案,但是如果将其放入子查询中,则可以添加使用别名。

SELECT [current] + [prin]
FROM (
    SELECT 
      SUM(currentbalance) AS 'current',
      SUM(principal) AS 'prin'
    FROM dbase ) x

0
投票

感谢我的懒哥们,这是处理这个问题的方法。现在一切都有意义了。

SELECT
  portfolio, 
  dateplaced, 
  file_principal,
  total_cash_cleared,
  IF(total_cash_cleared <> 0, file_principal / total_cash_cleared, 0) AS div_result,
  total_cash_pending,
  30_days,
  60_days,
  90_days
FROM
  (SELECT a.portfolio
       ,a.dateplaced
         ,(SELECT SUM(chargedoffprincipal) FROM Dbase WHERE portfolio = a.portfolio ) AS 'file_principal'
         ,SUM(IF(b.paymentstatus IN ('POSTED','PROCCESED'), paymentamount, 0)) AS 'total_cash_cleared'
         ,SUM(IF(b.paymentstatus = 'PENDING', paymentamount, 0)) AS 'total_cash_pending'
         ,SUM(IF(b.paymentstatus = 'PENDING' AND YEAR(paymentdate) = YEAR(CURRENT_DATE + INTERVAL 1 MONTH) AND MONTH(paymentdate) = MONTH(CURRENT_DATE + INTERVAL 1 MONTH),paymentamount, 0)) AS '30_days'
         ,SUM(IF(b.paymentstatus = 'PENDING' AND YEAR(paymentdate) = YEAR(CURRENT_DATE + INTERVAL 1 MONTH) AND MONTH(paymentdate) = MONTH(CURRENT_DATE + INTERVAL 2 MONTH),paymentamount, 0)) AS '60_days'
         ,SUM(IF(b.paymentstatus = 'PENDING' AND YEAR(paymentdate) = YEAR(CURRENT_DATE + INTERVAL 1 MONTH) AND MONTH(paymentdate) = MONTH(CURRENT_DATE + INTERVAL 3 MONTH),paymentamount, 0)) AS '90_days'
  FROM dbase a
  LEFT JOIN payments b
  ON a.filenumber = b.filenumber
  GROUP BY a.portfolio
) AS original_query
ORDER BY STR_TO_DATE(original_query.dateplaced, '%m/%d/%Y') DESC

0
投票

我发现的最干净的方法是使用变量进行数学计算,然后将该变量分配给别名以显示结果。

@val1 =  SUM(currentbalance), 
@val2 =  SUM(principal),
@val1+@val2 AS 'Total',
// to show the values at columns
@val1 AS 'current',
@val2 AS 'prin', 
   
© www.soinside.com 2019 - 2024. All rights reserved.