如何在保留 group by 子句的同时在同一个表中再添加一列
P粉729436537
P粉729436537 2024-04-01 10:43:59
0
1
417

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

表2中的列列表: 奥吉德、沙普兰德

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

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

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

P粉729436537
P粉729436537

全部回复(1)
P粉447785031

我认为您可以使用 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
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板