Hary
Hary

Reputation: 5818

Getting Count and Rows in same query

Is it possible to get the total table count and rows in same query. something like this

SELECT COUNT(1),*
FROM tbl
GROUP BY ALL

Upvotes: 6

Views: 3637

Answers (4)

Hary
Hary

Reputation: 5818

I found this one too and marc_c answer will be more better than this

SELECT (SELECT COUNT(1) from tbl) AS Total,*
FROM tbl

Upvotes: 1

user2001117
user2001117

Reputation: 3777

You can use :

1) select column1,coulmn2,COUNT(*) OVER (PARTITION BY 1) as RowCnt from #Table;

2)Using the cross join method:

SELECT a.*, b.numRows
      FROM TABLE a
CROSS JOIN (SELECT COUNT(*) AS numRows
              FROM TABLE) b

Upvotes: 3

marc_s
marc_s

Reputation: 755391

You can always try something like this:

SELECT
    COUNT(*) OVER (),
    (list of your other columns here)
FROM dbo.YourTableNameHere

The OVER() clause will give you a count of all rows right in your query.

Upvotes: 12

Orangecrush
Orangecrush

Reputation: 1990

Try this,

SELECT (select count(*) from table), b.*
  FROM table b;

Upvotes: 3

Related Questions