Solved

Access query, find highest number for unique value

Posted on 2010-11-12
17
761 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34125488
try this query


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







0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34125517



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



see if that is what you are after
0
 

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 120

Expert Comment

by:Rey Obrero (Capricorn1)
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) 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 120

Expert Comment

by:Rey Obrero (Capricorn1)
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

Industry Leaders: 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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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…

627 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