Thursday, March 22, 2012

Count 4 tables

I am trying to get a count of rows in 4 tables at once.

Output:

Table1 Table2 Table3 Table4

12345 143 892626 28428

Or maybe even

Table Count

Table1 12345

Table2 143

Table3 892626

Table4 28428

Thanks,

Cory

here it is...

Select

(Select Count(*) From Table1) [Table1],

(Select Count(*) From Table2) [Table2],

(Select Count(*) From Table3) [Table3],

(Select Count(*) From Table4) [Table4]

--Or

Select 'Table1' Table, Count(*) Count From Table1

Union ALL

Select 'Table2' Table, Count(*) Count From Table2

Union ALL

Select 'Table3' Table, Count(*) Count From Table3

Union ALL

Select 'Table4' Table, Count(*) Count From Table4

|||

Would this do the trick?

SELECT 'Table1' AS table, COUNT (*) AS count FROM table1 UNION

SELECT 'Table2', COUNT (*) FROM table2 UNION

SELECT 'Table3, COUNT (*) FROM table3 UNION

SELECT 'Table4', COUNT (*) FROM table4

|||

At always better option to use UNION ALL rather than UNION.

No comments:

Post a Comment