错误表明 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;