473,503 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Select based on field comparison

303 Recognized Expert Contributor
I am trying to select all table data with a restriction based on a comparison of fields within the table. I'm not sure how to describe the query very well, so I'll give an example of what I need. The table setup is similar to the following:
Expand|Select|Wrap|Line Numbers
  1. Number     Version
  2. item1        1
  3. item1        2
  4. item2        1
  5. item2        2
  6. item2        3
  7.  
I need my query to return item1 version 2 and item2 version 3 (the record for each item with the largest version number). Can anyone help?
Dec 10 '07 #1
8 1400
NeoPa
32,557 Recognized Expert Moderator MVP
It seems you need to GROUP BY the first field then return the Max() of the second.

Something like :
Expand|Select|Wrap|Line Numbers
  1. SELECT [Number], Max([Version]) AS MaxVer
  2. FROM [YourTable]
  3. GROUP BY [Number]
Dec 10 '07 #2
improvcornartist
303 Recognized Expert Contributor
Thanks. Now I need to add a third field. What I need in the end is Select Field1, Field2, Field3 From Table Where Field2 is the maximum value of Field2 in relation to Field1.
Expand|Select|Wrap|Line Numbers
  1. Number   Version   Qty
  2. item1      1       3
  3. item1      2       6
  4.  
I have to be able to get the specific Qty for the largest Version for each item in the table. This is what's giving me the most trouble.
Dec 10 '07 #3
improvcornartist
303 Recognized Expert Contributor
I can get the data I need by joining the table on select statements:
Expand|Select|Wrap|Line Numbers
  1. Select T2.Number, T2.Qty From (Select Number, Max(Version) as V From Table Group By Number) as T1, (Select Number, Version, Qty From Table) as T2 Where T1.Number = T2.Number and T1.V = T2.Version
  2.  
But is this really the best way to get the data? I also need to tie in 3 other tables to get everything I need.
Dec 10 '07 #4
NeoPa
32,557 Recognized Expert Moderator MVP
Thanks. Now I need to add a third field. What I need in the end is Select Field1, Field2, Field3 From Table Where Field2 is the maximum value of Field2 in relation to Field1.
Expand|Select|Wrap|Line Numbers
  1. Number   Version   Qty
  2. item1      1       3
  3. item1      2       6
  4.  
I have to be able to get the specific Qty for the largest Version for each item in the table. This is what's giving me the most trouble.
You don't seem to explain what data you want out when you add the extra field into the equation. You don't share the relationship you're looking for for a field you refer to as both Field3 & Qty. If you can specify that clearly and unambiguously then I can try to answer your question.
Dec 10 '07 #5
improvcornartist
303 Recognized Expert Contributor
You don't seem to explain what data you want out when you add the extra field into the equation.
The query I provided in my last post is the exact data I want out when I add the extra field.
You don't share the relationship you're looking for for a field you refer to as both Field3 & Qty.
I'm not sure I understand you. I'm not looking for a relationship. The only thing that matters is the version number. I just meant that I need the specific Qty in the largest version number record. Sorry for the ambiguity, I used field3 to express the idea of what I needed, where Qty is what the field actually is.

The table contains many item numbers. Each number has 1 or more revisions. Each number + revision has it's own quantity. I need to get every numbers highest revision and the quantity for that revision. The query I posted gets what I need. It just seems like there should be a better way of getting the same data.
Dec 10 '07 #6
NeoPa
32,557 Recognized Expert Moderator MVP
That explains the relationship (between Revision & Qty) quite well thank you.
This is something that is not natively supported in SQL but there are ways of extracting the data you need.
You're looking for the [Qty] value that matches the maximum [Version] value in the recordset for each item ([Number]). I usually concatenate the two values in a field and then strip out the unrequired parts to leave the value required. Perhaps the following SQL will explain the concept better (It's also what I think you need in your scenario) :
Expand|Select|Wrap|Line Numbers
  1. SELECT [Number],
  2.        Max([Version]) AS MaxVer, 
  3.        Val(Mid(Max(Format([Version],'0000') & [Qty]),5)) AS RelQty
  4. FROM [YourTable]
  5. GROUP BY [Number]
It assumes the value of [Version] will never exceed 9,999. The trouble with this method is that it does rely on manipulating the results somewhat, and requires foreknowledge of the format of the expected data. If neither is a problem, then this method will work fine for you.

PS.
The query I provided in my last post is the exact data I want out when I add the extra field.
There are two problems with that approach :
  1. It puts the responsibility on me to do your work. Not nice when you're asking for assistance.
  2. It only works if your SQL is correct. I have no reason to doubt it in this case, but I can also do without checking it, as an answer that relies on the correctness of the question is not a very clever concept.
Dec 10 '07 #7
improvcornartist
303 Recognized Expert Contributor
Sorry. I did not intend for you to do my work. I only wanted to know if there was a better approach. The query I posted was just to show what I needed and how I approached getting it (I tried solving my problem).

Your query is a better solution, thank you for spending the time to help me. The Version will always be formatted like '000', and unless I'm missing something, manipulating the results shouldn't cause a problem.

Thanks again, and sorry for being difficult. I do appreciate your help.
Dec 11 '07 #8
NeoPa
32,557 Recognized Expert Moderator MVP
Not a problem. It was clearly not a deliberate thing so no issue (You just caught me on a bad day ;)).
I'm glad my version was able to help.
BTW it's a concept I find useful so often, so I'd keep a note of the idea at least.
Dec 11 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

5
2526
by: Mark | last post by:
Hi - I have set-up security for my users - the security is held in a text field, separated by a comma. If the users a member of groups 1, 5 and 6 - the usergroups field is set to 1,5,6 - to...
12
6507
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
10
2457
by: SueB | last post by:
I currently have a 'mail-merge' process in my Access db project. It generates custom filled out Award Certificates based on an SQL SELECT statement in a VBA routine invoked by clicking on a...
37
2761
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current...
3
2048
by: stefaan.lhermitte | last post by:
Dear MySQL-ians, I perform a SELECT on my database, but it takes over a minute for every run. I have to run it over 10000 times (with different values in the WHERE), so it takes way too long. A...
22
12423
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
10
1978
by: necapa82 | last post by:
I have an Access 2000 database in which I need to update both records in a pairing, based on the difference in the value of a quantity field. This is an example of the pertinent data in the table....
3
1952
by: tdes42 | last post by:
I believe I am looking for some form of Join Query, but my understanding of Access logic and my logic do not yet click entirely…. I have a table of ocean buoy data, taken every hour over many...
16
2199
by: Malcolm McLean | last post by:
I want this to be a serious, fruitful thread. Sabateurs will be plonked. Table-based programming is a new paradigm, similar to object-orientation, procedural decomposition, or functional...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.