Discussion:
Selecting Row with Maximum?
(too old to reply)
jp2msft
2008-11-11 17:23:01 UTC
Permalink
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?
Joe H
2008-11-13 15:08:03 UTC
Permalink
Something like:
SELECT EmpName, EmpBadge
FROM Employees
Post by jp2msft
SELECT EmpName, EmpBadge
FROM Employees
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
SELECT EmpName, EmpBadge
FROM Employees
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?
Ricardo Junquera
2008-11-14 08:15:01 UTC
Permalink
Hello.
A where clause in the SubSelec?
Post by jp2msft
SELECT EmpName, EmpBadge
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.
Post by jp2msft
SELECT EmpName, EmpBadge
FROM Employees
Post by jp2msft
SELECT EmpName, EmpBadge
FROM Employees
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
SELECT EmpName, EmpBadge
FROM Employees
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?
unknown
2008-11-24 07:33:11 UTC
Permalink
If you already have the field MaxId in your table then make sure its an auto increment one.Using your query pick the Maximum from MaxId.
Loading...