Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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_Sql Server 2008 - Fatal编程技术网

Sql 如何获得表中元素的等效组合(划分为固定列集)的行频率?

Sql 如何获得表中元素的等效组合(划分为固定列集)的行频率?,sql,sql-server-2008,Sql,Sql Server 2008,如何获得表中元素的等效组合(划分为固定列集)的行频率 假设我有一个表格,表格中有行: [id1, a1, b1, c1, d1, e1, f1] [id2, a2, b2, c2, d2, e2, f2] [id3, a3, b3, c3, d3, e3, f3] ... 式中,将a-c和d-f列拆分为集合,例如 ,可能有多个(或没有)行,其中setR1=setR2(这意味着它们包含相同的元素,但顺序不一定相同),并且多个行可能具有相同的两个集合(也不一定与其他行中的等效集合顺序相同) 如何获

如何获得表中元素的等效组合(划分为固定列集)的行频率

假设我有一个表格,表格中有行:

[id1, a1, b1, c1, d1, e1, f1]
[id2, a2, b2, c2, d2, e2, f2]
[id3, a3, b3, c3, d3, e3, f3]
...
式中,将a-c和d-f列拆分为集合,例如

,可能有多个(或没有)行,其中
setR1
=
setR2
(这意味着它们包含相同的元素,但顺序不一定相同),并且多个行可能具有相同的两个集合(也不一定与其他行中的等效集合顺序相同)

如何获得每行的集合匹配频率?例如

[id1, a1,      b1,      c1,      d1(=a1), e1(=b1), f1(=c1)]
[id2, a2,      b2,      c2,      d2(=a2), e2(=b2), f2(=c2)]
[id3, a3(=a2), b3(=b2), c3(=c2), d3(=c2), e3(=b2), f3(=a2)]
[id4, a4(=c2), b4(=b2), c4(=a2), d4(=c2), e4(=b2), f4(=a2)]

gets turned into / results in table like

[a1, b1, c1, d1, e1, f1, 1]
[a2, b2, c2, d2, e2, f2, 3]
谢谢。

使用一对夫妇,并取消数据锁定:

;with cte as ( /* unpivot */
  select t.*, x, y
  from t
    cross apply (values (a,d),(b,e),(c,f)) v (x,y)
)
, match as ( /* set matching */
  select l.id as lid, r.id as rid, l.a, l.b, l.c, l.d, l.e, l.f
  from cte l
    left join cte r
      on l.x = r.y
  group by l.id, r.id, l.a, l.b, l.c, l.d, l.e, l.f
  having count(*) = 3 /* 3 is the number of elements in the sets */
)
/* return first instance of a set's order along with match count */ 
select a, b, c, d, e, f, count(rid) as [match_count]
from match m
where not exists (
  select 1
  from match i
  where i.lid = m.lid
    and i.rid < i.lid
  )
group by a, b, c, d, e, f
从该测试设置:

create table t (id int, a char(2), b char(2), c char(2), d char(2), e char(2), f char(2))
insert into t values 
 (1,'a1','b1','c1','a1','b1','c1')
,(2,'a2','b2','c2','a2','b2','c2')
,(3,'a2','b2','c2','c2','b2','a2')
,(4,'c2','b2','a2','c2','b2','a2')
,(5,'a5','b5','c5','d5','e5','f5');
使用一对和取消填充数据:

;with cte as ( /* unpivot */
  select t.*, x, y
  from t
    cross apply (values (a,d),(b,e),(c,f)) v (x,y)
)
, match as ( /* set matching */
  select l.id as lid, r.id as rid, l.a, l.b, l.c, l.d, l.e, l.f
  from cte l
    left join cte r
      on l.x = r.y
  group by l.id, r.id, l.a, l.b, l.c, l.d, l.e, l.f
  having count(*) = 3 /* 3 is the number of elements in the sets */
)
/* return first instance of a set's order along with match count */ 
select a, b, c, d, e, f, count(rid) as [match_count]
from match m
where not exists (
  select 1
  from match i
  where i.lid = m.lid
    and i.rid < i.lid
  )
group by a, b, c, d, e, f
从该测试设置:

create table t (id int, a char(2), b char(2), c char(2), d char(2), e char(2), f char(2))
insert into t values 
 (1,'a1','b1','c1','a1','b1','c1')
,(2,'a2','b2','c2','a2','b2','c2')
,(3,'a2','b2','c2','c2','b2','a2')
,(4,'c2','b2','a2','c2','b2','a2')
,(5,'a5','b5','c5','d5','e5','f5');