求一个Sql语句,谢谢~
一个发布表,基本字段如下:
PubGuid SiteID Period * *
**** 4 200701
**** 4 200702
**** 4 200703
**** 6 200605
**** 6 200606
**** 12 200702
**** 12 200703
**** 12 200704
想写一个语句,取得每个SiteID下Period最大的那个PubGuid。
------解决方案--------------------select max(PubGuid), SiteID from table group by SiteID
------解决方案--------------------Select a.PubGuid from Table a,(Select SiteID,max(Period) from Table group by SiteID) b where a.SiteID=b.SiteID and a.Period=b.Period
------解决方案--------------------SELECT A.PubGuid ,B.SiteID,B.Period FROM T A
LEFT JOIN
(
SELECT SiteID,MAX(Period) FROM T
)B ON A.SiteID=B.SiteID AND A.Period=B.Period
------解决方案--------------------Select a.PubGuid from Table a,(Select SiteID,max(Period) from Table where Period <=200703 group by SiteID) b where a.SiteID=b.SiteID and a.Period=b.Period
------解决方案--------------------Select a.PubGuid from Table a,(Select SiteID,max(Period) as Period from Table where Period <=200703 group by SiteID) b where a.SiteID=b.SiteID and a.Period=b.Period