SQL:CUSTOMER从每个ARTIST ||购买了一个WORK 2桌

时间:2014-09-01 12:28:22

标签: sql oracle oracle-sqldeveloper relational-division

所以我有2个表都包含ID

工作:

workID, artistID
1       6
2       7
3       8
4       6
5       6

trans:

CustomerID, workID
10          1 
11          2
12          3
10          4
10          5

到目前为止,我只设法将所有ID放在一起,但我需要的结果是显示从每个artistID购买了workID的任何customerID:

SELECT
     trans.customerID, trans.workID, work.artistID
FROM
    trans
      INNER JOIN work
      ON trans.workID=work.workID
WHERE
    trans.customerID IS NOT null
ORDER BY
    trans.customerID;

基本上我不知道我的下一步应该是什么,如果有人能指出我正确的方向或解释一个很棒的解决方案。

编辑:将简化数据添加到表格

1 个答案:

答案 0 :(得分:0)

这应该让你前进:

SELECT t.customerId 
FROM 
  ( SELECT t.CustomerId, COUNT(DISTINCT artistId) as artists
    FROM trans t INNER JOIN work w ON w.workid = t.workid) t 
  INNER JOIN 
  ( SELECT COUNT(DISTINCT artistId) artists FROM work) a ON  t.artists = a.artists