Solved

Access query - Get the last of each item by date

Posted on 2014-01-25
13
465 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

839 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