0

我对新创建的别名 ABONO 和 CARGO 有疑问。ALIASES(用于结果的列名)和 SQL 查询工作正常,直到我尝试计算“ABONO-CARGO AS AJUSTE”=> Interbase 给出它不知道“ABONO”列的错误。

动态 SQL 错误 SQL 错误代码 = -206 列未知 ABONO

怎么了?

SELECT M."MOV_CUENTA",
       (SELECT SUM(MM."MOV_MONTO") 
          FROM "movimientos" MM
         WHERE MM."MOV_TIPOMOV" = 'A' AND MM."MOV_CUENTA" = CS."ID_CUENTAMUN"
         GROUP BY MM."MOV_CUENTA"
       ) AS ABONO,
       (SELECT SUM(XM."MOV_MONTO")
          FROM "movimientos" XM
         WHERE XM."MOV_TIPOMOV" = 'C' AND XM."MOV_CUENTA" = CS."ID_CUENTAMUN"
         GROUP BY XM."MOV_CUENTA"
       ) AS CARGO,
       ABONO-CARGO AS AJUSTE
  FROM "cuentasSaldo" CS
 INNER JOIN "movimientos" M ON (CS."ID_CUENTAMUN" = M."MOV_CUENTA")
 INNER JOIN "ajustes" AA ON (M."ID_AJUSTE" = AA."ID_AJUSTE")
 INNER JOIN "documentos" DD ON (AA."ID_DOCUMENTO" = DD."ID_DOCUMENTO")
 WHERE M."ID_AJUSTE" IN
           (SELECT A."ID_AJUSTE"
              FROM "ajustes" A
             WHERE A."ID_DOCUMENTO" IN
                     (SELECT D."ID_DOCUMENTO"
                        FROM "documentos" D
                       WHERE D."ID_EMPRESA" = 1 AND D."DOC_EDITABLE" = 1
                     )
           )
ORDER BY M."ID_AJUSTE", M."MOV_CUENTA"
4

2 回答 2

2

简而言之,在许多 SQL DBMS 中,选择列表中给出的列别名不能在查询的其他地方使用,甚至在选择列表的其他部分也不能使用。

您要么必须在报告工具中进行计算,要么重复两个查询以获取值(即使使用复制粘贴,写起来也很痛苦——这只是拥有一个好的报告工具的另一个理由)。

SELECT M."MOV_CUENTA",
       (SELECT SUM(MM."MOV_MONTO") 
          FROM "movimientos" MM
         WHERE MM."MOV_TIPOMOV" = 'A' AND MM."MOV_CUENTA" = CS."ID_CUENTAMUN"
         GROUP BY MM."MOV_CUENTA"
       ) AS ABONO,
       (SELECT SUM(XM."MOV_MONTO")
          FROM "movimientos" XM
         WHERE XM."MOV_TIPOMOV" = 'C' AND XM."MOV_CUENTA" = CS."ID_CUENTAMUN"
         GROUP BY XM."MOV_CUENTA"
       ) AS CARGO,
       ((SELECT SUM(MM."MOV_MONTO") 
          FROM "movimientos" MM
         WHERE MM."MOV_TIPOMOV" = 'A' AND MM."MOV_CUENTA" = CS."ID_CUENTAMUN"
         GROUP BY MM."MOV_CUENTA"
       ) -
       (SELECT SUM(XM."MOV_MONTO")
          FROM "movimientos" XM
         WHERE XM."MOV_TIPOMOV" = 'C' AND XM."MOV_CUENTA" = CS."ID_CUENTAMUN"
         GROUP BY XM."MOV_CUENTA"
       )) AS AJUSTE
  FROM "cuentasSaldo" CS
 INNER JOIN "movimientos" M ON (CS."ID_CUENTAMUN" = M."MOV_CUENTA")
 INNER JOIN "ajustes" AA ON (M."ID_AJUSTE" = AA."ID_AJUSTE")
 INNER JOIN "documentos" DD ON (AA."ID_DOCUMENTO" = DD."ID_DOCUMENTO")
 WHERE M."ID_AJUSTE" IN
           (SELECT A."ID_AJUSTE"
              FROM "ajustes" A
             WHERE A."ID_DOCUMENTO" IN
                     (SELECT D."ID_DOCUMENTO"
                        FROM "documentos" D
                       WHERE D."ID_EMPRESA" = 1 AND D."DOC_EDITABLE" = 1
                     )
           )
ORDER BY M."ID_AJUSTE", M."MOV_CUENTA"

-在这些表达中很容易失去那种孤独。

另一种方法是:

SELECT "MOV_CUENTA", ABONO, CARGO, (ABONO - CARGO) AS AJUSTE
  FROM (SELECT M."ID_AJUSTE",
               M."MOV_CUENTA",
               (SELECT SUM(MM."MOV_MONTO") 
                  FROM "movimientos" MM
                 WHERE MM."MOV_TIPOMOV" = 'A' AND MM."MOV_CUENTA" = CS."ID_CUENTAMUN"
                 GROUP BY MM."MOV_CUENTA"
               ) AS ABONO,
               (SELECT SUM(XM."MOV_MONTO")
                  FROM "movimientos" XM
                 WHERE XM."MOV_TIPOMOV" = 'C' AND XM."MOV_CUENTA" = CS."ID_CUENTAMUN"
                 GROUP BY XM."MOV_CUENTA"
               ) AS CARGO
          FROM "cuentasSaldo" CS
         INNER JOIN "movimientos" M ON (CS."ID_CUENTAMUN" = M."MOV_CUENTA")
         INNER JOIN "ajustes" AA ON (M."ID_AJUSTE" = AA."ID_AJUSTE")
         INNER JOIN "documentos" DD ON (AA."ID_DOCUMENTO" = DD."ID_DOCUMENTO")
         WHERE M."ID_AJUSTE" IN
                   (SELECT A."ID_AJUSTE"
                      FROM "ajustes" A
                     WHERE A."ID_DOCUMENTO" IN
                             (SELECT D."ID_DOCUMENTO"
                                FROM "documentos" D
                               WHERE D."ID_EMPRESA" = 1 AND D."DOC_EDITABLE" = 1
                             )
                   )
       ) AS X
ORDER BY "ID_AJUSTE", "MOV_CUENTA"

我认为您也应该选择 ID_AJUSTE,但这是您的决定。我也不确定您是否真的需要所有这些分隔标识符,但我基本上没有触及它们。

于 2012-05-08T17:42:24.803 回答
0

您应该在表“cuentasSaldo”(CS)中有一个字段 ABONO,并且您有一个别名为 ABONO 的子查询。所以你有两个同名的“对象”。我建议您尝试以以下格式调用字段:query.field。第 6 行中的示例:而不是:ABONO-CARGO AS AJUSTE 使用:CS.ABONO-CARGO AS AJUSTE

于 2012-05-08T19:42:28.123 回答