from (select flow_no,sum(old_amount) ff from pos_t_payflow group by flow_no) t1
left join (select flow_no,sale_amount from pos_t_payflow where flow_id='1') t2
on t1.flow_no=t2.flow_no
where t1.ff<>coalesce(t2.sale_amount,-1)
你好!
先获取不重复的ID,即x、y表的不重复并集,观察x、y表结构类似,通过union进行排重即可。
使用第一步获取的数据进行左链接x、y表的扩展表(第3步)。
分别对x、y表进行分组并合计,针对图片中y表中ID为A的记录出现多次,最后显示为ID为A的盘点数量为2,这样的数据进行处理,以方便最后显示。
通过(库存数-盘点数)得到比对字段值。
SELECTt.ID,
IFNULL(t.`库存数量`, 0) `库存数量`,
IFNULL(t.`盘点数量`, 0) `盘点数量`,
(
IFNULL(t.`库存数量`, 0) - IFNULL(t.`盘点数量`, 0)
) `比对`
FROM
(
SELECT
a.ID ID,
p1.`库存数量`,
p2.`盘点数量`
FROM
(
SELECT
ID
FROM
x
UNION
SELECT
ID
FROM
y
) a
LEFT JOIN (
SELECT
ID,
sum(`库存数量`) `库存数量`
FROM
x
GROUP BY
ID
) p1 ON a.id = p1.id
LEFT JOIN (
SELECT
ID,
sum(`盘点数量`) `盘点数量`
FROM
y
GROUP BY
ID
) p2 ON a.id = p2.id
) t
ORDER BY
ID
希望对你有帮助!
selectbase.name, base.year
, a.成绩 as [a表成绩]
, b.成绩 as [b表成绩]
, case
when a.成绩 is null then 'a表中不存在'
when b.成绩 is null then 'b表中不存在'
when a.成绩=b.成绩 then '成功'
else '差异' end as 比较结果
from (
select name, year from tb_a
union
select name, year from tb_b
) as base
left join tb_a as a on a.name=base.name and a.year=base.year
left join tb_b as b on b.name=base.name and b.year=base.year
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)