比如有一个表,字段名称:
A B C D E F G H
1 1 2 3 1 1 1 2
2 1 2 1 1 2 2 1
3 3 3 3 1 2 2 1
。……。…。…。………
比如有另一行数据:
A B C D E F G H
1 1 2 1 2 1 1 2那么我要查询表里有几个对应字段的值和它相同的,
然后把对应字段值相同数超过3个的过滤出来。在我给的数据里应该查出来的是前两行数据。
如何实现呢?
A B C D E F G H
1 1 2 3 1 1 1 2
2 1 2 1 1 2 2 1
3 3 3 3 1 2 2 1
。……。…。…。………
比如有另一行数据:
A B C D E F G H
1 1 2 1 2 1 1 2那么我要查询表里有几个对应字段的值和它相同的,
然后把对应字段值相同数超过3个的过滤出来。在我给的数据里应该查出来的是前两行数据。
如何实现呢?
一列重复就选出。表1:
A B
1 1
2 1
3 3表2:
A B
1 1with tb as
(
select 表1.*,
case when 表1.A=表2.A
then 1
else 0
end as isA,
case when 表1.B=表2.B
then 1
else 0
end as isB
from 表1
cross join 表2
)
select * from tb
where isA+isB>=1
--11212112
with tb(A,B,C,D,E,F,G,H)as(
select 1,1,2,3,1,1,1,2 union
select 1,1,2,1,1,2,2,1 union
select 3,3,3,3,1,2,2,1 union
select 1,1,2,1,1,3,3,1)
select
case when (COUNT(case when a=1 then 1 else null end))>=3 then 1 else 0 end+
case when (COUNT(case when b=1 then 1 else null end))>=3 then 1 else 0 end+
case when (COUNT(case when c=2 then 1 else null end))>=3 then 1 else 0 end+
case when (COUNT(case when d=1 then 1 else null end))>=3 then 1 else 0 end+
case when (COUNT(case when e=2 then 1 else null end))>=3 then 1 else 0 end+
case when (COUNT(case when f=1 then 1 else null end))>=3 then 1 else 0 end+
case when (COUNT(case when g=1 then 1 else null end))>=3 then 1 else 0 end+
case when (COUNT(case when h=2 then 1 else null end))>=3 then 1 else 0 end
from tb