943,608 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 8578
  • MS SQL RSS
Mar 3rd, 2009
0

SQL record count

Expand Post »
hi i need to know how to query a number of count on a particular table
let say i have emp_id and emp_name field. on my table their are 2 records.

the result looks this:

rowcount | emp_id | emp_name
1 1000 albert
2 2000 leah

is it possible?

thanks,
albert
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ebyong77 is offline Offline
3 posts
since Dec 2008
Mar 5th, 2009
0

Re: SQL record count

If your table has multiple records per employee as in

MS SQL Syntax (Toggle Plain Text)
  1. EmployeeID, EmployeeName
  2. ------------------------------------
  3. 12345, John Smith
  4. 12346, Jane Doe
  5. 12345, John Smith
  6. 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)
  1. SELECT EmployeeID, EmployeeName, COUNT(*) AS RowCount FROM TableA
  2. GROUP BY EmployeeID, EmployeeName
  3. 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)
  1. EmployeeID, EmployeeName, RowCount
  2. -------------------------------------
  3. 12345, John Smith, 2
  4. 12346, Jane Doe, 1
  5. 12347, Bob Smith, 1

Hope this helps
Reputation Points: 12
Solved Threads: 6
Junior Poster
Geek-Master is offline Offline
156 posts
since Dec 2004
Mar 5th, 2009
0

Re: SQL record count

Hi ebyong77

In sql server 2005 you can use this query to get rownumber

sql Syntax (Toggle Plain Text)
  1. SELECT emp_name, ROW_NUMBER() OVER (ORDER BY emp_id) AS 'ROWCOUNT'
  2. 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ravinder007 is offline Offline
4 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: How to retireve single column records horizontally with SQL Statement
Next Thread in MS SQL Forum Timeline: Problem with "like" statement





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC