我想这样做:
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
我试着在评论中添加这个,但是太长了所以我编辑了这篇文章抱歉。
答案 0 :(得分:0)
您不能在同一个SELECT子句中重用别名
SELECT SUM(currentbalance) AS 'current',
SUM(principal) AS 'prin',
SUM(currentbalance + principal) AS 'total'
FROM dbase
答案 1 :(得分:0)
我更喜欢juergen's answer,但如果将它放在子查询中,则可以使用别名添加。
SELECT [current] + [prin]
FROM (
SELECT
SUM(currentbalance) AS 'current',
SUM(principal) AS 'prin'
FROM dbase ) x
答案 2 :(得分: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