SQL record count

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 3
Reputation: ebyong77 is an unknown quantity at this point 
Solved Threads: 0
ebyong77 ebyong77 is offline Offline
Newbie Poster

SQL record count

 
0
  #1
Mar 3rd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 149
Reputation: Geek-Master is an unknown quantity at this point 
Solved Threads: 6
Geek-Master's Avatar
Geek-Master Geek-Master is offline Offline
Junior Poster

Re: SQL record count

 
0
  #2
Mar 5th, 2009
If your table has multiple records per employee as in

  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

  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.

  1. EmployeeID, EmployeeName, RowCount
  2. -------------------------------------
  3. 12345, John Smith, 2
  4. 12346, Jane Doe, 1
  5. 12347, Bob Smith, 1

Hope this helps
If in doubt, reach into the trash can and remove the user guide.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 4
Reputation: ravinder007 is an unknown quantity at this point 
Solved Threads: 0
ravinder007 ravinder007 is offline Offline
Newbie Poster

Re: SQL record count

 
0
  #3
Mar 5th, 2009
Hi ebyong77

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

  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC