|
|
Subject:
MySQL Select Query
Category: Computers > Programming Asked by: kevinjbowman-ga List Price: $7.50 |
Posted:
10 May 2005 09:00 PDT
Expires: 09 Jun 2005 09:00 PDT Question ID: 519995 |
How Do I write a MySql select statement that will select all information from 1 record based on the recors with the highest value within a group My example would be, I want to know what the transaction code was for the most recent transaction date for each customer SAMPLE TABLE. RECORD ID CUSTOMER TRANSACTION CODE TRANSACTION DATE 1 Alpha 3 01/01/2005 2 Bravo 3 01/01/2005 3 Charlie 3 01/01/2005 4 Alpha 5 01/02/2005 5 Bravo 4 01/03/2005 6 Alpha 2 01/03/2005 7 Alpha 2 01/05/2005 8 Bravo 7 01/07/2005 9 Alpha 3 01/07/2005 10 Alpha 2 01/06/2005 Result Set I Am Looking For CUSTOMER TRANSACTION DATE TRANSACTION CODE Alpha 01/07/2005 3 Bravo 01/07/2005 7 Charlie 01/01/2005 3 |
|
There is no answer at this time. |
|
Subject:
Re: MySQL Select Query
From: jackbauer-ga on 10 May 2005 12:16 PDT |
Its easy my friend SELECT customer,transaction_code,max(transaction_date) FROM sample_table GROUP BY customer; Let me know it this does not work |
Subject:
Re: MySQL Select Query
From: jackbauer-ga on 10 May 2005 12:35 PDT |
sorry, it won't work as the transaction_code is not in the group list What you could do is write a derived tables for this as SELECT T2.customer,T1.transaction_code,T2.transaction_date FROM (SELECT record_id,transaction_code FROM sample_table) AS T1 INNER JOIN (SELECT record_id,customer,max(transaction_date) 'transaction_date' FROM sample_table GROUP BY record_id,customer) AS T2 ON T1.record_id=T2.record_id; Assuming record_id as the Primary Key |
Subject:
Re: MySQL Select Query
From: kevinjbowman-ga on 11 May 2005 10:07 PDT |
Jack, Thanks for the help so far. The query returned an error message saying the syntax was wrong. I did break the 2 portions of the query down indidually and looked at them. The first portion returned all recordID and code. The second returned company name and record id and date. Even though the join was failing I still did not se how these 2 subtables would give me my information. But your structure was very helpful in allowing me to understand the fundamentals I needed. I am thinking now that SELECT T2.customer,T1.transaction_code,T2.transaction_date FROM (SELECT name,date,code FROM sample_table) AS T1 INNER JOIN (SELECT customer,max(date) 'transaction_date' FROM sample_table GROUP BY customer) AS T2 ON (T1.customer=T2.customer;) AND (T1.date=T2.date;) would give me the result set I am looking for, but there is an error in the code that keeps it from running, any ideas there? |
Subject:
Re: MySQL Select Query
From: jackbauer-ga on 12 May 2005 12:16 PDT |
What version of mySQL ? What tool you use to run the query ? My query should work fine in version 4.1 and above. Let me know if you still have the problem. |
Subject:
Re: MySQL Select Query
From: j3f-ga on 20 May 2005 21:10 PDT |
i just noticed a small error in your code in this line: ON (T1.customer=T2.customer;) AND (T1.date=T2.date;) u have 2 semi-colons which tell SQL to run the query it should look like this: ON (T1.customer=T2.customer) AND (T1.date=T2.date); -jeff |
Subject:
Re: MySQL Select Query
From: kevinjbowman-ga on 21 May 2005 11:15 PDT |
I have just upgraded over the weekend to a server that is using 4.1 I will try the query on Monday when I get back into work.. Thank you!!! |
Subject:
Re: MySQL Select Query
From: harshad09-ga on 05 Jul 2005 21:40 PDT |
Try this, SELECT * FROM tableName a WHERE a.CUSTOMER + ',' + a.[Transaction Date] IN ( SELECT b.CUSTOMER + ',' + MAX(b.[Transaction Date]) FROM tableName b GROUP BY b.CUSTOMER ) Thanks, |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |