经常看到这样的SQL写法
select .. 
from (select ... 
        from  (select ...from 记录集合)
)
例如:
select t.*
       ,P.Fund
from
   (
       select max("Date") as "LASTDATE"
              ,Name
       from P
       group by Name
   )t
join P on t.LASTDATE=P."Date"
          and
          t.Name=P.Name
where to_char(t.LASTDATE,'mm')='07'问题:
能否说一下这种写法的思路?有什么优点?
跟(select .. from 记录集1,记录集2,记录集3 where.. )的写法差别在哪儿?往指点一二。

解决方案 »

  1.   

    哦,这样啊,我也是来学习的。
    需求:
    求P表中7月份每个NAME最大日期的记录,
    用select .. from 记录集1,记录集2,记录集3 where.. 
    的方式如何写??我想知道。
      

  2.   

    一般建议使用诸如select .. from 记录集1,记录集2,记录集3 where.. 查询
    但因为记录集1,记录集2没有满足条件,必须使用一个查询以后才能得到一个满足条件的
    结果集,此时就要用子查询如题,象snowy_howe(天下有雪) 提出的需求恐怕不能满足
    我有个带子查询的解法是:
      select * from P a
      where a."date"=(select max("date") from P where a.name=name)
    :),
    期待能有不带子查询的解法……
      

  3.   

    to snowy_howe(天下有雪):用分析函数可以容易的实现,
    select * from  
    (select P.*, dense_rank()over(partition by name order by name_date desc) as rn from P )
    where rn = 1 
     
      

  4.   

    TO xiaoxiao1984(笨猫儿^_^) 
    你还是使用了子查询
    我的意思是无法使用不带子查询的方式来解决问题。