473,569 Members | 2,731 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 1402
NeoPa
32,564 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,564 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,564 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,564 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
2527
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 check if they are allowed access to the books, I need to check each of these numbers (by using split,"," and building a SQL statement - this needs to...
12
6522
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 courses to pass the correct option value and then be displayed at the following URL: http://www.dslextreme.com/users/kevinlyons/selectResults.html ...
10
2466
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 command button. The "problem": I want to conditionally insert some text into the award certificate based on a field selected by the SELECT statement. ...
37
2774
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 behaviour, which is returning a value which is, really, id({}) < id(). He also said that default equality comparison will continue to be identity-based....
3
2052
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 was therefore wondering if I could improve the query speed. Below you find the query. It is based on the ratio between a pixel (pix) vs. the average...
22
12436
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=" & msDbFilename moConn.Properties("Persist Security Info") = False moConn.ConnectionString = msConnString moConn.CursorLocation = adUseClient...
10
1985
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. Pairing Individual Quantity Status AB A 15 AB B 13 CD C 13 CD D 15
3
1960
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 days (hopefully a whole year if it doesn’t slow things down too much): tblBuoy Date Time Swell Direction Swell Period Swell...
16
2205
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 programming. The idea is that all the data in the program comes in "tables". A table consists of records and fields, and is thus a 2d entry. Fields may be...
0
7698
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7673
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2113
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 we have to send another system
0
937
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.