SQL Server 多表聯集查詢取最新一條資料

來源:互聯網
上載者:User

所有的SQL語句如下:

create database David;use David--雨量站表create table RainSite(   ID int not null primary key identity(1,1),   SiteCode varchar(8) not null,   SiteName varchar(20) not null,   Latitute decimal(10,6) not null,   Longitute decimal(10,6) not null)--插入雨量站測試資料insert into RainSite values('34242580','AA',117.377656,29.990671);insert into RainSite values('34242581','BB',117.387761,29.924643);insert into RainSite values('34242582','CC',117.397844,29.935675);insert into RainSite values('34242583','DD',117.437625,29.950649);--水位站create table WaterSite(   ID int not null primary key identity(1,1),   SiteCode varchar(8) not null,   SiteName varchar(20) not null,   Latitute decimal(10,6) not null,   Longitute decimal(10,6) not null)--插入水位站測試資料insert into WaterSite values('34242584','EE',117.477656,29.690671);insert into WaterSite values('34242585','FF',117.587761,29.324643);insert into WaterSite values('34242586','GG',117.697844,29.835675);insert into WaterSite values('34242587','HH',117.737625,29.550649);--網站類型表create table SiteType(    ID int not null primary key identity(1,1),    SiteTypeName varchar(20))--插入網站類型資料insert into SiteType values('水位');insert into SiteType values('雨量');--預警記錄表create table WarningForcast(   ID int not null primary key identity(1,1),   SiteTypeID int not null,   SiteCode varchar(8) not null,   SiteName varchar(20) not null,   ForecastTime datetime not null,   ForecastContent text)--插入預警記錄資料insert into WarningForcast values(1,'34242581','BB','2012-06-05 12:20:25','發生險情,請相關單位注意!');insert into WarningForcast values(1,'34242581','BB','2012-06-06 15:50:25','發生險情,請相關單位注意!');insert into WarningForcast values(2,'34242586','GG','2012-06-05 12:30:25','發生險情,請相關單位注意!');insert into WarningForcast values(2,'34242586','GG','2012-06-05 18:40:25','發生險情,請相關單位注意!');insert into WarningForcast values(2,'34242587','HH','2012-06-05 19:20:25','發生險情,請相關單位注意!');insert into WarningForcast values(1,'34242583','DD','2012-06-05 15:50:25','發生險情,請相關單位注意!');--需求,要取出預警記錄表中的三天內不同網站的最新一條記錄及對應的水位或雨量網站的經度和緯度資訊select t.ID,t.SiteTypeID,t.SiteCode,t.SiteName,t.ForecastTime,t.ForecastContent,t1.SiteCode,t1.SiteName,t1.Latitute,t1.Longitutefrom WarningForcast t,(   select * from RainSite   union   select * from WaterSite) t1where t.SiteCode=t1.SiteCode and ForecastTime in (   select MAX(ForecastTime) from WarningForcast where SiteCode=t.SiteCode)and ForecastTime between DATEADD(DAY,-3,GETDATE()) and GETDATE()order by t.ForecastTime desc;--水位站表select * from WaterSite;--雨量站表select * from RainSite;

實際效果如下:


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.