79195008

Date: 2024-11-16 11:08:59
Score: 0.5
Natty:
Report link

My answer would be logically same as to @Dale's.

you can create a common table which has same logic and then create 3 temporary tables.

Regarding performance, you can create index on CommonData table on customerId and country as they are in filtering condition.

example:

CREATE TABLE #CommonData (
-- Column definitions
);

INSERT INTO #CommonData
SELECT 
    C.*, 
    O.*, 
    OT.*
FROM Customers C
LEFT JOIN Orders O ON C.CustomerID = O.CustomerID
LEFT JOIN OtherTables OT ON C.SomeID = OT.SomeID
WHERE C.Active = 1 AND O.OrderStatus = 'Completed';

SELECT * FROM #CommonData WHERE CustomerID > 80;
SELECT * FROM #CommonData WHERE CustomerID = 1;
SELECT * FROM #CommonData WHERE Country = 'Mexico';
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @Dale's
  • Low reputation (0.5):
Posted by: samhita