简体   繁体   English

如何根据这种特定场景从主表中提取数据?

[英]How do I extract data from a main table based on this specific scenario?

I am using SQL Server 2014 .我正在使用SQL Server 2014 I have a table (T1) which contains the list of IDs of cancelled bookings and their equivalent re-bookings.我有一个表(T1),其中包含取消预订的 ID 列表及其等效的重新预订。

Extract of table T1 :T1的摘录:

CancelledID       Re-bookingID
  301                754
  387                801
  400                900
  ...

Each CancelledID has a unique equivalent in the Re-bookingID column.每个 CancelledID 在 Re-bookingID 列中都有一个唯一的等价物。

I have another table ( T2 ) which contains the list of ALL BookingIDs with additional information related to each ID.我有另一个表( T2 ),其中包含所有 BookingID 的列表以及与每个 ID 相关的附加信息。 An extract of this table is shown below:该表的摘录如下所示:

BookingID     MonthOfStay      RoomNights
...
301             2019-03-01        10
387             2019-04-01         7
400             2019-03-01         5
754             2019-08-01        10
801             2019-09-01         3
900             2019-07-01         5
900             2019-08-01         4
...   

I need a t-sql query which will me the following output:我需要一个 t-sql 查询,它将获得以下 output:

  BookingID       Cancelled_MonthOfStay     Re-booking_MonthOfStay     RoomNights
    301                2019-03-01                                           10
    387                2019-04-01                                            7
    400                2019-03-01                                            5
    754                                           2019-08-01                10
    801                                           2019-09-01                 3
    900                                           2019-07-01                 5
    900                                           2019-08-01                 4

As you can see, a re-booking can span over 2 months with additional room nights.如您所见,重新预订可能会持续超过 2 个月,并增加房晚。

I am thinking about "Joins" between the 2 tables but I am stuck at the logic to be used for the "Joins" (if that is the right way of tackling the problem).我正在考虑两张表之间的“联接”,但我被用于“联接”的逻辑所困(如果这是解决问题的正确方法)。

Note: The end in mind is to use this data to build a matrix with Cancelled_MonthOfStay as rows and Re-booking_MonthOfStay as columns.注意:最终要使用此数据构建一个矩阵,其中 Cancelled_MonthOfStay 作为行,Re-booking_MonthOfStay 作为列。 The value inside this matrix will be the total RoomNights.此矩阵内的值将是总 RoomNights。

Any help would be appreciated.任何帮助,将不胜感激。

Seems like the easiest method would be to unpivot your data in the " Cancellations " table, and then use a CASE expression to display the data in the appropriate column:似乎最简单的方法是在“ Cancellations ”表中取消透视数据,然后使用CASE表达式在相应列中显示数据:

USE Sandbox;
GO

CREATE TABLE dbo.Cancellations (CancelledID int, RebookingID int)
INSERT INTO dbo.Cancellations
VALUES (301,754),
       (387,801),
       (400,900);

GO

CREATE TABLE dbo.Bookings (BookingID int, MonthOfStay date, RoomNights int)
INSERT INTO dbo.Bookings
VALUES (301,'20190301',10),
       (387,'20190401', 7),
       (400,'20190301', 5),
       (754,'20190801',10),
       (801,'20190901', 3),
       (900,'20190701', 5),
       (900,'20190801', 4);
GO

SELECT B.BookingID,
       CASE V.BookingType WHEN 'Cancellation' THEN B.MonthOfStay END AS CancelledMonthOfStay,
       CASE V.BookingType WHEN 'Rebooking' THEN B.MonthOfStay END AS RebookedMonthOfStay,
       B.RoomNights
FROM dbo.Cancellations C
     CROSS APPLY (VALUES(C.CancelledID, 'Cancellation'),(C.RebookingID, 'Rebooking')) V(BookingID,BookingType)
     JOIN dbo.Bookings B ON V.BookingID = B.BookingID
ORDER BY B.BookingID;

GO

DROP TABLE dbo.Cancellations;
DROP TABLE dbo.Bookings;

You can LEFT JOIN the T1 table twice to your T2 table.您可以将 T1 表两次 LEFT JOIN 到您的 T2 表中。 And then use CASE to leave the right columns empty.然后使用 CASE 将右列留空。

SELECT T2.bookingID,
    case when canc.CancelledID is not null then t2.monthOfSTay end as cancelled_MonthOfStay,
    case when reb.rebookingID is not null then t2.monthOfStay end as rebooking_MonthOfStay

FROM T2
LEFT JOIN T1 as canc
    on T2.bookingID = canc.CancelledID
LEFT JOIN T1 as reb
    on T2.bookingID = reb.rebookingID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据另一个表中的选择从一个表中检索特定数据? - How do I retrieve specific data from one table based on a selection in another table? 如何循环遍历一个表并根据另一个表中的数据更新特定列? - How do i loop through a table and updating a specific column based on data in another table? 如何从DataTable中提取数据? - How do I extract data from a DataTable? 如何根据另一个表中的数据更新一个表中的附加字段? - How do I update the additional fields from 1 table based on the data from another table? 如何从具有不同值的表中提取括号内的数据? - How do I extract data within parentheses from a table with different values? 当行数未预先确定时,如何使用SqlDataReader从表中提取数据? - How do I extract data from a table using a SqlDataReader when the number of rows are not predetermined? 如何根据表格中的特定条件选择表格中的特定行? - How do I do select specific rows in a table based on specific conditions in the table? 如何根据表格中的特定值选择值? - How do I select values based on a specific value in a Table? 如何有效地从临时表中提取多个值? - How do I efficiently extract multiple values from a temporary table? 如何根据特定逻辑从任一表中加入和获取数据? - How to JOIN and get data from either table based on specific logics?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM