从关于人员兴趣的表中创建SQL视图

时间:2016-02-22 18:46:56

标签: sql sql-server sqlite

所以给出了下表:

ResInt (pid, topic) - Person with pid has topic as a research interest

我需要完成以下任务:

1. Write a SQL query to find all person-person-interest triplets (pid1, 
pid2, topic) such that person pid1 has topic as a research interest,
but person pid2 doesn’t. Call this the PPT view.

2. Given the earlier view PPT and the original table ResInt,
write the full SQL query to find all pairs of people with the exact
same set ofresearch interests. Remove mirror pairs and self-pairs. 

一个人可以有多个兴趣,因此表中可能有多个具有不同主题的相同PID的元组。

我的尝试看起来像这样:

CREATE VIEW PPT AS
SELECT T1.pid AS FirstPID, SELECT T2.pid as SecondPID, T1.topic
FROM ResInt AS T1, ResInt AS T2
WHERE T1.pid != T2.pid AND T1.topic != T2.topic

但这只会给我不同的元组组合,其中两个不同的人没有列出相同的兴趣。我认为这也会增加太多不属于的元组,因为会添加不同主题的每个组合,而不是那些没有共同兴趣的人。

0 个答案:

没有答案