Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access query, find highest number for unique value

Posted on 2010-11-12
17
Medium Priority
?
765 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 31

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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

721 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