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
SUM() and COUNT() functions SUM of values of a field or column of a SQL table, generated using SQL SUM() function can be stored in a variable or temporary column referred as alias. The same approach can be used with SQL COUNT() function too.
The use of COUNT() function in conjunction with GROUP BY is useful for characterizing our data under various groupings. A combination of same values (on a column) will be treated as an individual group.
The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With