Solved

Access query not returning expected data

Posted on 2012-03-19
23
347 Views
Last Modified: 2012-06-27
I have this data:
SalesOrder      AmmendDate                      EnteredBy
381894          19/03/2012 11:00:01      JS  
381894          19/03/2012 11:00:02      JS  
381894          19/03/2012 11:24:56      JS  
381894          19/03/2012 11:25:02      PW  
381894          19/03/2012 11:25:02      PW  
381894          19/03/2012 14:24:57      PW  

The AmendedDate column is Date/Time

I want to return the user (EnteredBy Field) that put the order on, So I group by Order and display firstof AmmendedDate and firstof Enteredby which returns: -

SalesOrder      FirstOfAmmendDate      EnteredBy
381894          19/03/2012 14:24:57      PW  

This is incorrect as I want to find the user who entered the order which is 'JS' on 19/03/2012 11:00:01

Can anyone help?
0
Comment
Question by:HKFuey
  • 7
  • 6
  • 5
  • +2
23 Comments
 
LVL 18

Expert Comment

by:Cluskitt
Comment Utility
SELECT m.SalesOrder, m.FirstOfAmmendDate, m.EnteredBy
FROM MyTable m INNER JOIN
(SELECT SalesOrder, MIN(FirstOfAmmendDate) FirstOfAmmendDate FROM MyTable GROUP BY SalesOrder) t ON m.SalesOrder=t.SalesOrder AND m.FirstOfAmmendDate=t.FirstOfAmmendDate
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
select SalesOrder,AmmendDate, EnteredBy
from tableX
Where AmmendDate=(select min([AmmendDate]) from tableX As t where t.SalesOrder=tableX.SalesOrder)
0
 
LVL 18

Expert Comment

by:Cluskitt
Comment Utility
I usually try to avoid tables in the where clause. I used to be a big fan of them, especially with EXISTS, or IN, but the problem is that the table is constructed for each record, as opposed to being constructed once then joined.
Also, your query will repeat records (he was grouping by SalesOrder). He only wants the first for each SalesOrder. A SELECT DISTINCT will solve it but is, imho, inefficient.
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
@cluskitt
are you referring to the query i posted?
i suggest that you try it first before making any negative comments.
0
 
LVL 18

Expert Comment

by:Cluskitt
Comment Utility
I apologize. I misread your solution. It will return the desired records as well. Sorry.
0
 
LVL 44

Expert Comment

by:GRayL
Comment Utility
Or:

SELECT SalesOrder, EnteredBy, Min(AmendDate)
FROM MyTable
GROUP BY SalesOrder, EnteredBy;
0
 
LVL 44

Expert Comment

by:GRayL
Comment Utility
For completeness:

SELECT SalesOrder, EnteredBy, Min(AmendDate) as FirstEntry
FROM MyTable
GROUP BY SalesOrder, EnteredBy;
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
that will work as long as the Group By will return single record per SalesOrder when you add more fields to the query
0
 
LVL 44

Expert Comment

by:GRayL
Comment Utility
It will return a single record per sales order/entered by as long as there are no duplicates in the date/time field for that combination.  BTW, we should be answering what is asked, not conjecturing about adding more fields at a later date.
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
it is very common that the real query have more fields than what was given as an example.

the query i posted at http:#a37738734  can handle that, even if you include all the fields from the table. i.e.,

select * from
tablex
Where AmmendDate=(select min([AmmendDate]) from tableX As t where t.SalesOrder=tableX.SalesOrder)
0
 
LVL 44

Expert Comment

by:GRayL
Comment Utility
Slow tho
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 18

Expert Comment

by:Cluskitt
Comment Utility
My query will address will address the original question and is, I think, efficient. It can work with however many fields you want to use.
0
 

Author Comment

by:HKFuey
Comment Utility
Thanks for all the input.

I tried suggestion 37738734 by Capricorn1: -

SELECT MyTable.SalesOrder, MyTable.Operator, MyTable.AmmendDate
FROM MyTable
WHERE (((MyTable.SalesOrder)="99999") AND ((MyTable.AmmendDate)=(select min([AmmendDate]) from MyTable As t )));

Not sure my syntax is right as this returns nothing, now trying first response from Cluskit.
0
 

Author Comment

by:HKFuey
Comment Utility
Suggestion from Cluskit gives "can't represent the join expression m.SalesOrder=t.SalesOrder in design view: -

SELECT m.SalesOrder, m.FirstOfAmmendDate, m.EnteredBy
FROM MyTable m INNER JOIN
(SELECT SalesOrder, MIN(FirstOfAmmendDate) FirstOfAmmendDate FROM MyTable GROUP BY SalesOrder) t ON m.SalesOrder=t.SalesOrder AND m.FirstOfAmmendDate=t.FirstOfAmmendDate
0
 
LVL 18

Expert Comment

by:Cluskitt
Comment Utility
Don't use design view. Use the SQL view and paste it there.
0
 

Author Comment

by:HKFuey
Comment Utility
OK I have done this now and it works, not very elegant though: -

Select Order and Min of date, save as Query1

Add new Query2 and join Min of date and order to fields in my table
0
 
LVL 18

Expert Comment

by:Cluskitt
Comment Utility
It's pretty much the same. What you have to do is create a temporary table (or view), then link it with the actual table. Whether you do it on Access or SQL, with a JOIN or in the WHERE clause, a temporary table is always required.
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
test this one


SELECT MyTable.SalesOrder, MyTable.Operator, MyTable.AmmendDate
FROM MyTable
WHERE (((MyTable.SalesOrder)="99999") AND ((MyTable.AmmendDate)=(select min([AmmendDate]) from MyTable As t where t.SalesOrder=MyTable.SalesOrder)));
0
 

Author Comment

by:HKFuey
Comment Utility
Access still giving errors, thinking of going to a SQL view and starting a new question will give points later.

SELECT     SalesOrder, Operator, AmmendDate
FROM         dbo.MyTable
WHERE     (SalesOrder = '999999') AND (AmmendDate =
                          (SELECT     MIN(dbo.MyTable.AmmendDate) AS Expr1
                            FROM          dbo.MyTable AS t
                           Where      (dbo.MyTable.SalesOrder = dbo.MyTable.SalesOrder)))
0
 
LVL 18

Accepted Solution

by:
Cluskitt earned 300 total points
Comment Utility
As long as you create a view with:
SELECT SalesOrder So, MIN(FirstOfAmmendDate) Dt FROM MyTable GROUP BY SalesOrder
You can then create a second view that includes both the table and the first view. Join them both and you have your results. You can do this in design mode, if you want.
0
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 200 total points
Comment Utility
SELECT     SalesOrder, Operator, AmmendDate
FROM         dbo.MyTable
WHERE     (SalesOrder = '999999') AND (AmmendDate =
                          (SELECT     MIN(dbo.MyTable.AmmendDate) AS Expr1
                            FROM          dbo.MyTable AS t
                           Where      (t.SalesOrder = dbo.MyTable.SalesOrder)))
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Tags:access 2007 sql 2000
Topics: Microsoft Access Database, MS SQL Server

You posted this in the MS SQL Server topic area with a "sql 2000" tag, were you looking for T-SQL solution?
0
 

Author Closing Comment

by:HKFuey
Comment Utility
Capricorn1, I tried your final suggestion and I get error "An aggregate may not appear in the Where clause...

Thanks for trying to help, I need to go do some work now!!

In the end I just created 2 views as I suggested earlier in the thread.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now