用于在报告中获取正确的延迟部门的SQL查询

时间:2013-09-17 13:12:25

标签: sql ms-access accounting

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

  1. Dossier_id
  2. 金额
  3. 所许
  4. 时滞 (所有字段均为数字)
  5. 想象一下包含这些条目的表格:

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

    如何编写查询以计算(仅)该表的结尾部分(总计)以实现正确的延迟率作为总负债? (我玩内联,但我不能很清楚)

2 个答案:

答案 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;