0
我有一個問題鏈接兩個表,其中需要多次連接1個字段。加入兩個SQL表,其中一個連接字段被多次使用
兩個表如下:
Venue_Location_Master
- ID
- LOCATION_NAME
- UNID
- is_warehouse
Bag_Dim
- EVENT_ID
- Bag_type
- bag_id
- label_id
- CREATED_DATE
- created_by_employee
- origin_location_id
- destination_location_id
- composition_id
該表的連接origin_location_id或destination_location_id到Venue_Location_Master.id
我試圖構建一個返回查詢:
- bag_id
- created_by_employee
- event_name
- origin_location_id
- Venue_Location_Master.location_name(ORIGIN_NAME)
- destination_location_id
- Venue_Location_Master.location_name(DESTINATION_NAME)
我用工會試過,但返回所需的數據,而是跨越兩行(見下文)。任何人有任何建議?
SELECT [bag_id],
[created_date],
[created_by_employee],
[origin_location_id],
ISNULL([venue_location_master].[location_name], 'NULL') AS [origin_location_name],
[destination_location_id],
ISNULL([venue_location_master].[location_name], 'NULL') AS [destination_location_name]
,ISNULL([event_master].[event_name], 'NULL') AS [event_name]
FROM [variance_cash].[dbo].[Bag_Dim]
LEFT JOIN [verteda_rts_v4].[dbo].[venue_location_master]
ON [Bag_Dim].[destination_location_id] = [venue_location_master].[id]
LEFT JOIN [verteda_rts_v4].[dbo].[event_master]
ON [Bag_Dim].[event_id] = [event_master].[id]
WHERE [bag_id] = 'K5334'
左對齊SQL很難閱讀。 – jarlh
您想要將兩列合併爲一個列值(s)? –
這個想法是爲origin_location_id和destination_location_id返回一個帶venue_location_master.location_name的行。我猜這可能需要給列提供一個別名來區分它們? – Sarah