Home > Software design >  Union tables based on row count
Union tables based on row count

Time:06-12

How do I union tables based on row count

For instance, assume I have 10 tables and have to select the top 10 rows as below

  1. If table1 has 10 rows then return 10 rows.

  2. If table1 has 5 rows then union table2 and return the top 10 rows(if row count>10 then don't union table3 and etc)

  3. If table1 has 3 rows and table2 has 2 rows then union with table 3 and return the top 10 rows and so on.

My current SQL query is below, but this one union all table and returns top 10 which is not performant (I think).

SELECT TOP 10  NAME FROM(
SELECT TOP 10 NAME FROM TABLE1 WHERE NAME LIKE '           
  • Related