Number Expression in Query

V

Vinnie

Hello all,

Trying to Figure how to go about adding an expression in a Query what will
Automatically Number the Results.

Example:
Query of the Employeee Table, of employees who live in NY:

John
Ralph
Jim
Doug

Would like to add a Number Field in there, so results would like this:
1 John
2 Ralph
3 Jim
4 Doug

Thanks As Usual !

Vincent
 
G

Graham R Seach

SELECT Count(EmpName) AS [Count], EmpName
FROM myTable
GROUP BY EmpName, LivesIN
HAVING LivesIn = "NY"

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
J

John Spencer

Easy to do in a report. Harder to do in a query.

In a query, you need to be able to specify an order to determine which item is
1, 2, etc.

SELECT Employees.EmployeeName,
(
SELECT Count(*)
FROM Employees as Temp
WHERE Temp.EmployeeName <= Employees.EmployeeName
AND Temp.State = Employees.State) + 1 As Counter
FROM Employees
WHERE Employees.State = "NY"
ORDER BY Employees.State, Employees.Name

Note that this will give you ties if two employees have the same name and reside
in the same state.

In a report you could skip the calculation in the query.

Group the report by State
Add a control
Control Source: =1
Running Sum: Over Group
 
J

John Spencer

small error in sample query.

SELECT Employees.EmployeeName,
(
SELECT Count(*)
FROM Employees as Temp
WHERE Temp.EmployeeName < Employees.EmployeeName
AND Temp.State = Employees.State) + 1 As Counter
FROM Employees
WHERE Employees.State = "NY"
ORDER BY Employees.State, Employees.Name
 
V

Vinnie

Thank You !

John Spencer said:
small error in sample query.

SELECT Employees.EmployeeName,
(
SELECT Count(*)
FROM Employees as Temp
WHERE Temp.EmployeeName < Employees.EmployeeName
AND Temp.State = Employees.State) + 1 As Counter
FROM Employees
WHERE Employees.State = "NY"
ORDER BY Employees.State, Employees.Name
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top