SQL - Relationship between a SubQuery and an Outer Table

前端 未结 3 567
小蘑菇
小蘑菇 2021-01-08 00:55

Problem

I need to better understand the rules about when I can reference an outer table in a subquery and when (and why) that is an inappropriate re

3条回答
  •  情书的邮戳
    2021-01-08 01:42

    How about the following query:

    SELECT t1.* FROM 
    (
      SELECT * 
      FROM 
      (
        SELECT t2.id,
        RANK() OVER (PARTITION BY t2.id, t2.date ORDER BY t2.date DESC) AS R  
        FROM table2 t2
      )
      WHERE R = 1
    ) sub 
    INNER JOIN table1 t1 
    ON t1.id = sub.id
    

提交回复
热议问题