Reputation: 125
So here's the two tables I'm working with
StockItem:
ItemID SKU MSKU
-------------------- ------------------------------ -------------------------
36414 SWR0060
14859140 4220.12.010 220.12.010
14860211 6220.12.010 220.12.010
47717413 6000.22.010 6000.22.XXX
102244764 SWR-SPIGOT SWR-SPIGOTS
115377648 SWR-SPIGOTBLA SWR-SPIGOTS
115381142 SWR-SPIGOTCOP SWR-SPIGOTS
116112478 6000.22.030 6000.22.XXX
116112701 6000.22.050 6000.22.XXX
119263276 SWR-SPIGOTAG SWR-SPIGOTS
StockItemMemo:
ItemID MemoText
-------------------- ----------------------------------------
36414 Title:Professional C7 Steel Wire Rope Cutters (0.5mm - 4mm) Snips Fence Tool
14859140 Title:Stainless Steel Base Plate for 42.4mm Balustrade Handrail Railing Component
47717413 Title:52mm Stainless Glass Adapter (10-20mm) - Metric and Wood Thread Balustrade Handrail
102244764 Title:Stainless Steel Spigot 10-12mm Glass Handrail Balustrade Railing Balcony Clamp
Currently, when I run the query below, the title is displayed for the primary items which have the title directly joined to them, but the items joined to those items by the MSKU field show NULLs
DECLARE @title as VARCHAR(MAX) = 'Title:';
SELECT
ItemID,
Code as SKU,
AnalysisCode12 as MSKU,
(SELECT TOP 1 MemoText FROM StockItemMemo WHERE StockItemMemo.ItemID = StockItem.ItemId and MemoText like @title+'%') as Title
From StockItem WHERE AnalysisCode7 = 'YES'
ItemID SKU MSKU Title
-------------------- ------------------------------ ------------------------------------------------------------ ----------------------------------------------
36414 SWR0060 Professional C7 Steel Wire Rope Cutters (0.5mm - 4mm) Snips Fence Tool
14859140 4220.12.010 220.12.010 Stainless Steel Base Plate for 42.4mm Balustrade Handrail Railing Component
14860211 6220.12.010 220.12.010 NULL
47717413 6000.22.010 6000.22.XXX 52mm Stainless Glass Adapter (10-20mm) - Metric and Wood Thread Balustrade Handrail
102244764 SWR-SPIGOT SWR-SPIGOTS Stainless Steel Spigot 10-12mm Glass Handrail Balustrade Railing Balcony Clamp
115377648 SWR-SPIGOTBLA SWR-SPIGOTS NULL
115381142 SWR-SPIGOTCOP SWR-SPIGOTS NULL
116112478 6000.22.030 6000.22.XXX NULL
116112701 6000.22.050 6000.22.XXX NULL
119263276 SWR-SPIGOTAG SWR-SPIGOTS NULL
I need the title from the main SKU to show for all the items with a matching MSKU too, so the below is the result I want:
ItemID SKU MSKU Title
-------------------- ------------------------------ ------------------------------------------------------------ ----------------------------------------------
36414 SWR0060 Professional C7 Steel Wire Rope Cutters (0.5mm - 4mm) Snips Fence Tool
14859140 4220.12.010 220.12.010 Stainless Steel Base Plate for 42.4mm Balustrade Handrail Railing Component
14860211 6220.12.010 220.12.010 Stainless Steel Base Plate for 42.4mm Balustrade Handrail Railing Component
47717413 6000.22.010 6000.22.XXX 52mm Stainless Glass Adapter (10-20mm) - Metric and Wood Thread Balustrade Handrail
102244764 SWR-SPIGOT SWR-SPIGOTS Stainless Steel Spigot 10-12mm Glass Handrail Balustrade Railing Balcony Clamp
115377648 SWR-SPIGOTBLA SWR-SPIGOTS Stainless Steel Spigot 10-12mm Glass Handrail Balustrade Railing Balcony Clamp
115381142 SWR-SPIGOTCOP SWR-SPIGOTS Stainless Steel Spigot 10-12mm Glass Handrail Balustrade Railing Balcony Clamp
116112478 6000.22.030 6000.22.XXX 52mm Stainless Glass Adapter (10-20mm) - Metric and Wood Thread Balustrade Handrail
116112701 6000.22.050 6000.22.XXX 52mm Stainless Glass Adapter (10-20mm) - Metric and Wood Thread Balustrade Handrail
119263276 SWR-SPIGOTAG SWR-SPIGOTS Stainless Steel Spigot 10-12mm Glass Handrail Balustrade Railing Balcony Clamp
What would be the best way of doing this?
Upvotes: 1
Views: 30
Reputation: 17126
I believe you can achieve your desired output using a query like below
DECLARE @title as VARCHAR(MAX) = 'Title:';
; WITH CTE AS
(
SELECT
AnalysisCode12 as MSKU,
MemoText as Title
FROM StockItemMemo SIM
JOIN StockItem SI
ON SIM.ItemID = SI.ItemId
WHERE MemoText like @title+'%'
)
SELECT
ItemID,
Code as SKU,
AnalysisCode12 as MSKU,
C.Title
From StockItem SI
LEFT JOIN CTE C
ON C.MSKU=SI.AnalysisCode12
WHERE AnalysisCode7 = 'YES'
Upvotes: 2