如何在保留 group by 子句的同时在同一个 table 中再添加一列

How to add one more column in same table while keeping the group by clause

表 1 中的列列表: Plan_ID、Claim_id、Patient_id、B_OR_G

表 2 中的列列表: ORGID, 沙普兰

select distinct a.Plan_ID
       , a.Total_Claims
       , Total_Patients
       ,  b.PERIOD
       , b.ORGID,a.B_OR_G
FROM (Select distinct Plan_ID
             , count(distinct Claim_id) as Total_Claims
             , count(distinct Patient_id)  as Total_Patients 
      from table1 group by 1) a
JOIN (select *
             , row_number() over (partition by ORGID,SHAPLANID order by PROCESSINGDATE desc) as rank
      from table2 qualify rank = 1) b
ON LTRIM(a.PLAN_ID, '0') = b.SHAPLANID

在上面的查询中,我想从表 1 中再提取一个名为 'B_or_G' 的列(即 a),但不干扰 group by 子句,因为根据我们的要求这是必要的。

有没有更好的方法来做到这一点? 谢谢!!

我想你可以使用 ANY_VALUE(B_or_G)

以你的例子为例:

select distinct a.Plan_ID
       , a.Total_Claims
       , Total_Patients
       ,  b.PERIOD
       , b.ORGID,a.B_OR_G
FROM (Select distinct Plan_ID
             , count(distinct Claim_id) as Total_Claims
             , count(distinct Patient_id)  as Total_Patients 
             , ANY_VALUE(B_OR_G)
      from table1 group by 1) a
JOIN (select *
             , row_number() over (partition by ORGID,SHAPLANID order by PROCESSINGDATE desc) as rank
      from table2 qualify rank = 1) b
ON LTRIM(a.PLAN_ID, '0') = b.SHAPLANID

,希望对您有所帮助。