如何从其他 table 中获取内连接不同值的第一个值

how to get first value of inner join distinct value from other table

我有下面的图片和查询。问题是我的查询显示错误的值,在多个记录中没有显示正确的酒店名称,这意味着我想要 VoucherHotelTable 的第一家酒店,但它向我显示第二家酒店,因为 Distinct 我想请帮助我这个如何处理

Select distinct v.VoucherId,u.Name,v.ArrivalFromCity,
       CAST(v.ArrivalDate  AS DATE) as ADDate, v.ArrivalFlightNo, hm.HotelName
from  VoucherMaster v
inner join UserMaster u on  v.AgentId = u.UserId
inner join VoucharHotel vh on v.VoucherId = vh.VoucharId
inner join HotelMaster hm on vh.HotelId = hm.HotelId
inner join AirportTerminal t on  v.ArrivalTerminalId = t.AirprtTerminalId 
where v.ArrivalDate  between '11/15/2018 12:00:00 AM'
    and '11/16/2018 12:00:00 AM'  AND v.ArrivalSectorId = 3

此查询正在通过分区进行。 试试这个。 rank() 函数应该 return 每张优惠券的酒店排名。

;with VoucherHotelRnk as (
    select voucherId, hotelId, date, rank() over(partition by voucherId order by date) rnk
    from VoucharHotel
)
Select v.VoucherId,u.Name,v.ArrivalFromCity,
       CAST(v.ArrivalDate  AS DATE) as ADDate, v.ArrivalFlightNo, hm.HotelName
from  VoucherMaster v
inner join UserMaster u on  v.AgentId = u.UserId
inner join VoucherHotelRnk vh on v.VoucherId = vh.VoucherId and vh.rnk = 1
inner join HotelMaster hm on vh.HotelId = hm.HotelId
inner join AirportTerminal t on  v.ArrivalTerminalId = t.AirprtTerminalId 
where v.ArrivalDate  between '11/15/2018 12:00:00 AM'
    and '11/16/2018 12:00:00 AM'  AND v.ArrivalSectorId = 3
;with VoucherHotelRnk as (
    select voucherId, hotelId, date, ROW_NUMBER() over(partition by voucherId order by date) rowcount
    from VoucharHotel
)
Select v.VoucherId,u.Name,v.ArrivalFromCity,
       CAST(v.ArrivalDate  AS DATE) as ADDate, v.ArrivalFlightNo, hm.HotelName
from  VoucherMaster v
inner join UserMaster u on  v.AgentId = u.UserId
inner join VoucherHotelRnk vh on v.VoucherId = vh.VoucherId and vh.rowcount= 1
inner join HotelMaster hm on vh.HotelId = hm.HotelId
inner join AirportTerminal t on  v.ArrivalTerminalId = t.AirprtTerminalId 
where v.ArrivalDate  between '11/15/2018 12:00:00 AM'
    and '11/16/2018 12:00:00 AM'  AND v.ArrivalSectorId = 3