在Interbase子查询中正确使用别名

时间:2012-05-08 17:27:41

标签: sql alias interbase

我新创建的别名ABONO和CARGO有问题。 ALIASES(对于结果的列名称)和SQL查询工作正常UNTIL我尝试计算“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"

2 个答案:

答案 0 :(得分:2)

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

你要么必须在报告工具中进行计算,要么重复两次查询以获得价值(即使使用copy'n'paste,写入也很痛苦 - 这只是获得良好报告的另一个原因工具)。

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,但这是你的决定。我不确定你是否真的需要所有这些分隔标识符,但我基本上没有触及它们。

答案 1 :(得分:0)

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