| | |
SQL record count
Please support our MS SQL advertiser: Intel Parallel Studio Home
![]() |
If your table has multiple records per employee as in
You can group these records together, and use the aggregate function "COUNT" to get a number of rows each employee has in the table. This can be done using this script
Since Employee, John Smith, has two records in the table, his row count will be 2, while Jane Doe and Bob Smith will only have 1.
Hope this helps
MS SQL Syntax (Toggle Plain Text)
EmployeeID, EmployeeName ------------------------------------ 12345, John Smith 12346, Jane Doe 12345, John Smith 12347, Bob Smith
You can group these records together, and use the aggregate function "COUNT" to get a number of rows each employee has in the table. This can be done using this script
MS SQL Syntax (Toggle Plain Text)
SELECT EmployeeID, EmployeeName, COUNT(*) AS RowCount FROM TableA GROUP BY EmployeeID, EmployeeName ORDER BY RowCount DESC --this will sort most row count first
Since Employee, John Smith, has two records in the table, his row count will be 2, while Jane Doe and Bob Smith will only have 1.
MS SQL Syntax (Toggle Plain Text)
EmployeeID, EmployeeName, RowCount ------------------------------------- 12345, John Smith, 2 12346, Jane Doe, 1 12347, Bob Smith, 1
Hope this helps
If in doubt, reach into the trash can and remove the user guide.
•
•
Join Date: Mar 2009
Posts: 4
Reputation:
Solved Threads: 0
Hi ebyong77
In sql server 2005 you can use this query to get rownumber
Row_number retrurn the serial number of records.
In sql server 2005 you can use this query to get rownumber
sql Syntax (Toggle Plain Text)
SELECT emp_name, ROW_NUMBER() OVER (ORDER BY emp_id) AS 'ROWCOUNT' FROM yourtable name
Row_number retrurn the serial number of records.
Last edited by peter_budo; Mar 6th, 2009 at 8:58 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Ravinder kumar
http://www.ravinderweb.com
http://www.ravinderweb.com
![]() |
Similar Threads
- Finding the nth row (SQL)...debative question (MS SQL)
- Querystring values not matching with the SQL statement (ASP)
- Get occurrences of a record (MS SQL)
- How to display my sql 'Count' result... (VB.NET)
- My SQL Help :( (MySQL)
- verify page not finding random records (ASP)
- SQL Record COUNT (ASP.NET)
- "ADODB Recordset error" (ASP)
Other Threads in the MS SQL Forum
- Previous Thread: How to retireve single column records horizontally with SQL Statement
- Next Thread: Problem with "like" statement
| Thread Tools | Search this Thread |





