2012-09-18 47 views
2

假设我有两个结构相同的表,请将它们称为A & B.此问题唯一关注的列是product_type,price和volume。识别两个表中的不同行

product_type &价格的每个组合可能在每个表中以不同的体积重复多次。我试图找到在一张表中有不同总数的组合的实例。

这将包括何时来自表A的组合未在表B中表示,反之亦然。

===================

实施例:

表A:

ID Product_type Price Volume 
--- ------------ ----- ------ 
1   X   $1  10 
2   X   $1  11 
3   Z   $2  10 

表B:

ID Product_type Price Volume 
-- ------------- ----- ------- 
1   X   $1  21 
2   Y   $1  5 
3   Z   $2  7 
4   Z   $2  4 

请注意,表A中X @ $ 1的数量总和为21,与表B相匹配。 表B中存在Y @ $ 1,但A 012中不存在Z @ $ 2在两个表格中都存在,但它们的总和不同。我希望查询返回违反规则的每个product_type和price组合(即Y @ $ 1和Z @ $ 2)。

我试过使用GROUP,UNION,DISTINCT,子查询和上面的各种组合,但似乎无法弄清楚。

回答

0
create table a (ID integer, Product_type char(1), Price float, Volume integer); 
create table b (ID integer, Product_type char(1), Price float, Volume integer); 

insert into a (ID, Product_type, Price, Volume) values 
(1, 'X', 1, 10), 
(2, 'X', 1, 11), 
(3, 'Z', 2, 10) 
; 
insert into b (ID, Product_type, Price, Volume) values 
(1, 'X', 1, 21), 
(2, 'Y', 1, 5), 
(3, 'Z', 2, 7), 
(4, 'Z', 2, 4) 
; 

select 
    a.Product_type as Product_type_a, 
    a.Price as Price_a, 
    a.Volume as Volume_a, 
    b.Product_type as Product_type_b, 
    b.Price as Price_b, 
    b.Volume as Volume_b 
from (
     select Product_type, Price, sum(Volume) as Volume 
     from a 
     group by Product_type, Price 
    ) a 
    full outer join (
     select Product_type, Price, sum(Volume) as Volume 
     from b 
     group by Product_type, Price 
    ) b on a.Product_type = b.Product_type and a.Price = b.Price 
where 
    a.Volume != b.Volume 
    or a.Volume is null or b.Volume is null 
; 
product_type_a | price_a | volume_a | product_type_b | price_b | volume_b 
----------------+---------+----------+----------------+---------+---------- 
Z    |  2 |  10 | Z    |  2 |  11 
       |   |   | Y    |  1 |  5 
+0

感谢。这很好,只需要修改它以联合两个连接,因为MySQL不支持完整的外连接。 – eb51

+0

@ user1681248不知道。我在postgresql中测试了它,因为这是我现在可以访问的。 –

0

这似乎work for me

SELECT a.Product_type, a.volume, b.Product_type, b.volume 
FROM 
    (SELECT Product_type, SUM(volume) AS volume 
    FROM tbl1 GROUP BY Product_type) a 
INNER JOIN 
    (SELECT Product_type, SUM(volume) AS volume 
    FROM tbl2 GROUP BY Product_type) b ON b.Product_type = a.Product_type 
WHERE a.volume <> b.volume 

结果

| PRODUCT_TYPE | VOLUME | PRODUCT_TYPE | VOLUME | 
------------------------------------------------- 
|   Z |  10 |   Z |  11 |
1

我相信以下是你所期待的。我为奇怪的地方/在语法上抱歉,在concat中的子查询这似乎是最可读的方法。

(
SELECT 
    "TableA", 
    TA.* 

FROM TableA AS TA 

WHERE CONCAT(product_type, price, 
      (SELECT SUM(volume) FROM TableA WHERE product_type = TA.product_type AND price = TA.price)) 

NOT IN (SELECT CONCAT(product_type, price, SUM(volume)) FROM TableB GROUP BY product_type, price) 
) 

UNION 

(
SELECT 
    "TableB", 
    TB.* 

FROM TableB AS TB 

WHERE CONCAT(product_type, price, 
      (SELECT SUM(volume) FROM TableB WHERE product_type = TB.product_type AND price = TB.price)) 

NOT IN (SELECT CONCAT(product_type, price, SUM(volume)) FROM TableA GROUP BY product_type, price) 
) 

#ORDER BY <column> 

输出:

TableA ID Product_type Price Volume 
TableA 3 Z    $2  10 
TableB 2 Y    $1  5 
TableB 3 Z    $2  7 
TableB 4 Z    $2  4