我有三张桌子:
BookingNode,预订AirTrip
AirTrip:
+----+------------+
| ID | Name |
+----+------------+
| 0 | One way |
| 1 | Round trip |
| 2 | Circle |
| 3 | Other |
+----+------------+
我们在预订时将数据存储为: BookingNode表
+--------+-------------------+------------+----------------------+
| ID | CustomerGivenName | IPAddress | Email |
+--------+-------------------+------------+----------------------+
| 177022 | xfghfh | 2130706473 | mikehussey@gmail.com |
| 177021 | cfggjfj | 2130706473 | mikehussey@gmail.com |
+--------+-------------------+------------+----------------------+
预订表:
+--------+---------------+-----------+------------+------------+
| ID | BookingNodeID | AirTripID | AirLineId | Provider |
+--------+---------------+-----------+------------+------------+
| 181251 | 177020 | 1 | 978 | Jet |
| 181252 | 177021 | 0 | 982 | Go |
| 181253 | 177021 | 0 | 978 | Jet |
+--------+---------------+-----------+------------+------------+
如果预订往返航班且ProviderID相同,则在预订表中完成单个条目,AirTripID值为1.(预订ID:181251和提供者Jet) 但如果提供商的两条腿都不同,则两个条目在预订表中使用AirTripID完成,两个条目都是一个(预订ID:181252和181253 Provider Go,Jet)。在这种情况下,BookingNodeID值相同。
问题:我必须编写一个查询来获取不同类型的预订。(Oneway,RoundTrip,Circle)。但是当我在AirTripID上应用联接时,它给了我不正确的结果。 我如何编写我的查询以获得正确的结果,因为知道BookingNodeID对于往返行程是相同的(预订表中的两个条目)
示例输出
+-------------+---------------+-------------------+------------+
| AirTripName | BookingNodeID | CustomerGivenName | IPAddress |
+-------------+---------------+-------------------+------------+
| TwoWay | 177020 | xfghfh | 2130706473 |
| TwoWay | 177021 | cfggjfj | 2130706473 |
+-------------+---------------+-------------------+------------+
答案 0 :(得分:0)
基本上,由于我的数据输入懒惰,这段代码可能会出错。但是,查询的逻辑是,如果b.AirTripID为0,则通过Booking添加额外条件。如果结果返回超过1行,实际上是2路。所以AirTripType将变为1,否则,保持与b.AirTripID相同。您可以在下面复制并尝试修复,如果有任何错误。我相信逻辑应该基于你的预期结果。
select
bd.ID,
bd.CustomerGivenName,
case b.AirTripID
when 1 then 1
when 2 then 2
when 3 then 3
when 0 then
case select BookingNodeID
from Booking
where Booking.BookingNodeID = bd.ID group by BookingNodeID having Count(BookingNodeID)
when 1 then 1
else 0 end as AirTripType,
bd.IPAddress
from BookingNode bd
inner join (select BookingNodeID ,AirTripID from Booking group by BookingNodeID ,AirTripID) as b on b.BookingNodeID = bd.ID
where id=177021
答案 1 :(得分:0)
试试这个
WITH CTE
AS
(
SELECT
SeqNo = ROW_NUMBER() OVER(PARTITION BY BN.ID ORDER BY B.ID),
B.BookingNodeID,
BN.CustomerGivenName,
BN.IPAddress,
AirTripId = A.ID,
AirTripNm = A.Name
FROM Booking B
INNER JOIN AirTrip A
ON A.ID = B.AirTripID
LEFT JOIN BookingNode BN
ON B.BookingNodeID = BN.id
)
SELECT
C1.SeqNo,
AirTripName = CASE WHEN C2.SeqNo IS NOT NULL
THEN 'Round trip'
ELSE C1.AirTripNm END,
C1.BookingNodeID,
C1.CustomerGivenName,
C1.IPAddress
FROM CTE C1
LEFT JOIN CTE C2
ON C1.BookingNodeID = C2.BookingNodeID
AND C2.SeqNo = 2
WHERE c1.SeqNo = 1
答案 2 :(得分:0)
Select distinct bk.bookingnodeid,cst.customername,ipaddress,
case when count(airtripid)over(partition by bookingnodeid order by bookingnodeid)=2 then 'RoundTrip' else name end As AirTripName
from booking bk
inner join airlinetrip at
on bk.airtripid=at.id
inner join customer cst
on cst.id=bk.bookingnodeid