M K
M K

Reputation: 31

Using select * and count(*) together in a query in SQL Server 2012

I have a table with 12 columns.

I need a query for computing COUNT(*) and selecting all the columns.

I mean I want to have these two queries just in one query:

select * 
from mytable 
where OneOfTheColumns = something;

select COUNT(*) 
from mytable 
where OneOfTheColumns = something;

Conditions and tables are the same.

Can I do this?

Thanks a million.

Upvotes: 0

Views: 2337

Answers (1)

user330315
user330315

Reputation:

You can use a window function for that

select *,
       count(*) over () as total_count
from mytable 
where OneOfTheFields = something;

Upvotes: 3

Related Questions