oracle 有没有一种方法可以在分组查询中使用包含任何函数?

rqcrx0a6  于 2023-04-05  发布在  Oracle
关注(0)|答案(2)|浏览(160)

我需要从我的数据表FRUIT_TRANSPORT(事实)和FRUIT(维度)中创建所需的描述表。对我来说,数据重复,我无法弄清楚。
水果_运输:

水果:

所需表格:

okxuctiv

okxuctiv1#

您可以:

select
  t.receipt,
  case when count(case when f.fruit_color = 'YELLOW' then 1 end) > 0
      then 'yes' else 'no' end as has_yellow,
  case when count(case when f.fruit_color = 'GREEN' then 1 end) > 0
      then 'yes' else 'no' end as has_green,
  sum(t.amount) as amount  
from fruit_transport t
join fruit f on f.fruit_id = t.fruit_id
group by t.receipt
bjg7j2ky

bjg7j2ky2#

与前面的答案略有不同,您可以在case when表达式中生成'yes'字符串,取组中的最大值,并使用coalesce给予默认值'no':

select receipt,
       coalesce(max(case fruit_color when 'YELLOW' then 'yes' end), 'no') has_yellow,
       coalesce(max(case fruit_color when 'GREEN'  then 'yes' end), 'no') has_green,
       sum(amount) amount
from   fruit f
inner join fruit_transport ft on f.fruit_id = ft.fruit_id
group by receipt;

相关问题