我有我的数据在postgres数据库。示例数据和所需的输出应该如下所示

问题描述:

enter image description here我有我的数据在postgres数据库。示例数据和所需的输出应该如下所示

我在解决这个问题时非常困难。有人可以看看图片并提供解决方案。 请不要提供涉及任何脚本语言编码的解决方案,因为我不是编程人员。 我想通过SQL查询(postgres)或在Excel中使用Excel公式来解决此问题。

我没有postgres测试(所以这可能不完美)。此解决方案假设opening_id不超过两个locations

Select 
t1.opening_id 
Case when t2.Num >1 THEN 
CONCAT(t1.location,' | ',t2.location) 
ELSE t1.location END as location 
,AVG(t1.days_open,t2.days_open) as days_open 
,t1.age_band 
from table t1 
INNER JOIN(
      Select 
      opening_id 
      ,location 
      ,AVG(days_open) 
      ,COUNT(distinct location) as Num 
      from table 
      Group by opening_id 
        ,location 
      )t2 
ON t1.opening_id=t2.opening_id 
Group by 
     t1.opening_id 
     ,t2.Num 
     ,CONCAT(t1.location,'|',t2.location) 
     ,t1.location 
     ,t1.age_band