select 来自三列的不同组合的所有列

select all columns from distinct combination of three column

我必须根据三列的不同组合将记录插入到另一个 table。 Table A 有 50 列,因为我必须取三列的不同值并将所有列值插入另一个 table。我该怎么做?我尝试执行以下操作但出现错误。

select * from engagementmasterstaging 
where EngagementId 
in (select distinct EngagementId,ServiceLine,SubServiceLine 
from engagementmasterstaging)

如果您想要所有列,请使用 window 函数:

select . . .    -- list the columns here
from (select ems.*,
             row_number() over (partition by EngagementId, ServiceLine, SubServiceLine order by EngagementId) as seqnum
      from engagementmasterstaging ems
     ) ems
where seqnum = 1; 

如果您在暂存中有一个主键 table,您可以使用它并仍然使用 select *:

select ems.*
from engagementmasterstaging ems
where ems.pk = (select min(ems2.pk)
                from engagementmasterstaging ems2
                where ems2.EngagementId = ems.EngagementId and
                      ems2.ServiceLine = ems.ServiceLine and
                      ems2.SubServiceLine = ems.SubServiceLine
               );