SQL - 跨列比较属性

时间:2014-07-15 19:33:04

标签: sql postgresql

我有以下表格

customers(custid, name)
orders(orderid, custid)
itemsordered(itemid, orderid)
items(itemid, description)

我的目标是每对客户订购具有相同描述的商品,检索两个客户的名称。消除重复,不要将客户与自己配对,只包括每一对。对于每对,请按字母顺序返回对中的名称。

我理解我需要引用两个custid并将它们的每个描述相互比较,例如:

select nameA
from customers nameA
join orders using (custid)
join itemsordered using (orderid)
join item using (itemid)
where (select nameB
from customers
from customers nameA
join orders using (custid)
join itemsordered using (orderid)
join item using (itemid)
where descriptionA = descriptionB

etc.

但我不确定如何继续。这是正确的答案:

Christina|Janine
Christina|Max
Christina|Teddy
Christina|David
Christina|Rachel
Rachel|Teddy
David|Janine
David|Rachel
David|Teddy
Janine|Rachel
Janine|Teddy
Janine|Max
Max|Teddy

我需要一些新的例子。我的大多数猜测都包含以下子查询:

select attribute from table
join anotherTable using (somekey)
where table in (select anotherAttribute 
from anotherTable
where....etc.

任何建议或指示都将不胜感激。

更新:名称是唯一的。

2 个答案:

答案 0 :(得分:1)

WITH t AS (
    SELECT DISTINCT c.name, i.description  -- collapse dupes per name
    FROM   customers    c
    JOIN   orders         USING (custid)
    JOIN   itemsordered   USING (orderid)
    JOIN   items        i USING (itemid)
    )
SELECT DISTINCT t.name, t1.name
FROM   t
JOIN   t t1 USING (description)
WHERE  t.name < t1.name    -- rules out self-joins and duplicate pairs
ORDER  BY 1, 2;

这与@Clodoaldo发布的内容相似,只是有几个重要的区别。

  • 假设名称独特。否则结果就没有意义了。更简单的外SELECT
    如果名称不唯一,则需要输出custid(另外)。

  • 根据问题匹配description

  • 立即折叠每位客户的重复说明。只有少数欺骗,这无济于事。通过不止一些,它可以提高性能。

答案 1 :(得分:0)

with s as (
    select *
    from
        customers
        inner join
        orders using (custid)
        inner join
        itemsordered using (orderid)
        inner join
        items using (itemid)
)
select distinct
    least(s1.name, s2.name),
    greatest(s1.name, s2.name)
from
    s s1
    inner join
    s s2 on
        s1.description = s2.description
        and
        s1.custid < s2.custid
order by 1, 2
相关问题