Morteza
Morteza

Reputation: 191

Get Count All Rows In Select Column

I have table like this

  Id   |    Name    |    Status
 ------+------------+--------------
  1       example1         3
  1       example2         2
  2       example3         3
  2       example4         1
  3       example5         1
  4       example6         3

How To Write SELECT That Get To ME Result Like This

  Id   |    Name    |    Status    |    Count_All
 ------+------------+--------------+------------
  1       example1         3               6
  2       example2         2               6
  3       example3         3               6
  4       example4         1               6
  5       example5         1               6
  6       example6         3               6

The Value Of Column Count_All, Is Count All Rows

Upvotes: 1

Views: 2914

Answers (4)

Sai Kumar Reddy
Sai Kumar Reddy

Reputation: 75

SELECT *,COUNT(1) OVER() AS COUNT FROM TABLE

Upvotes: 0

tysonwright
tysonwright

Reputation: 1525

This will do it:

   SELECT
        y.ID,
        y.Name,
        y.Status,
        Count_All = (SELECT COUNT(*) FROM yourtable)
    FROM
        yourtable AS y

Upvotes: 0

Ryan Wilson
Ryan Wilson

Reputation: 10765

If I am understanding you correctly, you want a count of all rows in the table as the column Count_All, so Add a Count on column id to get all rows as a new column named Count_All then Group by your other columns to allow for the aggregate Count method.

SELECT [Id], [Name], [Status], COUNT([Id]) AS [Count_All]
FROM [dbo].[YourTable]
GROUP BY [Id], [Name], [Status]

Upvotes: 1

Pawan Kumar
Pawan Kumar

Reputation: 2011

Please use this solution..

SELECT Id , [Name] , [Status], COUNT(*) OVER() Count_All
FROM yourTableName

Upvotes: 4

Related Questions