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 2000 Forums
 SQL Server Development (2000)
 count(*) question

Author  Topic 

varmasvv5
Starting Member

2 Posts

Posted - 2012-01-23 : 07:54:02
please help me in sorting out this..
how to count group by year from hire_date in an employees table the format is e.g.17-jun-87

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-01-23 : 08:01:24
[code]
DECLARE @EmployeeTable TABLE (
[EmpID] INT IDENTITY(1,1)
, [Name] VARCHAR(255)
, [HireDate] DATE
)
INSERT @EmployeeTable ([Name], [HireDate])
VALUES ('Fred', '20000101')
, ('Barney', '20000512')
, ('Fred', '20020215')
, ('Fred', '20100104')



-- Query
SELECT
DATEDIFF(YEAR, [HireDate], GETDATE()) AS [Year's Service]
, COUNT(*) AS [Employees]
FROM
@EmployeeTable
GROUP BY
DATEDIFF(YEAR, [HireDate], GETDATE())
[/code]

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-01-23 : 08:02:21
Note that this only compares the year. So there are 2 entries for 12 years (both Fred and Barney)

But you can play with it....

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

varmasvv5
Starting Member

2 Posts

Posted - 2012-01-23 : 08:10:40
sorry that didnt work..
one of the hire date in the table is 17-jun-87

there are many entries in the table as such.. so how to count the number of hires by year
i mean how to group by year
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-01-23 : 10:10:37
don't store dates as string. store them as DATES.

Then your problem will go away.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -