[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

rodchar

3/19/2007 7:47:00 PM

hey all,
I have the following table in which i need to get a count, percentage,
average salary.

EmpID,Salary,Salary Date,Gender
1,1.00,1/1/07,M
1,2.00,3/1/07,M
2,1.00,3/1/07,F
3,1.00,3/1/07,M

The problem i have is the employee that has 2 salary records. i only need
the most recent salary record for an employee to get a count of
males/females, percentage, avg salary. what's the best way to get the
information i need without including that 1 duplicate record?

thanks,
rodchar
4 Answers

Hugo Kornelis

3/19/2007 7:56:00 PM

0

On Mon, 19 Mar 2007 12:47:03 -0700, rodchar wrote:

>hey all,
>I have the following table in which i need to get a count, percentage,
>average salary.
>
>EmpID,Salary,Salary Date,Gender
>1,1.00,1/1/07,M
>1,2.00,3/1/07,M
>2,1.00,3/1/07,F
>3,1.00,3/1/07,M
>
>The problem i have is the employee that has 2 salary records. i only need
>the most recent salary record for an employee to get a count of
>males/females, percentage, avg salary. what's the best way to get the
>information i need without including that 1 duplicate record?
>
>thanks,
>rodchar

Hi rodchar,

Sounds like school work to me, so instead of a full solution, I'll get
you some hints.

First, you need to exclude the rows you are not interested in. The rows
you need to exclude are those that are not the most recent for an
employee. Think about what is always in the table for rows that are not
the most recent, and never for rows that are the most recent.

After excluding the rows you don't need, you form groups and compute
averages as per the problem description. I assume that this is where you
deploy the knowledge of the chapter covered in the last lesson.

If this is not homework, then I apologize for misinterpreting you. I
hope you're still able to figure it out based on the hints above.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hug...

Alejandro Mesa

3/19/2007 7:57:00 PM

0

Try:

select
Gender,
count(*) as cnt,
avg(salary) as avg_salary
from
dbo.t1 as a
where
[Salary Date] = (select top 1 b.[Salary Date] from dbo.t1 as b where
b.EmpID = a.EmpID order by b.[Salary Date] desc)
group by
Gender
go


AMB


"rodchar" wrote:

> hey all,
> I have the following table in which i need to get a count, percentage,
> average salary.
>
> EmpID,Salary,Salary Date,Gender
> 1,1.00,1/1/07,M
> 1,2.00,3/1/07,M
> 2,1.00,3/1/07,F
> 3,1.00,3/1/07,M
>
> The problem i have is the employee that has 2 salary records. i only need
> the most recent salary record for an employee to get a count of
> males/females, percentage, avg salary. what's the best way to get the
> information i need without including that 1 duplicate record?
>
> thanks,
> rodchar

Alejandro Mesa

3/19/2007 8:03:00 PM

0

For SQL Server 2005, you can try:

;with cte
as
(
select
Gender,
Salary,
row_number() over(partition by EmpID order by [Salary Date] desc) as rn
from
dbo.t1
)
select
Gender,
count(*) as cnt,
avg(Salary) as avg_Salary
from
cte
where
rn = 1
group by
Gender;


AMB

"Alejandro Mesa" wrote:

> Try:
>
> select
> Gender,
> count(*) as cnt,
> avg(salary) as avg_salary
> from
> dbo.t1 as a
> where
> [Salary Date] = (select top 1 b.[Salary Date] from dbo.t1 as b where
> b.EmpID = a.EmpID order by b.[Salary Date] desc)
> group by
> Gender
> go
>
>
> AMB
>
>
> "rodchar" wrote:
>
> > hey all,
> > I have the following table in which i need to get a count, percentage,
> > average salary.
> >
> > EmpID,Salary,Salary Date,Gender
> > 1,1.00,1/1/07,M
> > 1,2.00,3/1/07,M
> > 2,1.00,3/1/07,F
> > 3,1.00,3/1/07,M
> >
> > The problem i have is the employee that has 2 salary records. i only need
> > the most recent salary record for an employee to get a count of
> > males/females, percentage, avg salary. what's the best way to get the
> > information i need without including that 1 duplicate record?
> >
> > thanks,
> > rodchar

rodchar

3/19/2007 8:32:00 PM

0

thanks all for the generous help on my work issue. i appreciate it very much.

"rodchar" wrote:

> hey all,
> I have the following table in which i need to get a count, percentage,
> average salary.
>
> EmpID,Salary,Salary Date,Gender
> 1,1.00,1/1/07,M
> 1,2.00,3/1/07,M
> 2,1.00,3/1/07,F
> 3,1.00,3/1/07,M
>
> The problem i have is the employee that has 2 salary records. i only need
> the most recent salary record for an employee to get a count of
> males/females, percentage, avg salary. what's the best way to get the
> information i need without including that 1 duplicate record?
>
> thanks,
> rodchar