获取重复输出,可能做错连接了吗?

时间:2019-06-17 15:39:05

标签: sql sql-server

我遇到一个问题,我在输出中得到了一些不正确的值。我将下面突出显示的表格列与底部的带圆圈的列绑定在一起。突出显示的列上的service_id是唯一的,但是我需要绑定booking_id来检索信息(如果这样有意义。我最终得到的是重复出现的顶层表格,或者价格不正确。我应该顶表中只有5行。

enter image description here

这是我的代码。我怀疑我的连接可能做错了吗?

SELECT bad.agent as Agents,
dog.SUPPLIER as SUPPLIER,
bad.status as TheStatus,
country.analysis_master1 as Country,
ftb.booking_actual_retail as BookingActualRetail,
ftb.Booking_Actual_Cost as BookingCost,
ftb.Booking_Actual_Retail_inc as BookingRetailINC,
fts.Service_Id,
fts.Service_Actual_Retail_inc as ServiceActualCostInc,
Product.SERVICE,
Product.SL_STATUS as SLSTATUS,
cat.name as Product2,
bad.LAST_SERVICE_DATE as Servicedate,
bad.LW_DATE as LWDATE,
ftb.Entered_Date as DATEENTERED,
ftb.Booking_Pax as PEOPLE,
ftb.Booking_Children as KIDS,
bad.TRAVELDATE as TRAVELDATE,
bad.FULL_REFERENCE
from BHD bad
inner join FTB on bad.FULL_REFERENCE = ftb.booking_reference
inner join FTS on FTB.Booking_Id = fts.Booking_Id
inner join DRM Country on bad.agent = country.code
inner join BSL Product on bad.BHD_ID = Product.BHD_ID
inner join SRV cat on Product.SERVICE = cat.CODE
inner join OPT dog on Product.OPT_ID = dog.OPT_ID
where bad.STATUS = 'IV' AND bad.FULL_REFERENCE = 'LTIT129488'

更新:

好,所以看起来这里的连接会导致多个输出:

inner join FTS on FTB.Booking_Id = fts.Booking_Id

我已经包含了两个表,它们的标题和示例数据

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

您在某处以错误的方式放置了服务或供应商的联接。请再次检查此行。

y = ([[0, 0, 0, 0],
       [0, 2, 1, 0],
       [1, 0, 1, 1],
       [0, 0, 0, 0]])

as per the screenshot you have provided see the multiple service acreoss the same product

更新的解决方案:

根据您更新的屏幕截图,我发现了问题... 你已经像这样加入了。

inner join SRV cat on Product.SERVICE = cat.CODE

在此联接中,您的一个表具有针对预订参考的单个记录,而另一个表具有针对预订参考的多个记录。这就是为什么要在输出中获取多个记录的原因。

enter image description here

删除此联接,您的问题将得到解决。如果您确实要从该表中获取数据,则可以采用其他方式进行选择,例如使用外部应用等。