0

有一个包含这些字段的付款表:

  1. Dossier_id
  2. 数量
  3. 已付款
  4. 迟钝(所有字段都是数字)

想象一下包含这些条目的表:

    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
    ==================================================================

我如何编写查询来计算(仅)该表的末尾部分(总计)以实现正确的延迟率作为总负债?(我玩过内部连接,但我想不通)

4

2 回答 2

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

编辑(愚蠢的访问 -加入不支持的问题):

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)
于 2013-09-17T14:01:25.453 回答
-1

它是这个简单的查询:

SELECT    SUM(AMOUNT), SUM(PAYED), SUM(RETARDED) FROM TABLE;
于 2013-09-17T13:16:55.013 回答