SELECT DISTINCT BH.Doc_No, BH.Expected_Arrival_Date,BH.Checkout_Date,BH.Ref_No,LGH.Node_Name AS Location_Name,LGH.Node_Code AS Location_Code,CH.Doc_No AS Checking,BH.Doc_Date,BH.Setup_Date_Time,GD.Guest_Name
FROM [booking].[Booking_Header] AS BH
LEFT JOIN [booking].[Booking_Details] AS BD ON BD.Doc_No =BH.Doc_No
LEFT JOIN [booking].[Guest_Details] AS GD ON BD.Customer_Code=GD.Guest_Code
LEFT JOIN [booking].[Checkout_Header] AS CH ON BH.Doc_No= CH.Booking_Code AND CH.Status=0
LEFT JOIN [dbo].[Location_Group_Header] AS LGH ON BH.Location_Code= LGH.Node_Code
LEFT JOIN [dbo].[User_Locations] AS UL ON BH.Setup_Location_Code = UL.Location_Code
WHERE UL.User_Code= 'EZCMP1/EZUSR-8' AND BH.Status= 0 AND UL.Status= 0 AND LGH.Status= 0
ORDER BY BH.Expected_Arrival_Date ASC
我想从此查询中获取顶级客人名称....
答案 0 :(得分:1)
这个怎么样:
SELECT TOP 1 *
FROM
(
SELECT DISTINCT BH.Doc_No, BH.Expected_Arrival_Date,BH.Checkout_Date,BH.Ref_No,LGH.Node_Name AS Location_Name,LGH.Node_Code AS Location_Code,CH.Doc_No AS Checking,BH.Doc_Date,BH.Setup_Date_Time,GD.Guest_Name
FROM [booking].[Booking_Header] AS BH
LEFT JOIN [booking].[Booking_Details] AS BD ON BD.Doc_No =BH.Doc_No
LEFT JOIN [booking].[Guest_Details] AS GD ON BD.Customer_Code=GD.Guest_Code
LEFT JOIN [booking].[Checkout_Header] AS CH ON BH.Doc_No= CH.Booking_Code AND CH.Status=0
LEFT JOIN [dbo].[Location_Group_Header] AS LGH ON BH.Location_Code= LGH.Node_Code
LEFT JOIN [dbo].[User_Locations] AS UL ON BH.Setup_Location_Code = UL.Location_Code
WHERE UL.User_Code= 'EZCMP1/EZUSR-8' AND BH.Status= 0 AND UL.Status= 0 AND LGH.Status= 0
) DS
ORDER BY Expected_Arrival_Date ASC
获取唯一值,然后对它们进行排序并获得第一个值。
答案 1 :(得分:0)
LEFT JOIN [预订]。[Booking_Details] AS BD ON BD.Doc_No = BH.Doc_No AND BD.Status = 0 AND BD.Customer_Code = (选择TOP 1 T_BD.Customer_Code FROM [预订]。[Booking_Details] AS T_BD WHERE T_BD.Doc_No = BH.Doc_No) LEFT JOIN [预订]。[Guest_Details] AS GD ON BD.Customer_Code = GD.Guest_Code AND GD.Status = 0
尝试了这个,它给出了第一个客人名称的正确答案