Wrox Programmer Forums
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #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


  #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


  #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





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





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