I think I need a CROSS JOIN to make this work, but I can't seem to get it right. If I use DISTINCT, I get the expected results but I'm sure there's a better way.SET NOCOUNT ONCREATE TABLE Table1( parm_defn_id int NOT NULL)CREATE TABLE Table2( terminal_gt_id int NOT NULL, parm_defn_id int NOT NULL)INSERT INTO Table1 (parm_defn_id)SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5INSERT INTO Table2 (terminal_gt_id, parm_defn_id)SELECT 42, 1 UNION ALL SELECT 42, 3 UNION ALL SELECT 98, 1 UNION ALL SELECT 98, 3UNION ALL SELECT 66, 1 UNION ALL SELECT 66, 3UNION ALL SELECT 32, 1 UNION ALL SELECT 32, 3 UNION ALL SELECT 32, 4SELECT t2.terminal_gt_id, t1.parm_defn_idFROM Table1 t1CROSS JOIN Table2 t2DROP TABLE Table1DROP TABLE Table2
Notice the dups in the result set. Now if I do this, I get what I need:SELECT DISTINCT t2.terminal_gt_id, t1.parm_defn_idFROM Table1 t1CROSS JOIN Table2 t2Expected result set:terminal_gt_id parm_defn_id -------------- ------------ 32 132 232 332 432 542 142 242 342 442 566 166 266 366 466 598 198 298 398 498 5
DISTINCT just doesn't seem right though. I'd like to further refine my query. I'd like to exclude parm_defn_ids that already have a row for that terminal_gt_id. Here's the expected result set:terminal_gt_id parm_defn_id -------------- ------------ 32 232 542 242 442 566 266 466 598 298 498 5
Note terminal_gt_id of 32 is different from the others. It already has 4 for parm_defn_id where as the others do not so that's why 32 doesn't have 4 in this result set.Does a CROSS JOIN automatically order the results? I was surprised to see the ordering be terminal_gt_id, parm_defn_id since I didn't specify it. Tara