使用多个SELECT INTO语句编译ACE报告时出错

时间:2010-10-04 20:53:48

标签: sql informix

INFORMIX-SQL 4.10:

好的,所以在修复INTO TEMP语法并使用AS别名后,ACE编译器抱怨在GROUP BY子句中没有每个别名,所以我将它添加到每个SELECT语句中。但是现在我仍然在FORMAT语句上获得GRAM ERR(参见更新的代码示例)

database PAWNSHOP
END

define 
variable sfecha date
variable efecha date
end

input
prompt for sfecha using "DESDE FECHA: "
prompt for efecha using "HASTA FECHA: "

end

output
report to printer
top margin 0
bottom margin 0
page length 33
left margin 0
right margin 80
end

select count(*)      AS rcount,
       pwd_trx_date  AS rtrxdate,
       pwd_trx_type  AS rtrxtype,
       pwd_last_type AS rlasttype,
       pwd_last_amt  AS rlastamt,
       pwd_pawn_amt  AS rpawnamt,
       pwd_cob1      AS rcob1,
       pwd_cob2      AS rcob2,
       pwd_cob3      AS rcob3,
       pwd_cob4      AS rcob4
  from boletos
 where pwd_trx_date >= $sfecha
   and pwd_trx_date <= $efecha
   and pwd_trx_type = "E"
   and pwd_last_type = "R"
 group 
    by rtrxdate,
       rtrxtype,
       rlasttype,
       rlastamt,
       rpawnamt,
       rcob1,
       rcob2,
       rcob3,
       rcob4
  into 
  temp r;

select count(*)      AS icount,
       pwd_trx_date  AS itrxdate,
       pwd_trx_type  AS itrxtype,
       pwd_last_type AS ilasttype,
       pwd_last_amt  AS ilastamt
  from boletos
 where pwd_trx_date >= $sfecha
   and pwd_trx_date <= $efecha
   and pwd_trx_type = "E"
   and pwd_last_type = "I"
 group 
    by itrxdate, 
       itrxtype, 
       ilasttype, 
       ilastamt
  into
  temp i;

select count(*)      AS fcount,
       pwd_trx_date  AS ftrxdate,
       pwd_trx_type  AS ftrxtype,
       pwd_last_type AS flasttype,
       pwd_last_amt  AS flastamt,
       pwd_pawn_amt  AS fpawnamt
  from boletos
 where pwd_trx_date >= $sfecha
   and pwd_trx_date <= $efecha
   and pwd_trx_type IN ("E","C","P")
   and pwd_last_type = "F"
 group 
    by ftrxdate,
       ftrxtype,
       flasttype,
       flastamt,
       fpawnamt
  into
  temp f;

select count(*)      AS pcount,
       pwd_trx_date  AS ptrxdate,
       pwd_trx_type  AS ptrxtype,
       pwd_last_type AS plasttype,
       pwd_last_amt  AS plastamt,
       pwd_pawn_amt  AS ppawnamt
  from boletos
 where pwd_trx_date >= $sfecha
   and pwd_trx_date <= $efecha
   and pwd_trx_type = "P"
   and pwd_last_type = "R"
 group 
    by ptrxdate,
       ptrxtype,
       plasttype,
       plastamt,
       ppawnamt
  into
  temp p;

select count(*)      AS ecount,
       pwd_trx_date  AS etrxdate,
       pwd_trx_type  AS etrxtype,
       pwd_last_type AS elasttype,
       pwd_last_amt  AS elastamt,
       pwd_pawn_amt  AS epawnamt
  from boletos
 where pwd_trx_date >= $sfecha
   and pwd_trx_date <= $efecha
   and pwd_trx_type = "E"
   and pwd_last_type = "E"
 group 
    by etrxdate,
       etrxtype,
       elasttype,
       elastamt,
       epawnamt
  into
  temp e;

select count(*)      AS ccount,
       pwd_trx_date  AS ctrxdate,
       pwd_trx_type  AS ctrxtype,
       pwd_last_type AS clasttype,
       pwd_pawn_amt  AS cpawnamt
  from boletos
 where pwd_trx_date >= $sfecha
   and pwd_trx_date <= $efecha
   and pwd_trx_type = "C"
   and pwd_last_type = "C"
 group 
    by ctrxdate,
       ctrxtype,
       clasttype,
       cpawnamt
  into
  temp c

end



format
   **^
   GRAM ERR UNDESIREABLE CONSTRUCT**


after group of 
       rtrxdate,
       rtrxtype,
       rlasttype,
       rlastamt,
       rpawnamt,
       rcob1,
       rcob2,
       rcob3,
       rcob4

print column  1,"CANTIDAD INGRESOS    TOTAL              GANANCIA"
print column  1,"-------- --------- -------              --------"

print column  2,group total of rcount using "###,###",
      column 10,"RETIROS",
      column 20,group total of rlastamt "###,###",
      column 42,(
                (group total of rcob1) +
                (group total of rcob2) +
                (group total of rcob3) +
                (group total of rcob4)
                ) - 
                (group total of rpawnamt) using "###,###" 

after group of
       itrxdate, 
       itrxtype, 
       ilasttype, 
       ilastamt

print column  2,group total of icount using "###,###",
      column 10,"INTERESES",
      column 20,group total of ilastamt using "###,###",
      column 42,group total of ilastamt using "###,###" 



after group of 
       ftrxdate,
       ftrxtype,
       flasttype,
       flastamt,
       fpawnamt

print column  2,group total of fcount using "###,###",
      column 10,"FUNDIDOS",
      column 20,group total of flastamt using "###,###",
      column 42,(group total of flastamt) - 
                (group total of fpawnamt) using "###,###" 



after group of 
       ptrxdate,
       ptrxtype,
       plasttype,
       plastamt,
       ppawnamt

print column  2,group total of pcount using "###,##&",
      column 10,"PLATERIA",
      column 20,group total of plastamt using "###,###",
      column 42,group total of plastamt using "###,###"





after group of
       etrxdate,
       etrxtype,
       elasttype,
       elastamt,
       epawnamt

skip 2 lines

print column  1,"CANTIDAD EGRESOS     TOTAL  "
print column  1,"-------- --------- -------  "

print column  2,group total of ecount using "###,###",
      column 10,"PRESTAMOS",
      column 20,group total of elastamt using "###,###"



after group of 
       ctrxdate,
       ctrxtype,
       clasttype,
       cpawnamt

print column  2,group total of ccount using "###,###",
      column 10,"COMPRAS  ",
      column 20,group total of clastamt using "###,###"


end

4 个答案:

答案 0 :(得分:2)

INTO TEMP子句是最后一个 - 必须在FROM,WHERE,GROUP BY和HAVING子句之后。它不能出现在ORDER BY中,但也会出现在它之后。

因此(仅使用第一个查询作为示例):

select count(*) AS counter,  -- Aggregates or expressions must be named
       pwd_trx_date,
       pwd_trx_type,
       pwd_last_type,
       pwd_last_amt,
       pwd_pawn_amt,
       pwd_cob1,
       pwd_cob2,
       pwd_cob3,
       pwd_cob4
  from boletos
 where pwd_trx_date >= $sfecha
   and pwd_trx_date <= $efecha
   and pwd_trx_type = "E"
   and pwd_last_type = "R"
  INTO TEMP r;

如果看起来似乎有道理,则需要更多列重命名,然后:

select count(*)       AS rcount,
       pwd_trx_date   AS rtrxdate,
       pwd_trx_type   AS trxtype,
       pwd_last_type  AS rlasttype,
       pwd_last_amt   AS rlastamt,
       pwd_pawn_amt   AS rpawnamt,
       pwd_cob1       AS rcob1,
       pwd_cob2       AS rcob2,
       pwd_cob3       AS rcob3,
       pwd_cob4       AS rcob4
  from boletos
 where pwd_trx_date >= $sfecha
   and pwd_trx_date <= $efecha
   and pwd_trx_type = "E"
   and pwd_last_type = "R"
  INTO TEMP r;

你的问题“最终的SELECT看起来是什么样的”的答案取决于报告产生的内容。我已经获得了整个报告的副本(253行),应用了一些最小的重新格式化并将其缩小为193行(单行上的GROUP BY子句;使用pwd_trx_date BETWEEN $sfecha AND $efecha作为日期范围)。

您有6个临时表,所有表都选自相同的Boletos表,具有相同的日期范围,并且在pwd_trx_typepwd_last_type字段中具有不同的条件集。不幸的是,这6个查询显示了3组不同的分组列,以及许多不同数量的字段。

目前尚不清楚这些结果是否应与UNION或一组连接组合使用。

如果答案是UNION,则需要确保所有中间表的列数与“r”(具有最多列的临时表)相同,或者您必须使用虚拟对象编写UNION为每个“窄”表提供的字段与“最宽”表匹配。

如果答案是JOIN,则需要定义连接条件 - 我们不熟悉您的DBMS的人没有机会获得正确的连接。

无论答案是什么(JOIN或UNION),您还需要SELECT中的ORDER BY子句。这将控制数据呈现给报告的顺序。

您的'BEFORE GROUP OF'和'AFTER GROUP OF'子句一次只能列出一个变量。这些变量必须在最终SELECT的ORDER BY子句中。

看起来好像要连接6个单独的报告:一个报告使用临时表r;下一个使用临时表'i',然后'f'等。这不是一个有助于简单ACE报告的结构。最好有6个单独的报告,每个报告写入一个单独的文件,然后组合(连接)这些单独的报告。您可能会安排将日期作为参数而不是输入提供,因此用户只需输入一次日期,但控制shell脚本将运行6个报告,其中两个日期与参数相同。

鉴于上面显示的临时表'r',与之相关的输出格式(在您看来 - 不幸的是,ACE对事物有不同的看法)是:

AFTER GROUP OF
       rtrxdate,
       rtrxtype,
       rlasttype,
       rlastamt,
       rpawnamt,
       rcob1,
       rcob2,
       rcob3,
       rcob4

PRINT COLUMN  1,"CANTIDAD INGRESOS    TOTAL              GANANCIA"
PRINT COLUMN  1,"-------- --------- -------              --------"

PRINT COLUMN  2,GROUP TOTAL OF rcount USING "###,###",
      COLUMN 10,"RETIROS",
      COLUMN 20,GROUP TOTAL OF rlastamt "###,###",
      COLUMN 42,(
                (GROUP TOTAL OF rcob1) +
                (GROUP TOTAL OF rcob2) +
                (GROUP TOTAL OF rcob3) +
                (GROUP TOTAL OF rcob4)
                ) -
                (GROUP TOTAL OF rpawnamt) USING "###,###"

大多数其他格式化部分有些相似 - 它们具有相同的四列。两个部分只有3列。我认为您将要将查询结构化为UNION查询。 我想这意味着你将修改你的主要系列查询:

SELECT COUNT(*)           AS rcount,
       pwd_trx_date       AS rtrxdate,
       pwd_trx_type       AS rtrxtype,
       pwd_last_type      AS rlasttype,
       "RETIROS"          AS rlabel,
       1                  AS rsequence,
       SUM(pwd_last_amt)  AS rcol3,
       (SUM(pwd_cob1) + SUM(pwd_cob2) + SUM(pwd_cob3) + SUM(pwd_cob4) -
        SUM(pwd_pawn_amt) AS rcol4
  FROM boletos
 WHERE pwd_trx_date BETWEEN $sfecha AND $efecha
   AND pwd_trx_type = "E"
   AND pwd_last_type = "R"
 GROUP BY rtrxdate, rtrxtype, rlasttype
  INTO TEMP r;

您的其他查询将生成相同数量的列。 rsequence确保来自'r'的行将出现在'i'(将具有rsequence = 2)和'f'(将具有rsequence = 3)的行之前,依此类推。 rlabel值允许您正确打印标题。

您的UNION将能够:

SELECT * FROM r
UNION
SELECT * FROM i
UNION
...
SELECT * FROM c
ORDER BY rsequence, rtrxdate, rtrxtype, rlasttype

您的格式部分将包含:

PAGE HEADER
    PRINT COLUMN  1,"CANTIDAD INGRESOS    TOTAL              GANANCIA"
    PRINT COLUMN  1,"-------- --------- -------              --------"

BEFORE GROUP OF rsequence
    SKIP 1 LINE

ON EVERY ROW
    PRINT COLUMN  2, rcount USING "###,###",
          COLUMN 10, rlabel,
          COLUMN 20, rcol3,
          COLUMN 42, rcol4

由于有两个组没有rcol4的“实际”值 - 您可能只需选择0或SUM(0)作为它们的虚拟列,您可能需要:

ON EVERY ROW
    IF rsequence <= 4 THEN
        PRINT COLUMN  2, rcount USING "###,###",
              COLUMN 10, rlabel,
              COLUMN 20, rcol3,
              COLUMN 42, rcol4
    ELSE
        PRINT COLUMN  2, rcount USING "###,###",
              COLUMN 10, rlabel,
              COLUMN 20, rcol3

从这里开始,你就是独立的。

答案 1 :(得分:1)

我做了任何Informix已经有一段时间了,但我认为INTO TEMP子句出现在查询的末尾,我不确定是否将列名指定为临时表的一部分。请尝试使用此版本:

select count(*) as rcount,
       pwd_trx_date,
       pwd_trx_type,
       pwd_last_type,
       pwd_last_amt,
       pwd_pawn_amt,
       pwd_cob1,
       pwd_cob2,
       pwd_cob3,
       pwd_cob4
  from boletos
 where pwd_trx_date >= $sfecha
   and pwd_trx_date <= $efecha
   and pwd_trx_type = "E"
   and pwd_last_type = "R"
  into temp r;

答案 2 :(得分:0)

语法“SELECT ... INTO”仅在select返回恰好一行时才有效。

如果没有一个结果,您将收到类似“返回的子请求不是一行”的错误。

答案 3 :(得分:0)

好吧,我使用ORDER BY和GROUP BY解决了这个问题。请注意,列必须以相反的顺序放置在SELECT语句的ORDER BY子句中。以下ACE报告完成了挑战:

database PAWNSHOP
END

define 
variable sfecha date
variable efecha date
variable dummy integer
end

input
prompt for sfecha using "DESDE FECHA: "
prompt for efecha using "HASTA FECHA: "

end

output
{report to printer}
top margin 0
bottom margin 0
page length 24
left margin 0
right margin 80
end

select trxdate,
       trxtype,
       trxcode,
       trxamt,
       trxprofit
  from trx
 where trxdate >= $sfecha
   and trxdate <= $efecha
order by trxcode,trxtype,trxdate
end

format

page trailer
pause


page header

skip 2 lines

print column  21,"Transacciones del sistema viejo y sistema nuevo."

print column  21,"Totales desde  ",sfecha using "mmm-dd-yy",
                "  hasta  ",efecha using "mmm-dd-yy"

skip 1 line


print column  1,
"             CONTEO                       TOTAL               GANANCIA"
print column  1,
"             ------                      -------              --------"

after group of trxtype

if trxtype = "E" and trxcode = "R" then 
begin
print column 13,group count        using "###,##&",
      column 21,"Empenos Retirados",
      column 42,group total of trxamt    using "###,##&",
      column 64,group total of trxprofit using "###,##&" 
end

if trxtype = "E" and trxcode = "I" then 
begin
print column 13,group count        using "###,##&",
      column 21,"Pagos de Intereses",
      column 42,group total of trxamt    using "###,##&",
      column 64,group total of trxamt    using "###,##&" 
end


if trxtype = "E" and trxcode = "F" then 
begin
print column 13,group count        using "###,##&",
      column 21,"Empenos Fundidos",
      column 42,group total of trxamt    using "###,##&",
      column 64,group total of trxprofit using "###,##&" 
end


if trxtype = "E" and trxcode = "T" then 
begin
print column 13,group count        using "###,##&",
      column 21,"Empenos Transferidos",
      column 42,group total of trxamt    using "###,##&",
      column 64,group total of trxprofit using "###,##&" 
end


if trxtype = "C" and trxcode = "F" then 
begin
print column 13,group count        using "###,##&",
      column 21,"Compras Fundidos",
      column 42,group total of trxamt    using "###,##&",
      column 64,group total of trxprofit using "###,##&" 
end


if trxtype = "C" and trxcode = "T" then 
begin
print column 13,group count        using "###,##&",
      column 21,"Compras Transferidos",
      column 42,group total of trxamt    using "###,##&",
      column 64,group total of trxprofit using "###,##&" 
end



if trxtype = "P" and trxcode = "R" then 
begin
print column 13,group count        using "###,##&",
      column 21,"Plateria Retirados",
      column 42,group total of trxamt    using "###,##&",
      column 64,group total of trxprofit using "###,##&" 
end


if trxtype = "P" and trxcode = "F" then 
begin
print column 13,group count        using "###,##&",
      column 21,"Plateria Fundidos",
      column 42,group total of trxamt    using "###,##&",
      column 64,group total of trxprofit using "###,##&" 
end


if trxtype = "E" and trxcode = "E" then 
begin
print column 13,group count        using "###,##&",
      column 21,"Empenos Nuevos",
      column 42,group total of trxamt    using "###,##&",
      column 62,group total of trxprofit using "-,---,--&" 
end


if trxtype = "C" and trxcode = "C" then 
begin
print column 13,group count        using "###,##&",
      column 21,"Compras Nuevas",
      column 42,group total of trxamt    using "###,##&",
      column 62,group total of trxprofit using "-,---,--&" 
end

on last row

print column 14,"======",
      column 62,"========="
print column 13,count              using "###,##&", 
      column 62,total of trxprofit using "-,---,--&"


end

制作重要报告:

                Merged transactions from old and new systems.
                Totals from  SEP-01-10  to  SEP-30-10

          COUNT                       TOTAL                 PROFIT
         ------                      -------              --------
             32 New Purchases          4,383                -4,383
             73 New Pawns             12,875               -12,875
             20 Purchases Sold         2,001                   491
             53 Forfeited Pawns          193                 5,172
             82 Interest Payments      1,602                 1,602
             47 Redeemed Pawns         8,457                 1,059
         ======                                          =========
            307                                             -8,934