[原创]SQL Server2005新增SQL学习笔记

  1. 1.CROSS APPLY :
    可以理解成INNER JOIN
    适用:
        *  表A 存储的是客户信息,每个客户ID一条记录;表B存储的是发布记录,每个客户ID对应多条记录,
    需要取出每个客户最新的一条的发布信息记录
        * 表A存储多客户发布的多条信息,想取得每个客户最新发布的单条信息
        *
    代码:
    select
    distinct info.UserID,id.DisplayDate
    from view_InformationSimple as info
    CROSS apply(select top(1)DisplayDate,UserId from view_Information as i where i.UserId=info.UserId order by DisplayDate DESC)as idCROSS APPLY :可以理解成INNER JOIN 例如:select distinct info.UserID,id.DisplayDate from view_InformationSimple as info
    CROSS apply(select top(1)DisplayDate,UserId from view_Information as i where i.UserId=info.UserId order by DisplayDate DESC)as id 取得唯一用户的最新发布的信息
  2.  
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏