SQL: Using CROSS APPLY to replace multi-column predicate look-ups in T-SQL
A few months ago, I was asked a question by a friend of mine who is also a Data Platform MVP. He was trying to express a query like this:
SELECT si.StockItemID, si.StockItemName, si.Size, si.LeadTimeDays FROM Warehouse.StockItems AS si WHERE (si.Size, si.LeadTimeDays) IN (('M', 7), ('XXL', 7), ('XXL', 12));
He was trying to select stock items where the combinations of Size and LeadTimeDays are in the list. Unfortunately, in SQL Server and T-SQL, an IN can only be used with a single column. We can’t use a row constructor like (si.Size, si.LeadTimeDays) as a lookup into a list of constructed rows.
2019-06-13
