【问题标题】:SQL join one time onlySQL join 仅一次
【发布时间】:2020-07-02 07:58:45
【问题描述】:

我有以下情况

表 1

Id          Sum
1           100
2           100
3           200
4           400

表 2

ID          Sum
5           100
6           200
7           300
8           400

我想编写一个在两个表之间连接的查询,并只显示一次匹配。结果应该是

1     100     Match
2     100     Mismatch 
3     200     Match
4     300     Mismatch 
5     400     Match

我正在使用这个查询

select a.id, a.sum,b.id,b.sum,
CASE 
    when a.sum = b.sum then 'Match' 
    else 'Mismatch' 
end as Result 
from table1 a  LEFT OUTER join table2 b on a.sum = b.sum

结果是

    1     100    5     100     Match
    2     100    5     100     Match
    3     200    6     200     Match
    4     400    8     400     Match

基本上,这是一个收集系统。系统自动将数据保存在表 2 中。当员工进行实际收集时,他们会上传表 1 中的数据。现在我想显示一个报告,显示基于总和的收集之间的差异。因此,如果 Employee 插入 100$ 2 次,我应该表明系统只捕获了一次,并且表 1 中有 1 个额外的行

【问题讨论】:

  • 请提供您正在执行的查询。
  • select * From Table1 A left outer join Table2 B on A.Sum=B.Sum 这样的结果是100匹配2次,因为值存在于第二个表中。
  • 如果id=1 与第二个表中的id=5 具有相同的总和而匹配,那么为什么id=2 不匹配?修改您的要求
  • 请不要要求我们编写您的代码。展示你能做什么 & 说明你被卡住了。通过编辑而不是 cmets 进行澄清。请在代码问题中给出minimal reproducible example--cut & paste & runnable code,包括最小的代表性示例输入为代码;期望和实际输出(包括逐字错误消息);标签和版本;明确的规范和解释。对于包含 DBMS 和 DDL(包括约束和索引)和输入为格式化为表的代码的 SQL。 How to Ask。使用足够多的单词、句子和对部分示例的引用来清楚完整地表达你的意思。
  • 解释你进行比较的逻辑.. 100 - 匹配/不匹配令人困惑

标签: sql join select window-functions


【解决方案1】:

你想加入id吗?

select 
    t1.id, 
    t1.sum, 
    case when t1.sum = t2.sum then 'Match' else 'Mismatch' end as result
from table1 t1
inner join table2 t2 on t2.id = t1.id

或者,如果ids 不会数学,您可能想使用row_number()

select 
    t1.id id1, 
    t1.sum sum1, 
    t2.id id2,
    t2.sum sum2,
    case when t1.sum = t2.sum then 'Match' else 'Mismatch' end as result
from (select t1.*, row_number() over(order by id) rn from table1 t1) t1
inner join ((select t2.*, row_number() over(order by id) rn from table2 t2) t2 
    on t2.rn = t1.rn

最后:如果两个表中的行数可能不同,请使用full join 而不是inner join(如果您的数据库支持的话):

select 
    t1.id id1, 
    t1.sum sum1, 
    t2.id id2,
    t2.sum sum2,
    case when t1.sum = t2.sum then 'Match' else 'Mismatch' end as result
from (select t1.*, row_number() over(order by id) rn from table1 t1) t1
full join (select t2.*, row_number() over(order by id) rn from table2 t2) t2 
    on t2.rn = t1.rn

【讨论】:

  • 现在它正在工作。查询中有一个小错误。 select t1.id id1, t1.sum sum1, t2.id id2, t2.sum sum2, case when t1.sum = t2.sum then 'Match' else 'Mismatch' end as result from (select t1.*, row_number( ) over(order by id) rn from table1 t1) t1 full join (select t2.*, row_number() over(order by id) rn from table2 t2) t2 on t2.rn = t1.rn
  • @ahmed:确实,已修复。
【解决方案2】:

因为您想突出显示两个表之间的差异。您应该按总和进行分组并比较总和的连续性。使用完全外连接,因为您想显示双方的差异,如下所示:

select *,
CASE 
when a.cnt = b.cnt then 'Match' 
else 'Mismatch' 
end as Result from
(select sum,count(1) as cnt from table1 group by sum) a
full join
(select sum,count(1) as cnt from table2 group by sum) b
on a.sum=b.sum

sql Fiddle demo

输出如下:

【讨论】:

    【解决方案3】:

    试试这样的

    select t1.id, t1.sum,
        CASE 
            when t1.sum = t2.sum then 'Match' 
            else 'Mismatch' 
        end
        as Compare
        from table1 t1 
        inner join table2 t2 on t1.id = t2.id
    

    【讨论】:

    • ID 不匹配。我无法按 ID 加入
    • 那你想加入什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    相关资源
    最近更新 更多