Solved

Access query, find highest number for unique value

Posted on 2010-11-12
17
758 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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

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.

Question has a verified solution.

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

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…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
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…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

770 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