【问题标题】:SQL finding part of common and differences under some conditionsSQL在某些条件下查找部分共性和差异
【发布时间】:2014-04-28 11:32:01
【问题描述】:

首先我将描述整个想法。我有一个系统正在存储有关Projects 的信息。每个项目都有很多Topics。每个项目每个月都会进行修订。比如:

2014-01-01 Was added Project '1' in relation of 40 Topics
2014-01-01 Was added Project '2' in relation of 120 Topics
2014-02-01 Was added Project '1' in relation of 30 Topics
2014-02-01 Was added Project '2' in relation of 100 Topics

我想计算主题之间的差异,其中有多少是新的,有多少已经结束,有多少仍在进行中。

我已经按照试过了

创建一个查询。 表结构:

database=> \d project
                              Tabela "public.project"
    Kolumna     |   Typ   |                      Modyfikatory
----------------+---------+---------------------------------------------------------
 id             | integer | niepusty domyĹnie nextval('project_id_seq'::regclass)
 scid           | integer |
 starttime      | date    |


database=> \d topic
                              Tabela "public.topic"
    Kolumna     |   Typ   |                    Modyfikatory
----------------+---------+-----------------------------------------------------
 id             | integer | niepusty domyĹnie nextval('topic_id_seq'::regclass)
 project _id    | integer |
 topicengine_id | integer |
 parameter      | text    |
 description    | text    |
 severity       | text    |
 requirement_id | integer |
 topicparam_id  | integer |

我的第一次尝试是使用内部连接计算 on going 主题:

SELECT t1.id 
FROM   topic t1 
       INNER JOIN topic t2 
               ON t1.id = t2.id 
       INNER JOIN project p1 
               ON p1.id = t1.project_id 
       INNER JOIN project p2 
               ON p2.id = t2.project_id 
WHERE  p1.id = 1101 
       AND p2.id = 1168 

但是每次结果都是空的。谁能指出我做错了什么?

【问题讨论】:

    标签: postgresql join inner-join


    【解决方案1】:

    主题 id 似乎是主题的主键。 这意味着加入:t1.id = t2.id 将记录与自身加入,因此 t1.project_id 等于 t2.project_id 并且不能同时具有两个值。 也许您的意思是在 topicengine_id 上加入 t1 和 t2?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多