T-SQL Question

  • I have 4 separate tables that are related to each other. I want to return a count of each record in a table. Yet, return the answer in one query. What is the best method of going about this. I am interested in evaluating the logic necessary.

  • smiF (8/9/2016)


    I have 4 separate tables that are related to each other. I want to return a count of each record in a table. Yet, return the answer in one query. What is the best method of going about this. I am interested in evaluating the logic necessary.

    Here are two hints:

    select (select 1 as column_name) as column_subquery;

    select count(*) from dbo.table_name;

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • For a simple count of the tables, you can use something like this:

    SELECT t.name AS Table_Name, SUM(row_count) AS Row_Count

    FROM sys.dm_db_partition_stats s

    JOIN sys.tables t ON s.object_id = t.object_id

    WHERE index_id IN (0,1)--heap or clustered index

    AND t.name IN ('Table1', 'Table2')

    GROUP BY t.name;

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Thanks everyone for the replies...I could not see around this corner. This is the logic I was looking for..

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply