我有两个程序来计算两个数据。现在我想得到这两个输出的总和。在sql中可以吗?
例如:
Select bio.*,
SUM (cnt_report,cnt_report_2) as TOTAL --CAN I DO THIS?
cnt_report + cnt_report_2 as Total --This doesn't seem to work
from biographical bio
LEFT JOIN (
SELECT cr.id, COUNT (*) AS cnt_report
FROM report cr
GROUP BY cr.id
) cr11 ON bio.id = cr11.id
LEFT JOIN (
SELECT cr.id2,
COUNT (*) AS cnt_report_2
FROM report cr
GROUP BY cr.id2
) cr11 ON bio.id = cr11.id2
答案 0 :(得分:1)
您的Sum函数可以进行一些调整,但您需要在末尾添加GROUP BY行。此外,您似乎已使用相同的别名命名了两个子查询。试试这个:
Select bio.*,
SUM (cr11_1.cnt_report + cr11_2.cnt_report_2) as TOTAL
from biographical bio
LEFT JOIN (
SELECT cr.id, COUNT (*) AS cnt_report
FROM report cr
GROUP BY cr.id
) cr11_1 ON bio.id = cr11_1.id
LEFT JOIN (
SELECT cr.id2,
COUNT (*) AS cnt_report_2
FROM report cr
GROUP BY cr.id2
) cr11_2 ON bio.id = cr11_2.id2
GROUP BY bio.*
答案 1 :(得分:0)
你不能这样做:
sum(something, something_else)
但你可以这样做:
sum(something + something_else)
您可以弄清楚如何在查询中实现它。请记住,如果您要选择其他字段以及聚合,则需要group by子句。