如何选择不允许出现在group by子句中的列名?

问题描述 投票:0回答:1

我有sql基础知识,希望有人能解释我以下:我有以下sql查询:

 select
         f001,
         valid1,
         encid1,
         sum(val004) as AMOUNT#221,
         conid1,
         acc206
     from
         (
             select
                 f001,
                 encid1,
                 acc206,
                 valid1,
                 val004,
                 conid1
             from
                 table_tmp 
             where
                     valid2 = 4 and acc206=0 -- for EUR 
         ) table1
     group by
         f001,
        
         conid1,
         valid1,
         acc206
 ) table2

当我运行错误消息时:

00979. 00000 -  "not a GROUP BY expression"
。发生这种情况是因为我在 select 子句中有
encid1
,但在
not
中有
group by clause
。当我将其添加到组中时,错误消息消失了,但我们不想按
encid1
进行分组。现在,当我从 select 子句中删除
encid1
时,我无法使用
encid1
将结果与其他表连接起来。知道如何解决这个问题吗?

sql oracle
1个回答
0
投票

将其包含在

GROUP BY
中:

SELECT f001,
       valid1,
       encid1,
       SUM(val004) AS AMOUNT#221,
       conid1,
       acc206
from   table_tmp 
where  valid2 = 4
and    acc206 = 0 -- for EUR 
group by
       f001,
       valid1,
       encid1,
       conid1,
       acc206

否则聚合

encid1
列:

SELECT f001,
       valid1,
       MIN(encid1) AS min_encid1,
       MAX(encid1) AS max_encid1,
       LISTAGG(encid1, ',') WITHIN GROUP (ORDER BY encid1) AS list_of_encid1,
       SUM(val004) AS AMOUNT#221,
       conid1,
       acc206
from   table_tmp 
where  valid2 = 4
and    acc206 = 0 -- for EUR 
group by
       f001,
       valid1,
       conid1,
       acc206
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.