我正在尝试i,j从表中的每个元素中提取所有对对,而该表是同一张表中的每个元素,这是我的查询:
i,j
select a.Id L,b.id R into #cross from MyTable a cross join mytable b
我处于i,j == j,i只需要一半记录的情况。我的天真尝试是:
i,j == j,i
select a.Id L,b.id R into #cross from MyTable a cross join mytable b where not exists (select * from #cross c where c.L=R and c.R=L)
但我无法在插入时查询目标表,如SQL Server所述:
The SELECT INTO statement cannot have same source and destination tables
我怎样才能有效地做到呢?
编辑 仅供参考,我说:“我需要一半的记录”,这是错误的,在服用后账户记录计数i,j == j,i是n*(n+1)/2
n*(n+1)/2
因此,只需对连接进行条件调整,以使左侧始终等于或小于左侧!
select a.Id L,b.id R into #cross from MyTable a inner join mytable b on a.id <= b.id