Solved

Access query - Get the last of each item by date

Posted on 2014-01-25
13
466 Views
Last Modified: 2014-01-26
Hello all

In a query, I need to pull all of the last records of every itemID that is ActiveYN = Yes.

So if my table [t_Dtail] is

DetailID,  tdate, ItemID, ActiveYN

how do I do that?

If I do it as

SELECT * From t_dtail WHERE ActiveYN = True
I know I get everything that's true.

So if my table has 50 ItemID # 20 How do I get just the Last True of all the ItemID (not just number 20's??????

Tried
SELECT * From t_dtail WHERE ActiveYN = True HAVING ItemID= DMax(ItemID, t_Dtail,ItemID)

But that doesn't get it either
????????
0
Comment
Question by:wlwebb
  • 7
  • 5
13 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39809330
try this

select a.*
from t_dtail as a
inner join
(select max(b.tdate) as maxtdate, b.detailId
 from t_dtail as b
 group by b.detailId
having b.ActiveYN= true
) as c
on a.detailId = c.detailId  and a.tdate=c.maxtdate
0
 

Author Comment

by:wlwebb
ID: 39809497
I keep getting
Your query does not include the specified expression 'itemid' as part of an aggregate function
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39809546
try this


select a.*
from t_dtail as a
inner join
(select max(b.tdate) as maxtdate, b.itemid
 from t_dtail as b
 group by b.itemid
having b.ActiveYN= true
) as c
on a.itemid = c.itemid  and a.tdate=c.maxtdate
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:wlwebb
ID: 39809551
....your query expression does not ...... 'b.ActIveYN=-1' as part of an aggregate function
0
 

Author Comment

by:wlwebb
ID: 39809553
Tried True before -1
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39809559
sorry, try this


select a.*
from t_dtail as a
inner join
(select max(b.tdate) as maxtdate, b.itemid
 from t_dtail as b
 group by b.itemid,b.ActiveYN
having b.ActiveYN= true
) as c
on a.itemid = c.itemid  and a.tdate=c.maxtdate
0
 
LVL 10

Accepted Solution

by:
Gozreh earned 500 total points
ID: 39809648
Why not simple as

SELECT t_Dtail.ItemID, Max(t_Dtail.tdate) AS MaxDate
FROM t_Dtail
WHERE (((t_Dtail.ActiveYN)=True))
GROUP BY t_Dtail.ItemID;
0
 

Author Closing Comment

by:wlwebb
ID: 39809891
Thanks for the input!
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39810062
@ wlwebb

did you even try my post at http:#a39809559 ?


.
0
 

Author Comment

by:wlwebb
ID: 39810071
Cap 1....

I ALWAYS ALWAYS try anything you post first!  History has taught me that your posts always work 99.999999% of the time

Either I was typing something wrong (couldn't copy and paste last night -no connection for laptop- where I was.... I was reading on my phone and trying to type it into my laptop.  I will give your's a whirl again today.


PS.... The 0.0000001% of the time it doesn't work is because my table isn't set up right to allow what I want to do to work............
0
 

Author Comment

by:wlwebb
ID: 39810252
Cap 1 ....  I tried the last suggestion.......

select a.*
from t_dtail as a
inner join
(select max(b.tdate) as maxtdate, b.itemid
 from t_dtail as b
 group by b.itemid,b.ActiveYN
having b.ActiveYN= true
) as c
on a.itemid = c.itemid  and a.tdate=c.maxtdate


It produces the error

You tried to execute a query that does not include the specified expresion 'b.Active YN=-1' as part of an aggregate function.

The code above is a cut/paste from my sql view..........
????????????

*******************
NEVER MIND... I SEE I USED THE WRONG ONE NOW>>>>
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39810348
so did you get it working?
0
 

Author Comment

by:wlwebb
ID: 39810397
Cap1
Yes. .... Once I used the right one....So again... That bad was on me!

Like I said above ... I've seen enough here to know you are one of the best on here ....  You, MBiz and Irog and LSM always shoot straight with great pointers.  Others on here are great too ... Not discrediting them in any way ... Just know when one of the above post usually it always works (unless I screw up the typing or misinterpret something)

THANK YOU!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

685 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