473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

SELECT last record having same column data

Alireza355
I have two tables:

table1:

number explanation
10 something here
11 something here
12 something here
13 something here
14 something here
20 something here
24 something here
45 something here
122 something here



table 2:

number results
10 something here
10 something here *
11 something here
11 something here *
12 something here *
14 something here *
24 something here
24 something here *

I want to have a SELECT query to return all "number"s and all "explanation"s from table1 and the last "result" of each "number" in table2 (the ones I put * next to are the ones I want) (the *s are not actually in the table, I have put them here to show which records I want)

note that some "numbers" appear only once in table 2 and some others apprear twice.

this is what I want:

number explanation results
10 something here something here
11 something here something here
12 something here something here
13 something here
14 something here something here
20 something here
24 something here something here
45 something here
122 something here
Feb 28 '09 #1
8 5193
FishVal
2,653 Expert 2GB
Hello.
  • Leftjoin table1 with table2.
  • Grouping query on that join to return "last" record per group.
Feb 28 '09 #2
Dear FishVal,

Thanx for your reply. I am not an expert developer. Please provide more details. I honestly did not undrestand.

I know how to do the left-join part, but nothing about the grouping...

And one important thing I forgot to say is that there is also a unique numbering field in table1 (autonumber)

Perhaps this field can be helpful


Thanx a lot
Feb 28 '09 #3
FishVal
2,653 Expert 2GB
You can find more details in Access help concerning "GROUP BY Clause".
Feb 28 '09 #4
NeoPa
32,556 Expert Mod 16PB
Try :
Expand|Select|Wrap|Line Numbers
  1. SELECT   Table1.Number,
  2.          Table1.Explanation,
  3.          Last(Table2.Results) AS LastResults
  4.  
  5. FROM     Table1 LEFT JOIN Table2
  6.   ON     Table1.Number=Table2.Number
  7.  
  8. GROUP BY Table1.Number,
  9.          Table1.Explanation
  10.  
  11. ORDER BY Table1.Number
When searching Access Help for SQL related issues, navigate to a top-level entry including "Microsoft Jet SQL" from the Table of Contents.
Feb 28 '09 #5
Dear NeoPa,

Thank you so so so so so so much for your reply. I am not yet getting the results I want, because it is the Last of each "number" in Table2 that I want to see the "result" for. The "result" field sometimes is null, so if I try the "Last" thing on the result, I will get the previous record, in which the "result" is not null.

I have made a simple database with only the two tables I am working on. The database is attached. Please be kind enough to take a look at the tables, and see if you can help me with this query.

In Table2 of the attached database, the records with uniquenumbers 302, 304, 306, 307, 309, 311, 312, 313 and 314 are the ones I want.

Thank you very very very very much.
Attached Files
File Type: zip db1.zip (8.1 KB, 125 views)
Mar 1 '09 #6
NeoPa
32,556 Expert Mod 16PB
It seems that all aggregate functions (Count, Last, Sum, etc) ignore Null entries. This is usually good, but in your case it causes a problem. We will either need to use theAutoNumber field to resolve this or create an extra TimeStamp field. I will have another look later.

PS. I'm really pleased you created that database of just what was needed. That's exactly how it SHOULD be done :)
Mar 1 '09 #7
FishVal
2,653 Expert 2GB
Something like the following:

Query: qryT1LJT2
Expand|Select|Wrap|Line Numbers
  1. SELECT Table1.*, Table2.*
  2. FROM Table1 LEFT JOIN Table2 ON Table1.Number = Table2.Number;
  3.  
Query: qryLastRecordsUIDs
Expand|Select|Wrap|Line Numbers
  1. SELECT qryT1LJT2.Table1.Number, Max(qryT1LJT2.uniquenumber) AS LastRecordUID
  2. FROM qryT1LJT2
  3. GROUP BY qryT1LJT2.Table1.Number;
  4.  
Query: qryT1WithT2UID
Expand|Select|Wrap|Line Numbers
  1. SELECT Table1.*, qryLastRecordsUIDs.LastRecordUID
  2. FROM qryLastRecordsUIDs INNER JOIN Table1 ON qryLastRecordsUIDs.Number = Table1.Number;
  3.  
Query: qryFinal
Expand|Select|Wrap|Line Numbers
  1. SELECT qryT1WithT2UID.*, Table2.Results1, Table2.Results2
  2. FROM qryT1WithT2UID LEFT JOIN Table2 ON qryT1WithT2UID.LastRecordUID = Table2.uniquenumber;
  3.  
Regards,
Fish
Mar 1 '09 #8
Dear FishVal and Neopa,

Thank you very much for your kind help.

I figured it out this way:

first, making a query which selects the LAST of "Numbers" in Table2

Then, making another query, which LEFT JOINs all the Numbers in Table 1 with the ones already selected by the first query.

Thank you so much.
Mar 2 '09 #9

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

Similar topics

8
by: Dan Matthews | last post by:
Hey everybody, I have an 2000 Access database that stores job listings from potential employers for a school. I would like to be able to display the date on a webpage the last time the database...
4
by: adam | last post by:
hello I have strange problem. I write to MySQL data to table with one column of varchar(8000). I write 750000 bytes, so it get 93 records of 8000 bytes and the last - 6000bytes. but what is...
19
by: William Wisnieski | last post by:
Hello Everyone, I have a main form with a datasheet subform that I use to query by form. After the user selects two criteria on the main form and clicks the cmdShowResults button on the main...
2
by: Chris Plowman | last post by:
Hi all, I was wondering if anyone can help me with a really annoying problem I have been having. I made a derived datagrid class that will select the row when a user clicks anywhere on a cell...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
11
by: MurdockSE | last post by:
Greetings. My Situation: // I have an .xml file that I am reading into a dataset in the following code - DataSet ds = new DataSet("MyDataset"); ds.ReadXml(@"c:\data\"+cmyxmlfilename);
48
by: Jimmy | last post by:
thanks to everyone that helped, unfortunately the code samples people gave me don't work. here is what i have so far: <% Dim oConn, oRS, randNum Randomize() randNum = (CInt(1000 * Rnd) + 1) *...
22
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="...
5
by: Matthew Wells | last post by:
I have a listbox set to simple multi select. For this example, users only select one item at a time. I have command buttons on the form for First, Previous, Next, Last, New (record). The form...
6
by: plaster1 | last post by:
Been trying to come up with a query to filter-down my sample set into distinct records. For instance, lets say column1 is a sample set, column2 is the parameter, and column3 is a name and column4...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
0
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...
0
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...

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.