【问题标题】:Postgresql: How do I join same table with where conditionPostgresql:如何使用where条件加入同一张表
【发布时间】:2021-11-14 05:36:58
【问题描述】:

我有一张桌子statistic。该表具有step_index,例如1234。我需要让查询添加week2total_request2 其中星期是37 并加入具有相同step_index 的self 表。

例子:

step_index|type    |year|week|total_request|
----------+--------+----+----+-------------+
         1|Blasting|2021|  38|            1|
         2|Blasting|2021|  38|            1|
         3|Blasting|2021|  38|            1|
         4|Blasting|2021|  38|            1|
         1|Blasting|2021|  37|            6|
         2|Blasting|2021|  37|            6|
         3|Blasting|2021|  37|            6|
         4|Blasting|2021|  37|            6|

结果应该是:

step_index|type    |year|week|total_request|week2|total_request2|
----------+--------+----+----+-------------+-----+--------------+
         1|Blasting|2021|  38|            1|   37|             6|
         2|Blasting|2021|  38|            1|   37|             6|
         3|Blasting|2021|  38|            1|   37|             6|
         4|Blasting|2021|  38|            1|   37|             6|

我尝试了内部连接和 with 子句,但没有结果 week2total_request2

【问题讨论】:

  • 如果有第三周,第 39 周,那么你期望 week3 和 total_request3 ?
  • 不,我期待第 38 周和第 37 周

标签: sql postgresql join


【解决方案1】:

您可以通过显式 JOIN 来执行此操作。正确的语法是:

select s.step_index, s.type, s.year, s.week, s.total_request,
       s2.week as week2, s2.total_request as total_request2
from statistic s join
     statistic s2
     on s2.year = s.year and
        s2.step_index = s.step_index and
        s2.week = 37 and
        s.week = 38

【讨论】:

    【解决方案2】:
     WITH CTE(STEP_INDEX,TYPE,YEAR,WEEK,TOTAL_REQUEST) AS
      (
        SELECT 1,'BLASTING',2021,38,1  UNION ALL 
        SELECT 2,'BLASTING',2021,38,1  UNION ALL 
        SELECT 3,'BLASTING',2021,38,1  UNION ALL 
        SELECT 4,'BLASTING',2021,38,1  UNION ALL 
        SELECT 1,'BLASTING',2021,37,6  UNION ALL 
        SELECT 2,'BLASTING',2021,37,6  UNION ALL
        SELECT 3,'BLASTING',2021,37,6  UNION ALL
        SELECT 4,'BLASTING',2021,37,6  
      )
    
     SELECT C.STEP_INDEX,C.TYPE,C.YEAR,C.WEEK,C.TOTAL_REQUEST,
       X.WEEK AS WEEK_2,X.TOTAL_REQUEST AS TOTAL_REQUEST_2
      FROM CTE AS C
     LEFT JOIN LATERAL
      (
         SELECT C2.STEP_INDEX,C2.TYPE,C2.YEAR,C2.WEEK,C2.TOTAL_REQUEST
         FROM CTE AS C2
          WHERE C.STEP_INDEX=C2.STEP_INDEX AND C.WEEK-1=C2.WEEK AND C.YEAR=C2.YEAR
      )X ON TRUE
      WHERE C.YEAR=2021 AND C.WEEK=38
    

    根据您的样本数据

    【讨论】:

      【解决方案3】:

      根据您提供的内容,这是我对您想要的内容的最佳猜测。 如果您可以澄清主键以及有关您要查找的内容的一些更具体的信息,我可以修改我的查询以使其更好。

      select s.step_index
      ,s.type
      ,s.year
      ,s.week
      ,s.total_request
      ,s2.week
      ,s2.total_request2
      from statistic s
      ,statistic s2
      where s2.week = 37
      and s2.step_index = s.step_index
      and s.week = 38
      

      【讨论】:

        猜你喜欢
        • 2014-09-15
        • 2021-07-19
        • 1970-01-01
        • 2020-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多