Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 8th, 2004, 05:37 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: abu dbabi, , United Arab Emirates.
Posts: 518
Thanks: 0
Thanked 0 Times in 0 Posts
Default count clause in query

I use following query.

SELECT employees.empno, employees.name, approvals.contractno, assignments.acontract,assignments.aposit, assignments.astrdate, assignments.aenddate FROM (employees INNER JOIN approvals ON employees.empno=approvals.empno) INNER JOIN assignments ON employees.empno=assignments.empno

data retrieve from sql server tables like this.

empno name aposit contractno acontract
------------------------------------------------------
1234 Martin Engineer 2016 229
7890 Jones Ass Engr 2002 221
1234 Martin Engineer 2016 222
1590 Peter Sr.Engr 894 982
1234 Martin Engineer 2010 985
1590 Peter Sr.Engr 229 2016
1234 Martin Engineer 2016 3010
1590 Peter Sr.Engr 229 2016
7890 Jones Ass Engr 2002 229
4567 George Engineer 2015 222
7800 Mathew Sureyor 2017 225
...........
and so on.......


contractno means approvals.contractno
acontract means assignments.acontract

some employee are working four contracts, some are three and some are two and remaining employees working only one contract.

How can data can retreive from the above query that it count the employee that it working four contract, three contracts,
two contract and one contract etc.
data retrieve like this.

empno..name....aposit...contractno..acontract...co unt(acontract)
----------------------------------------------------------
1234...Martin...Engineer....2016....229........... ...4
1234...Martin...Engineer....2016....222
1234...Martin...Engineer....2010....985
1234...Martin...Engineer....2016....3010
1590...Peter....Sr.Engr.....894.....982........... ...3
1590...Peter....Sr.Engr.....229.....2016
1590...Peter....Sr.Engr.....229.....2016
7890...Jones....Ass Engr....2002....221..............2
7890...Jones....Ass Engr....2002....229
4567...George...Engineer....2015....222........... ...1
7800...Mathew...Sureyor.....2017....225........... ...1
...........
and so on........

How can use count clause in above query and display data like as above order ?
first display which employee working on highest number of contract ie four and so on..

regards.

Mateen


Reply With Quote
  #2 (permalink)  
Old June 9th, 2004, 10:59 AM
Registered User
 
Join Date: Aug 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try the following:

SELECT employees.empno, employees.name, approvals.contractno, assignments.acontract,assignments.aposit, assignments.astrdate, assignments.aenddate, COUNT (employees.empno) AS ContractsWorkingOn

FROM (employees INNER JOIN approvals ON employees.empno=approvals.empno) INNER JOIN assignments ON employees.empno=assignments.empno

GROUP BY employees.empno, employees.name, approvals.contractno, assignments.acontract,assignments.aposit, assignments.astrdate, assignments.aenddate

ORDER BY ContractsWorkingOn


Reply With Quote
  #3 (permalink)  
Old June 10th, 2004, 01:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: abu dbabi, , United Arab Emirates.
Posts: 518
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for your respose.

I try your query but it only display one (1) in each row.
it not count(acontract)
I use count(assignments.acontract) instead of count(employees.empno)
but it display one in each row.

problem is solve now. someone inform me the query like this.

SELECT * FROM (SELECT E.empno, E.name, A.contractno,asg.acontract,asg.aposit,asg.astrdate , asg.aenddate,
(SELECT Count(acontract) FROM assignments Where empno = E.EmpNo) as Projects
FROM (employees E INNER JOIN approvals A on E.empno=A.empno) INNER JOIN assignments ASG ON E.empno=ASG.empno ) A
ORDER BY Projects DESC

regards

Mateen



Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
new display count query carswelljr SQL Language 9 May 7th, 2007 04:59 PM
update query error in from clause prasath15@yahoo.co.in Classic ASP Databases 1 June 6th, 2005 06:02 AM
case clause in query mateenmohd SQL Server 2000 2 February 22nd, 2005 05:49 AM
query count? Raif Access 1 June 18th, 2004 05:08 PM
Syntax error in query. Incomplete query clause. dispickle ADO.NET 3 April 16th, 2004 01:04 PM



All times are GMT -4. The time now is 02:09 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.