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: -
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?
Microsoft AccessMicrosoft SQL Server
Last Comment
HKFuey
8/22/2022 - Mon
Cluskitt
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
Rey Obrero (Capricorn1)
select SalesOrder,AmmendDate, EnteredBy
from tableX
Where AmmendDate=(select min([AmmendDate]) from tableX As t where t.SalesOrder=tableX.SalesOrder)
Cluskitt
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.
SELECT SalesOrder, EnteredBy, Min(AmendDate) as FirstEntry
FROM MyTable
GROUP BY SalesOrder, EnteredBy;
Rey Obrero (Capricorn1)
that will work as long as the Group By will return single record per SalesOrder when you add more fields to the query
GRayL
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.
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.
HKFuey
ASKER
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
Cluskitt
Don't use design view. Use the SQL view and paste it there.
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
Cluskitt
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.
Rey Obrero (Capricorn1)
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)));
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)))
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?
HKFuey
ASKER
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.
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.Firs