Solved

Access query, find highest number for unique value

Posted on 2010-11-12
17
757 Views
Last Modified: 2012-06-21
As is, the following query finds all records in a table which have the highest value in field Mod. That works fine as is.

SELECT tbl1.fld1, tbl1.SKU, Max(tbl1.[Mod]) AS MaxOfMod, tbl1.fld4, tbl1.fld5, tbl1.fld6, tbl1.fld7, tbl1.fld8
FROM tbl1
GROUP BY tbl1.fld1, tbl1.SKU, tbl1.fld4, tbl1.fld5, tbl1.fld6, tbl1.fld7, tbl1.fld8
HAVING (((Max(tbl1.[Mod]))=(select max([Mod]) from tbl1)) AND ((tbl1.fld6)>0));

Now, in addition to those records and in the same query, I need to find the highest value in field Mod for SKU. A SKU can be in the table multiple times, and I need to see the record which has the highest Mod number.

So, the query as is finds all records that have the highest Mod number existing in field Mod. But, there are other SKU’s in the table which have as their Mod number, say, 7. That SKU may also have a record with a Mod number of 4. I just need to see the record with the Mod number of 7, in addition to all the records the query already shows.

Thanks very much.
0
Comment
Question by:pkromer
  • 9
  • 7
17 Comments
 
LVL 30

Expert Comment

by:hnasr
ID: 34125386
List few records with these 2 fields.

List expected output.
0
 

Author Comment

by:pkromer
ID: 34125453
Here's the records for one SKU... records in table
0
 

Author Comment

by:pkromer
ID: 34125454
Here's what the query needs to output... record from query
0
 

Author Comment

by:pkromer
ID: 34125459
Notice that there is a date also... if it makes it simpler, the query could look up the highest date for each SKU, then output a list of unique SKU's with thier highest dates. That would give me the result that i need.
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 34125488
try this query


select * from tbl1
where [mod]=(select max([mod]) from tbl1







0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 34125494
oops missed the  ")"


select * from tbl1
where [mod]=(select max([mod]) from tbl1)







0
 

Author Comment

by:pkromer
ID: 34125510
That has the same result as the query in my original post, which only finds all records with the highest mod number.

I guess at this point it may be easiest to just find the most recent date for each SKU, and forget the Mod entirely. That's the same result and maybe easier to achieve.
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 34125517



select * from tbl1
where [fld4]=(select max([fld4]) from tbl1)



see if that is what you are after
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:pkromer
ID: 34125544
That's still just giving me all the records with highest date (which is the same thing as highest mod, 12). what i need is to find the highest date for each SKU.

This most recent query returns 602 records, but there are 1217 that Im looking for. The missing ones have a Mod which is less than 12, like 7 or 4 or whatever. But for every SKU in the table, there is a max Date/Mod (same thing). I need to see the max Date/Mod for every single SKU in the table.
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 34125549


try this one



SELECT T.*
FROM tbl1 AS T
Inner Join
(SELECT T2.SKU, Max(T2.fld4) AS MaxOffld4
FROM tbl1 AS T2
GROUP BY T2.SKU) As T3
On T.fld4=T3.MaxOffld4 and T.SKU=T3.SkU



0
 

Author Comment

by:pkromer
ID: 34125552
btw... sorry this is so scattered. I should have left the original query out of it, AND realized that the date field tells us the same thing as the mod field. Maybe that doesnt matter.
0
 

Author Comment

by:pkromer
ID: 34125573
:-) Very close, but part of the original query had AND ((tbl1.fld6)>0));

can you put that in there? its a price field and i want to ignore the records with no price.
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 34125575


try this one


SELECT
    T.SKU, T.Mod, T.Fld4
FROM
    tbl1  AS T
 INNER JOIN
       (SELECT
             T2.SKU, T2.Mod, Max(T2.Fld4) As MaxFld4
         FROM
             tbl1  T2  
         INNER JOIN
                (SELECT T3.SKU, Max(T3.Mod) As MaxMod
                  FROM tbl1  T3
                  GROUP BY T3.SKU
                ) T4
           ON T2.SKU = T4.SKU AND T2.Mod = T4.MaxMod
         GROUP BY T2.SKU, T2.Mod
        ) T1
ON T.Fld4 = T1.MaxFld4  And T.SKU=T1.SKU;




if that does not show what you want to see,
upload a sample db with the table



0
 

Author Comment

by:pkromer
ID: 34125587
ID: 34125549 gave me what i need aside from having that >0 price check i just described, can you add that to ID: 34125549?.
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 34125595



SELECT T.*
FROM tbl1 AS T
Inner Join
(SELECT T2.SKU, Max(T2.fld4) AS MaxOffld4
FROM tbl1 AS T2
GROUP BY T2.SKU) As T3
On T.fld4=T3.MaxOffld4 and T.SKU=T3.SkU
Where T.Fld6>0

0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 34125600


 i think this is better



SELECT
    T.*
FROM
    tbl1  AS T
 INNER JOIN
       (SELECT
             T2.SKU, T2.Mod, Max(T2.Fld4) As MaxFld4
         FROM
             tbl1  T2  
         INNER JOIN
                (SELECT T3.SKU, Max(T3.Mod) As MaxMod
                  FROM tbl1  T3
                  GROUP BY T3.SKU
                ) T4
           ON T2.SKU = T4.SKU AND T2.Mod = T4.MaxMod
         GROUP BY T2.SKU, T2.Mod
        ) T1
ON T.Fld4 = T1.MaxFld4  And T.SKU=T1.SKU
Where T.fld6>0

0
 

Author Comment

by:pkromer
ID: 34125616
with that last one i got a query input box popup asking me for a value of Fld4. ID: 34125595 did the trick. What's better about that last one?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

867 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

19 Experts available now in Live!

Get 1:1 Help Now