简体   繁体   English

在两个不同的列中组合具有ID的联接表的方法?

[英]Method to combine joined tables with id in two different columns?

I've been trying to figure out how to join a table to other tables that I've joined but the way the tables are set-up, EventTable has the OrderID only if certain events happen. 我一直在尝试找出如何将一个表连接到我已连接的其他表,但是表的设置方式是EventTable仅在发生某些事件时才具有OrderID Otherwise it's Null. 否则为空。

EventTable 事件表

EventID EventName  OrderID DateTS
1       Rec        1       date1
2       Track      NULL    date2
3       Run        NULL    date3

EventItemTable EventItemTable

 EventID    Column2    OrderID
    1       Other      1
    2       stuff      2
    3       beer       3

OrderTable 订单表

OrderID         orderRef   ProdID
        1       1234       1
        2       1233       1
        3       1232       3

Now I've been trying to get all the Event rows which has all of the events that I'm looking for. 现在,我一直在尝试获取包含我正在寻找的所有事件的所有事件行。 What I need to be able to do is be able to allow users to use Tableau which will filter the orderRef column to show all the events that occurred. 我需要做的就是能够允许用户使用Tableau,它将过滤orderRef列以显示所有发生的事件。

I'm pretty new to SQL queries and the per row logic I'm thinking would be if statement but don't know how it would work in SQL. 我对SQL查询和我想使用的每行逻辑都是if语句很陌生,但不知道它如何在SQL中工作。

SELECT OrderTable.orderRef, EventTable.DateTS, EventTable.EventName

If EventTable.OrderID column isNull, then          

FROM EventItemTable  
  join EventItemTable ON EventItemTable.OrderID = EventTable.OrderID
  join OrderTable ON OrderTable.OrderID = EventItemTable.OrderID

ELSE

 FROM EventItemTable 
  left join OrderTable ON OrderTable.OrderID = EventTable.OrderID

In my mind, I would think that the orderRef would then be populated in the entire table and have no nulls or be filter out. 我认为,然后将orderRef填充到整个表中,并且没有空值或将其过滤掉。 I would still be able to have all the other columns but with the else condition the rows would just be populated with NULL. 我仍然可以拥有所有其他列,但在其他条件下,行将只填充为NULL。 I've been looking at subqueries and other functions to try but not seeing anything that seems to be able to do this. 我一直在尝试尝试子查询和其他功能,但没有看到似乎能够做到这一点的任何东西。 I'm using a Microsoft SQL 2005 (I believe) 我正在使用Microsoft SQL 2005(我相信)

EDIT 编辑

This is what I ended up with that got all the OrderID in one column to join with @AlexJohnson suggestion. 这就是我最终将所有OrderID放在一栏中并与@AlexJohnson建议一起加入的原因。 My issue now is that a couple have duplicate manufacturingEventID in the rows per order reference. 我现在的问题是,每个订单引用的行中有一对重复的ManufacturingEventID。 Not sure if the ISNULL is causing that issue 不知道ISNULL是否导致该问题

SELECT met.met_ManufacturingEventID
        ,met.met_OrderID
        ,ori.ori_OrderID
        ,mfi.mfi_OrderID
        ,mfi.mfi_LastEvent
        ,mfi.mfi_RunNumber
        ,mfi.mfi_InputOrder
        ,meo.meo_ManufacturingOperationName
        ,mes.mes_ManufacturingStationName as StationName
        ,mes.mes_StationStatusName as StationStatus
      ,met.met_EventDate as EventDate
--      ,oie.oie_EventDate
        ,met.met_ToothID
      ,met.met_Comment as metComment
      ,met.met_ManufacturingOrderNumber
--      ,ISNULL(mfi.mfi_OrderID,met.met_OrderID) as OrderID
        ,ord.ord_OrderReference as OrderRef


  FROM [dbo].[ManufacturingEvents] met
  LEFT JOIN [dbo].[ManufacturingEventOrderItems] mfi
      ON mfi.mfi_ManufacturingEventID = met.met_ManufacturingEventID
  LEFT JOIN [dbo].[OrderItems] ori
      ON ori.ori_OrderItemID = mfi.mfi_OrderItemID
  LEFT JOIN [AtlantisProductionSystem_Prod_US].[dbo].[Orders] ord
      ON ord.ord_OrderID = ISNULL(mfi.mfi_OrderID,met.met_OrderID)
  LEFT JOIN dbo.ManufacturingStations mes
      ON mes.mes_ManufacturingStationID = met.met_ManufacturingStationID
  JOIN dbo.ManufacturingOperations meo
      ON meo.meo_ManufacturingOperationID = mes.mes_ManufacturingOperationID

Give this a shot: 试一下:

SELECT ot.orderRef, et.DateTS, et.EventName
FROM OrderTable ot
JOIN EventItemTable eit
  ON eit.OrderID = ot.OrderID
LEFT JOIN EventTable et
  ON et.OrderID = ot.OrderID

You will still probably want to tweak this because if the EventTable OrderID is null, the information returned may not be useful. 您可能仍然需要进行调整,因为如果EventTable OrderID为null,则返回的信息可能没有用。

You can use: 您可以使用:

SELECT ot.orderRef, et.DateTS, ISNULL(et.EventName, eit.Column2)

Or something similar. 或类似的东西。

I'm not sure if EventTable and EventItemTable have any other relationship besides OrderID, but if we could join them on EventID you could get much better results: 我不确定EventTable和EventItemTable是否具有除OrderID之外的任何其他关系,但是如果我们可以将它们加入EventID上,则可以获得更好的结果:

SELECT ot.orderRef, et.DateTS, et.EventName
FROM EventTable et
JOIN EventItemTable eit
  ON et.EventID = eit.EventID
JOIN OrderTable ot
  ON eit.OrderID = ot.OrderID

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM