dbaspot
Tags Register FAQ Calendar Search Today's Posts Mark Forums Read

Selecting Row with Maximum? - sqlserver-datamining

This is a discussion on Selecting Row with Maximum? - sqlserver-datamining ; I've got this simple query: SELECT EmpName, EmpBadge FROM Employees WHERE EmpName=@EmpName1 Occasionally, there are duplicates (an employee is terminated, then hired again at a later date). Because of this, we have added an ID field that is automatically incremented ...


Home > Database Forum > Data Warehousing > sqlserver-datamining > Selecting Row with Maximum?

Reply

 

LinkBack Thread Tools Display Modes
  #1  
Old 11-11-2008, 01:23 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Selecting Row with Maximum?

I've got this simple query:

SELECT EmpName, EmpBadge
FROM Employees
WHERE EmpName=@EmpName1

Occasionally, there are duplicates (an employee is terminated, then hired
again at a later date). Because of this, we have added an ID field that is
automatically incremented whenever a new employee is added.

To prevent duplicates, I need to modify the query above to something like
this code below that does NOT work as it is written:

SELECT EmpName, EmpBadge
FROM Employees
WHERE (EmpName=@EmpName1) AND (Max(ID))

Obviously, the Max(ID) parameter is causing the failure.

I do not want the ID returned as part of the query, or I will need to write
another query to filter the ID out of the results.

So, how exactly should this query be written?
Reply With Quote
  #2  
Old 11-13-2008, 11:08 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Selecting Row with Maximum?

Something like:
SELECT EmpName, EmpBadge
FROM Employees
WHERE (EmpName=@EmpName1) AND id = (select Max(ID) from Employees)


"jp2msft" wrote in message
news836DEE9-807C-4E27-B42B-D587606C1F00@microsoft.com...
> I've got this simple query:
>
> SELECT EmpName, EmpBadge
> FROM Employees
> WHERE EmpName=@EmpName1
>
> Occasionally, there are duplicates (an employee is terminated, then hired
> again at a later date). Because of this, we have added an ID field that is
> automatically incremented whenever a new employee is added.
>
> To prevent duplicates, I need to modify the query above to something like
> this code below that does NOT work as it is written:
>
> SELECT EmpName, EmpBadge
> FROM Employees
> WHERE (EmpName=@EmpName1) AND (Max(ID))
>
> Obviously, the Max(ID) parameter is causing the failure.
>
> I do not want the ID returned as part of the query, or I will need to
> write
> another query to filter the ID out of the results.
>
> So, how exactly should this query be written?



Reply With Quote
  #3  
Old 11-14-2008, 04:15 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Selecting Row with Maximum?

Hello.
A where clause in the SubSelec?
Like this:

> SELECT EmpName, EmpBadge
> FROM Employees
> WHERE (EmpName=@EmpName1) AND id = (select Max(ID) from Employees

WHERE (EmpName=@EmpName1)
)
--
Ricardo Junquera
Consultor Business Intelligence

BG&S Online Consultores
Ganadora del Premio Microsoft Business Awards 2008.
Partner de Soluciones : Satisfacción de Cliente.



"Joe H" wrote:

> Something like:
> SELECT EmpName, EmpBadge
> FROM Employees
> WHERE (EmpName=@EmpName1) AND id = (select Max(ID) from Employees)
>
>
> "jp2msft" wrote in message
> news836DEE9-807C-4E27-B42B-D587606C1F00@microsoft.com...
> > I've got this simple query:
> >
> > SELECT EmpName, EmpBadge
> > FROM Employees
> > WHERE EmpName=@EmpName1
> >
> > Occasionally, there are duplicates (an employee is terminated, then hired
> > again at a later date). Because of this, we have added an ID field that is
> > automatically incremented whenever a new employee is added.
> >
> > To prevent duplicates, I need to modify the query above to something like
> > this code below that does NOT work as it is written:
> >
> > SELECT EmpName, EmpBadge
> > FROM Employees
> > WHERE (EmpName=@EmpName1) AND (Max(ID))
> >
> > Obviously, the Max(ID) parameter is causing the failure.
> >
> > I do not want the ID returned as part of the query, or I will need to
> > write
> > another query to filter the ID out of the results.
> >
> > So, how exactly should this query be written?

>
>
>

Reply With Quote
Reply

Thread Tools
Display Modes



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