Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Simple COUNT

Author  Topic 

FernandoLorival
Starting Member

19 Posts

Posted - 2008-07-01 : 17:10:50
Hi all,
I need to determine the number of days that an employee worked.
For that I rely on the production database that gives me the production for that employee during a period of time.
For that i have :

SELECT CONVERT(Varchar,EventStart,101) AS DATE
FROM [Ontegra-Production].[dbo].[tbl_Machine_Events_History]
WHERE Shift = '2'
AND CONVERT(Varchar,EventStart,101) >= '06/20/2008'
AND CONVERT(Varchar,EventEnd,101) <= '06/30/2008'
GROUP BY CONVERT(Varchar,EventStart,101)
ORDER BY CONVERT(Varchar,EventStart,101) ASC

The result is:

Date
-------
06/20/2008
06/21/2008
06/22/2008
06/23/2008
06/25/2008

So I know that he worked 5 days!
Now I need to multiply by the number of hours on that shift...
The problem is I can't retrive that number (5)!!!!!!
That must be very easy but I cant find a way of counting those days... Dawww

Thank you for the help.

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-07-01 : 17:13:39

SELECT COUNT(CONVERT(Varchar,EventStart,101)) AS DATE
FROM [Ontegra-Production].[dbo].[tbl_Machine_Events_History]
WHERE Shift = '2'
AND CONVERT(Varchar,EventStart,101) >= '06/20/2008'
AND CONVERT(Varchar,EventEnd,101) <= '06/30/2008'
GROUP BY CONVERT(Varchar,EventStart,101)
ORDER BY CONVERT(Varchar,EventStart,101) ASC

Also dont convert dates to varchars

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

FernandoLorival
Starting Member

19 Posts

Posted - 2008-07-01 : 17:25:52
Hi madhivanan,
Thank you for your post...
The way you have the query it will count the number of times the same date shows in the GROUP BY Clause not the number of times in the final result.

Here is the result of that:

DATE
------
10
13
7
25
15

I just need a simple number.... 5
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-07-02 : 15:21:19
SELECT COUNT(DATE) as counting from
(
SELECT CONVERT(Varchar,EventStart,101) AS DATE
FROM [Ontegra-Production].[dbo].[tbl_Machine_Events_History]
WHERE Shift = '2'
AND CONVERT(Varchar,EventStart,101) >= '06/20/2008'
AND CONVERT(Varchar,EventEnd,101) <= '06/30/2008'
GROUP BY CONVERT(Varchar,EventStart,101)
) as t

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

FernandoLorival
Starting Member

19 Posts

Posted - 2008-07-02 : 16:00:16
Excellent...

Thank you Madhivanan!
Go to Top of Page
   

- Advertisement -