![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
| SELECT ForeignID, Last(StatusID) AS LastStatus, Last(StartTime) AS |
#3
| |||
| |||
|
|
I have a totals query that is giving me incorrect results. The query is SELECT ForeignID, Last(StatusID) AS LastStatus, Last(StartTime) AS LastStart, Last(EndTime) AS LastEnd, Last(CounterId) AS LastCounter FROM BuildDeviceDetail GROUP BY ForeignID; CounterID is the autonumber field. ForeignID the detailID. The counterID field might be 401, 402, 403, 404. Running the query the LastCounter value will be 403. The LastStatus, LastStart, and LastEnd are for the 403 record. I'm guessing you are looking for information from the last StatusID for each |
#4
| |||
| |||
|
|
I have a totals query that is giving me incorrect results. The query is SELECT ForeignID, Last(StatusID) AS LastStatus, Last(StartTime) AS LastStart, Last(EndTime) AS LastEnd, Last(CounterId) AS LastCounter FROM BuildDeviceDetail GROUP BY ForeignID; CounterID is the autonumber field. ForeignID the detailID. The counterID field might be 401, 402, 403, 404. Running the query the LastCounter value will be 403. The LastStatus, LastStart, and LastEnd are for the 403 record. This is the second time this problem has occured in the past week. To correct, I've gone to the backend, compact/repaired it, and the query works correctly. I am running the application using A2003 with SP3. The other users are all using A2007. I don't have A2007 on the computer I've been assigned. Am I using the Last() function incorrectly? If so, since it works most of the time I am wondering if some folks are fully up-to-date on the service releases for A2007 and others may not be fully up-to-date and that has created corruption. Any advice would be appreciated. |
#5
| |||
| |||
|
|
SELECT ForeignID, Last(StatusID) AS LastStatus, Last(StartTime) AS LastStart, Last(EndTime) AS LastEnd, Last(CounterId) AS LastCounter FROM BuildDeviceDetail GROUP BY ForeignID; Greetings, Here is my interpretation of the query above: Select ForeignID, 'constVal' as LastStatus, 'constVal' as LastStart, 'constVal' As LastEnd, 'constVal' As LastCounter From BuildDeviceDetail Group By ForeignID For every field that has a "Last" function - you will always get the same value for that field. I say to try setting these values to some Vars like Dim LastStatus As String (whatever) LastStatus = DLookUp("StatusID", "BuildDeviceDetail", "StatusID = (select Last(StatusID) from BuilDeviceDetail)") .. Now rewrite the query as Select ForeignID, '" & LastStatus & "' As LastStatus, '" & ... & "', ... From BuilDeviceDetail Group By ForeignID Just a thought - but honestly, the query - at least as it is described in your post - is not very comprehendible what it is supposed to return. Rich *** Sent via Developersdex http://www.developersdex.com *** |
#6
| |||
| |||
|
|
Salad wrote: I have a totals query that is giving me incorrect results. The query is SELECT ForeignID, Last(StatusID) AS LastStatus, Last(StartTime) AS LastStart, Last(EndTime) AS LastEnd, Last(CounterId) AS LastCounter FROM BuildDeviceDetail GROUP BY ForeignID; CounterID is the autonumber field. ForeignID the detailID. The counterID field might be 401, 402, 403, 404. Running the query the LastCounter value will be 403. The LastStatus, LastStart, and LastEnd are for the 403 record. This is the second time this problem has occured in the past week. To correct, I've gone to the backend, compact/repaired it, and the query works correctly. I am running the application using A2003 with SP3. The other users are all using A2007. I don't have A2007 on the computer I've been assigned. Am I using the Last() function incorrectly? Here is what Microsoft says about the First and Last functions: http://msdn.microsoft.com/en-us/libr...ice.12%29.aspx "They simply return the value of a specified field in the first or last record, respectively, of the result set returned by a query. Because records are usually returned in no particular order (unless the query includes an ORDER BY clause), the records returned by these functions will be arbitrary." So you should probably try an ORDER BY in your query. However, I gave up on First and Last because they aren't supported in SQL Server, and I've been trying to design Access applications so that they may later be upsized with minimum fuss. Don't know whether that is a concern for you. But in your case, think I would use Max(CounterID) instead of Last. It's interesting that compact cures your ordering problems at least temporarily. Supposedly records are written to disk in primary key order during compact. However, that ordering is not maintained with table changes ... it only happens again at the next compact. I wouldn't depend on it to enforce your desired order; use ORDER BY instead. |
#7
| |||
| |||
|
|
I have a totals query that is giving me incorrect results. The query is SELECT ForeignID, Last(StatusID) AS LastStatus, Last(StartTime) AS LastStart, Last(EndTime) AS LastEnd, Last(CounterId) AS LastCounter FROM BuildDeviceDetail GROUP BY ForeignID; CounterID is the autonumber field. ForeignID the detailID. The counterID field might be 401, 402, 403, 404. Running the query the LastCounter value will be 403. The LastStatus, LastStart, and LastEnd are for the 403 record. This is the second time this problem has occured in the past week. To correct, I've gone to the backend, compact/repaired it, and the query works correctly. I am running the application using A2003 with SP3. The other users are all using A2007. I don't have A2007 on the computer I've been assigned. Am I using the Last() function incorrectly? |
#8
| |||
| |||
|
| For an example, let's say an order has 4 items on it. I want to group on |
#9
| |||
| |||
|
|
Hans Up wrote: Salad wrote: I have a totals query that is giving me incorrect results. The query is SELECT ForeignID, Last(StatusID) AS LastStatus, Last(StartTime) AS LastStart, Last(EndTime) AS LastEnd, Last(CounterId) AS LastCounter FROM BuildDeviceDetail GROUP BY ForeignID; CounterID is the autonumber field. ForeignID the detailID. The counterID field might be 401, 402, 403, 404. Running the query the LastCounter value will be 403. The LastStatus, LastStart, and LastEnd are for the 403 record. This is the second time this problem has occured in the past week. To correct, I've gone to the backend, compact/repaired it, and the query works correctly. I am running the application using A2003 with SP3. The other users are all using A2007. I don't have A2007 on the computer I've been assigned. Am I using the Last() function incorrectly? Here is what Microsoft says about the First and Last functions: http://msdn.microsoft.com/en-us/libr...ice.12%29.aspx "They simply return the value of a specified field in the first or last record, respectively, of the result set returned by a query. Because records are usually returned in no particular order (unless the query includes an ORDER BY clause), the records returned by these functions will be arbitrary." So you should probably try an ORDER BY in your query. However, I gave up on First and Last because they aren't supported in SQL Server, and I've been trying to design Access applications so that they may later be upsized with minimum fuss. Don't know whether that is a concern for you. But in your case, think I would use Max(CounterID) instead of Last. It's interesting that compact cures your ordering problems at least temporarily. Supposedly records are written to disk in primary key order during compact. However, that ordering is not maintained with table changes ... it only happens again at the next compact. I wouldn't depend on it to enforce your desired order; use ORDER BY instead. I agree with what you write. BTW, thanks for the link. In the end I decided to create 2 queries. The first got the max id per unit from the table. Then I created a query that joined the maxid to the table id. It's quick and it will be accurate. |
![]() |
| Thread Tools | |
| Display Modes | |
| |