有一个包含这些字段的付款表:
想象一下包含这些条目的表格:
Dossier_id || Year || Amount || Payed || Retarded
==================================================================
1000 2010 500 100 400
2000 2007 700 500 200
1000 2011 1200 700 500
2000 2009 900 800 100
==================================================================
Total || 3300 2100 600
==================================================================
如何编写查询以计算(仅)该表的结尾部分(总计)以实现正确的延迟率作为总负债? (我玩内联,但我不能很清楚)
答案 0 :(得分:2)
这应该这样做:
SELECT SUM(a.amount), sum(a.payed), b.retarded
FROM table a INNER JOIN (SELECT SUM(retarded) AS retarded
FROM table b INNER JOIN (SELECT dossier_id, MAX(year) FROM table) c ON b.dossier_id = c.dossier_id AND b.year = c.year) b ON 1 = 1
编辑(愚蠢的访问 - join not supported issue):
SELECT SUM(a.amount), sum(a.payed), b.retarded
FROM table a INNER JOIN (SELECT SUM(retarded) AS retarded
FROM table b INNER JOIN (SELECT dossier_id, MAX(year) FROM table) c ON (b.dossier_id = c.dossier_id AND b.year = c.year)) b ON (1 = 1)
答案 1 :(得分:-1)
这个简单的查询:
SELECT SUM(AMOUNT), SUM(PAYED), SUM(RETARDED) FROM TABLE;