Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 从三列的不同组合中选择所有列_Sql_Azure Sql Database_Azure Data Factory - Fatal编程技术网

Sql 从三列的不同组合中选择所有列

Sql 从三列的不同组合中选择所有列,sql,azure-sql-database,azure-data-factory,Sql,Azure Sql Database,Azure Data Factory,我必须根据三列的不同组合将记录插入另一个表中。表A有50列,因为我必须取三列的不同值,并将所有列值插入到另一个表中。我怎么能做到?我试着做下面的事情,但出错了 select * from engagementmasterstaging where EngagementId in (select distinct EngagementId,ServiceLine,SubServiceLine from engagementmasterstaging) 如果需要所有列,请使用窗口函数: se

我必须根据三列的不同组合将记录插入另一个表中。表A有50列,因为我必须取三列的不同值,并将所有列值插入到另一个表中。我怎么能做到?我试着做下面的事情,但出错了

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

如果需要所有列,请使用窗口函数:

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; 
如果暂存表中有主键,则可以使用该主键,但仍可以使用
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
               );

你犯了什么错误?除此之外,您的查询看起来不像insert语句。它看起来像是一个选择。@Serg编辑得很好。只有添加了一些换行符和缩进,才会有所帮助。没有这一点,第一个修订版更具可读性。如果不止一行具有相同的EngagementId、ServiceLine和SubServiceLine值,您将从哪一行获取其他列的值?您好,如果您希望所有与列相关的列都不同,您必须设置逻辑,以便在相同的行之间进行选择。如果在EngagementId、ServiceLine和SubServiceLine中有两行值相同,您将选择什么?你有一个日期栏,这样你可以选择最新的吗?你能帮我理解这个查询吗?简短的描述会很有帮助。@KunalSinha。你不明白什么
row_number()
有很好的文档记录。我理解的第一个查询是关于第二个查询,您提到在哪里使用pk。@KunalSinha。主键唯一地标识每一行。谢谢@Gordon,你能在这方面帮助我吗-