【问题标题】:ORA-01789: Query block has incorrect number of results columnsORA-01789: 查询块的结果列数不正确
【发布时间】:2018-12-06 03:40:30
【问题描述】:

我有这个(顶级查询)可以完美运行,但我正在尝试添加第二个查询以使用它,它所做的只是计算每天的总和。我对编写 SQL 还是比较陌生,我想知道是否有人可以帮助解决这个问题?我尝试使用 UNION 但不断收到查询块的结果列数不正确

提前谢谢你!

Select POSTING_DATE, MISTI_ID, MATERIAL, CASE WHEN MTL_DESC IS NULL THEN SHORT_TEXT ELSE MTL_DESC END AS MTL_DESC, VEND_NAME1, RECIPIENT, QTY, AMT_GRP_CURR From COST_DM5_DAILY Where COST_CENTER = 'C1003' ORDER BY POSTING_DATE DESC

这是第二个查询

Select SUM(AMT_GRP_CURR), Posting_date From COST_DM5_DAILY Where COST_CENTER = 'C1003' group by Posting_date order by Posting_date desc

【问题讨论】:

  • 样本数据和期望的结果真的很有帮助。
  • 你不能在一个查询中完成吗?

标签: sql oracle plsql


【解决方案1】:

错误表明 UNION 中使用的列的数量和数据类型必须匹配。看看这个简单的例子:

第二个select 中缺少col2

SQL> select 100 col1, 'a' col2 from dual
  2  union
  3  select 300 col1           from dual;
select 100 col1, 'a' col2 from dual
*
ERROR at line 1:
ORA-01789: query block has incorrect number of result columns    

第二个select 中的col2 数据类型错误:

SQL> select 100 col1, 'a' col2 from dual
  2  union
  3  select 300 col1, sysdate  from dual;
select 100 col1, 'a' col2 from dual
                 *
ERROR at line 1:
ORA-01790: expression must have same datatype as corresponding expression

最后,要走的路:

SQL> select 100 col1, 'a' col2 from dual
  2  union
  3  select 300 col1, 'b' col2 from dual;

      COL1 C
---------- -
       100 a
       300 b

SQL>

就您的代码而言,您似乎不能这样做 - 至少,这没有意义,因为您必须在 @987654329 中使用 GROUP BY all 列@未聚合的列列表:

select posting_date,
       misti_id,
       material,
       case
         when mtl_desc is null then short_text
         else mtl_desc
       end as mtl_desc,
      vend_name1,
      recipient,
      qty,
      amt_grp_curr,
      --
      sum(amg_grp_curr) sum_result
from cost_dm5_daily
where cost_center = 'C1003'
group by 
       posting_date,
       misti_id,
       material,
       case
         when mtl_desc is null then short_text
         else mtl_desc
       end as mtl_desc,
      vend_name1,
      recipient,
      qty,
      amt_grp_curr
order by posting_date desc;

也许SUM 函数的解析形式可能会有所帮助。例如:

select posting_date,
       misti_id,
       material,
       case
         when mtl_desc is null then short_text
         else mtl_desc
       end as mtl_desc,
      vend_name1,
      recipient,
      qty,
      amt_grp_curr,
      --
      sum(amg_grp_curr) over(partition by posting_date order by null) sum_result
from cost_dm5_daily
where cost_center = 'C1003'
order by posting_date desc;

【讨论】:

    【解决方案2】:

    您的第一个查询的结果集中有 8 列,而第二个查询只有 2 个;这解释了错误消息。第一个也有日期列,这是次要问题 - 列的数据类型必须在每个位置匹配(或者至少可以隐式转换)。

    您需要在第二个查询中交换列的顺序(因此日期排在第一位),然后决定您想要总和的位置 - 并为其余的添加空值(可能;或其他一些固定值)。

    Select POSTING_DATE,
           MISTI_ID,
           MATERIAL,
           CASE
             WHEN MTL_DESC IS NULL THEN
              SHORT_TEXT
             ELSE
             MTL_DESC
             END AS MTL_DESC,
           VEND_NAME1,
           RECIPIENT,
           QTY,
           AMT_GRP_CURR
      From COST_DM5_DAILY
      Where COST_CENTER = 'C1003'
    union all
    Select Posting_date,
           NULL,
           NULL,
           NULL,
           NULL,
           NULL,
           NULL,
           SUM(AMT_GRP_CURR)
      From COST_DM5_DAILY
      Where COST_CENTER = 'C1003'
      group by Posting_date
      order by Posting_date desc;
    

    您可能需要在 order by 中添加附加条款,因为您将在同一日期有多个条款。

    这可能是您的应用程序/报告层应该做的事情。

    【讨论】:

      猜你喜欢
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      相关资源
      最近更新 更多