Solved

Access query, find highest number for unique value

Posted on 2010-11-12
17
756 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

760 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

20 Experts available now in Live!

Get 1:1 Help Now