I have three tables
Table1
col1, col2
Table2
col1, col2
table3
col1, col2, col3
col2 of table2 is a foreign key refering tabl1(col1)
col2 of table3 is a foreign key refering table2(col1)
I want to get for each col1 in table1, how many distinct values of col3 are there in table3
I tryed the query like this
SELECT COL1,
(SELECT COUNT(DISTINCT(TBL3.COL3))
FROM TABLE2 TBL2 INNER JOIN TABLE3 TBL3
ON TBL3.COL2 = TBL2.COL1
WHERE TBL2.COL2 = TBL1.COL1
) XY
FROM TABLE1 TBL1
because of the DISTINCT key word, it is costing me time
Is there other way to write this in a more efficient manner
Thanks,
Table1
col1, col2
Table2
col1, col2
table3
col1, col2, col3
col2 of table2 is a foreign key refering tabl1(col1)
col2 of table3 is a foreign key refering table2(col1)
I want to get for each col1 in table1, how many distinct values of col3 are there in table3
I tryed the query like this
SELECT COL1,
(SELECT COUNT(DISTINCT(TBL3.COL3))
FROM TABLE2 TBL2 INNER JOIN TABLE3 TBL3
ON TBL3.COL2 = TBL2.COL1
WHERE TBL2.COL2 = TBL1.COL1
) XY
FROM TABLE1 TBL1
because of the DISTINCT key word, it is costing me time
Is there other way to write this in a more efficient manner
Thanks,