如何修改此SQL查询以获取位于另一个表中的信息?

时间:2015-07-11 17:43:52

标签: sql-server join

我正在使用SQL Server 2014,我有一个简单的查询,它使用3个表格给我以下与客人预订相关的输出:

查询如下所示:

USE MyDatabase

SELECT a.ResID, a.Name, a.Property, c.ArrivalDate,c.DepartureDate,c.ConfNum, 
d.CHAR07, c.StatusCode,c.CreatedOn, c.UpdatedBy, c.CreatedBy
FROM GuestStaySummary a
     left join GuestNameInfo b on b.ResID = a.ResID
     left join reservationstay c on c. ResID = a. ResID 
WHERE a.PrimaryGuest = '+'

输出如下:

ResID      Property  Name      ArrivalDate   DepartureDate  ConfNum    StatusCode  CreatedOn    UpdatedBy  CreatedBy 
125         ABC      Jones      2015-08-20    2015-08-27    659874      CONFIRMED   2015-03-10     AP01      AP01

由于系统上的应用程序升级,所有预订都被取消并重新创建。因此,上述条目如下:

ResID      Property  Name      ArrivalDate   DepartureDate  ConfNum   StatusCode   CreatedOn    UpdatedBy  CreatedBy 
125         ABC      Jones      2015-08-20    2015-08-27    659874     CANCELED    2015-03-10       R5      AP01
358         ABC      Jones      2015-08-20    2015-08-27    742651     CONFIRMED   2015-05-15       R5      AP01

原始ResID 125现在有StatusCode为“已取消”,并且已使用新的ResId和新ConfNum重新创建。

CreatedOn 125(现为Res ID 358)的原始ResId(即2015-03-10)对于我的分析目的非常重要。换句话说,我需要将ResID 358的CreatedOn与2015-03-10相关联,而不是2015-05-15的新CreatedOn

链接这两个条目的唯一方法是使用“原始”ConfNum,该P5RESERVATIONSTAY已存储在名为CHAR07的列中名为USE MyDatabase SELECT a.ResID, a.Name, a.Property, c.ArrivalDate,c.DepartureDate,c.ConfNum, d.CHAR07 AS ‘Original ConfNum’, c.StatusCode,c.CreatedOn, c.UpdatedBy, c.CreatedBy FROM GuestStaySummary a left join GuestNameInfo b on b.ResID = a.ResID left join reservationstay c on c. ResID = a. ResID left join P5RESERVATIONSTAY d on d.RSY_RESERVATIONSTAYID = a.ResID left join P5RESERVATIONSTAY d2 on d2.CHAR07 = c.ConfNum WHERE b.PrimaryGuest = '+' 的表中。

我已按如下方式修改了我的查询,以检索所有“已取消”预订的清单及其等效的“新”预订:

ResID      Property   Name      ArrDate      DepDate      ConfNum     Original ConfNum      StatusCode     CreatedOn    UpdatedBy  CreatedBy 
125         ABC      Jones      2015-08-20    2015-08-27    659874         NULL               CANCELED    2015-03-10       R5      AP01
358         ABC      Jones      2015-08-20    2015-08-27    742651         659874             CONFIRMED   2015-05-15       R5      AP01

输出现在如下:

ResID  Property  Name    ArrDate     DepDate      ConfNum     Original ConfNum    StatusCode     CreatedOn     OriginalCreatedOn  UpdatedBy  CreatedBy 

 358     ABC      Jones  2015-08-20    2015-08-27    742651       659874             CONFIRMED    2015-05-15       2015-03-10        R5        AP01

它完成了这项工作,但我在Excel中面临一些额外的工作,使用VLOOKUP来获取原始的CreatedOn。

我希望我的SQL查询能够提供以下输出:

   <center>
            <div id="slider">

                <image class="slider" id="shown" src="img/img1.jpg"/>
                <image class="slider" id="notShown" src="img/img3.jpg"/>
                <image class="slider" id="notShown" src="img/img2.jpg"/>

            </div>
   </center>

            <script type="text/javascript" src="js/slider.js"></script>

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

难道你真的只需要再次加入reservationstay吗?我认为内部联接将只包括正确的取消和重新创建的行,但我不是100%肯定,因为我不知道你在那里有什么其他数据。

SELECT
    gss.ResID, gss.Property, gss.Name, rs.ArrivalDate, rs.DepartureDate, rs.ConfNum,
    p5rs.CHAR07 as "Original ConfNum", rs.StatusCode, rs.CreatedOn,
    rsorg.CreatedOn as "Original CreatedOn", rs.UpdatedBy, rs.CreatedBy
FROM
    GuestStaySummary as gss
    inner join GuestNameInfo     as gni   on gni.ResID = gss.ResID
    inner join reservationstay   as rs    on rs.ResID = gss.ResID 
    inner join P5RESERVATIONSTAY as p5rs  on p5rs.RSY_RESERVATIONSTAYID = gss.ResID 
    inner join reservationstay   as rsorg on rsorg.ConfNum = p5rs.CHAR07
WHERE
    gni.PrimaryGuest = '+'