3

i have 3 tables in sql-server 2008

table A , table B , Table C

i need to count all 3 tables and see it in one query line, like this:

A  B   C
30 40 12

i tried this: select count(*) from A,select count(*) from B,select count(*) from C

but i got error

thank's in advance

4 Answers 4

4
select
    (select count(*) from A) as A,
    (select count(*) from B) as B,
    (select count(*) from C) as C
Sign up to request clarification or add additional context in comments.

1 Comment

Layout is a bit more legible than the others.
2
SELECT *
FROM 
(SELECT COUNT(*) AS A_Count
 FROM A) tmp,
(SELECT COUNT(*) AS B_Count
 FROM B) tmp2, 
(SELECT COUNT(*) AS C_Count
 FROM C) tmp3

Comments

1
SELECT
    A = (SELECT COUNT(*) FROM A),
    B = (SELECT COUNT(*) FROM B),
    C = (SELECT COUNT(*) FROM C)

Comments

0

The other solutions are a little cleaner, but... here's one more way. :)

SELECT SUM(CASE WHEN TableName = 'A' THEN RecordCount ELSE 0 END) AS A_Count,
SUM(CASE WHEN TableName = 'B' THEN RecordCount ELSE 0 END) AS B_Count,
SUM(CASE WHEN TableName = 'C' THEN RecordCount ELSE 0 END) AS C_Count
FROM
(
    SELECT 'A' AS TableName, COUNT(*) AS RecordCount FROM A
    UNION ALL
    SELECT 'B', COUNT(*) FROM B
    UNION ALL
    SELECT 'C', COUNT(*) FROM C
) q

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.