Note that there are some explanatory texts on larger screens.

plurals
  1. POMethod to combine joined tables with id in two different columns?
    primarykey
    data
    text
    <p>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, <strong>EventTable</strong> has the <strong>OrderID</strong> only if certain events happen. Otherwise it's Null. </p> <p>EventTable </p> <pre><code>EventID EventName OrderID DateTS 1 Rec 1 date1 2 Track NULL date2 3 Run NULL date3 </code></pre> <p>EventItemTable </p> <pre><code> EventID Column2 OrderID 1 Other 1 2 stuff 2 3 beer 3 </code></pre> <p>OrderTable</p> <pre><code>OrderID orderRef ProdID 1 1234 1 2 1233 1 3 1232 3 </code></pre> <p>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 <strong>orderRef</strong> column to show all the events that occurred. </p> <p>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.</p> <pre><code>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 </code></pre> <p>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. I would still be able to have all the other columns but with the else condition the rows would just be populated with 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)</p> <p><strong>EDIT</strong> </p> <p>This is what I ended up with that got all the OrderID in one column to join with @AlexJohnson suggestion. My issue now is that a couple have duplicate manufacturingEventID in the rows per order reference. Not sure if the ISNULL is causing that issue</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload