如何比较表的多列和多行

时间:2019-06-25 09:36:28

标签: sql postgresql

我有一个看起来像这样的大数据表

ID    Marker    Value1    Value2
================================
1     A         10        11
1     B         12        13
1     C         14        15
2     A         10        11
2     B         13        12
2     C
3     A         10        11
3     C         12        13

我想通过以下数据搜索此数据,这些数据是用户输入的,而不是存储在表中:

Marker    Value1    Value2
==========================
A         10        11
B         12        13
C         14        14

结果应该是这样的:

ID    Marker    Value1    Value2    Match?
==========================================
1     A         10        11        true
1     B         12        13        true
1     C         14        15        false
2     A         10        11        true
2     B         13        12        true
2     C                             false  
3     A         10        11        true
3     C         12        13        false

最终,这是必要的(上表不是必需的,它应该说明这些值是如何产生的):

ID    Matches    Percent
========================
1     2          66%
2     2          66%
3     1          33%

我正在寻找最有前途的方法来使它在SQL中工作(确切地说是PostgreSQL)。

我的想法:

  1. 创建一个临时表,将其与上面的表连接并将结果分组
  2. 使用CASE WHEN或临时过程仅使用一个(可能是ated肿的)查询

我对这两种方法都不满意,因此是一个问题。如何有效地比较两个这样的表?

3 个答案:

答案 0 :(得分:1)

您可以使用CTE预先计算匹配项。然后,简单的聚合即可解决问题。假设您的参数是:

Marker    Value1    Value2
==========================
m1        x1        y1
m2        x2        y2
m3        x3        y3

您可以这样做:

with x as (
  select
    id,
    case when 
       marker = :m1 and (value1 = :x1 and value2 = :y1 or value1 = :y1 and value2 = :x1)
    or marker = :m2 and (value1 = :x2 and value2 = :y2 or value1 = :y2 and value2 = :x2)
    or marker = :m3 and (value1 = :x3 and value2 = :y3 or value1 = :y3 and value2 = :x3)
    then 1 else 0 end as matches
  from t
)
select
  id,
  sum(matches) as matches,
  100.0 * sum(matches) / count(*) as percent
from x
group by id

答案 1 :(得分:1)

尝试一下:

CREATE TABLE #Temp
(
    Marker nvarchar(50),
    Value1 nvarchar(50),
    Value2 nvarchar(50)
)

INSERT INTO #Temp Values ('A', '10', '11')
INSERT INTO #Temp Values ('B', '12', '13')
INSERT INTO #Temp Values ('C', '14', '14')


SELECT m.Id, m.Marker, m.Value1, m.Value2, 
  (Select 
    CASE
        WHEN COUNT(*) = 0 THEN 'False'
        WHEN COUNT(*) <> 0 THEN 'True'
    END
    FROM #Temp t 
    WHERE t.Marker = m.Marker and t.Value1 = m.Value1 and t.Value2 = m.Value2) as Matches

FROM [Test].[dbo].[Markers] m
ORDER BY Matches DESC

Drop TABLE #Temp

enter image description here

如果这正是您想要的,我会尝试解决它的第二部分。

答案 2 :(得分:1)

可以使用公用表表达式中的VALUES子句提供用户输入,然后可以在与实际表的左联接中使用该用户输入。

with user_input (marker, value1, value2) as (
  values 
    ('A', 10, 11),
    ('B', 12, 13),
    ('C', 14, 14)
)
select d.id,
       count(*) filter (where (d.marker, d.value1, d.value2) is not distinct from (u.marker, u.value1, u.value2)), 
       100 * count(*) filter (where (d.marker, d.value1, d.value2) is not distinct from (u.marker, u.value1, u.value2)) / cast(count(*) as numeric) as pct
from data d
  left join user_input u on (d.marker, d.value1, d.value2) = (u.marker, u.value1, u.value2)
group by d.id
order by d.id;  

返回:

id | count | pct  
---+-------+------
 1 |     2 | 66.67
 2 |     2 | 66.67
 3 |     1 | 50.00

在线示例:https://rextester.com/OBOOD9042

修改

如果值的顺序无关紧要(因此(12,13)被认为与(13,12)相同,则比较会变得更加复杂。

with user_input (marker, value1, value2) as (
  values 
    ('A', 10, 11),
    ('B', 12, 13),
    ('C', 14, 14)
)
select d.id,
       count(*) filter (where (d.marker, least(d.value1, d.value2), greatest(d.value1, d.value2)) is not distinct from (u.marker, least(u.value1, u.value2), greatest(u.value1, u.value2)))
from data d
  left join user_input u on (d.marker, least(d.value1, d.value2), greatest(d.value1, d.value2)) = (u.marker, least(u.value1, u.value2), greatest(u.value1, u.value2))
group by d.id
order by d.id;