Solved

for loop type query in mysql

Posted on 2009-05-12
11
175 Views
Last Modified: 2012-06-21
I have a table ('metrics') which has fields "code" and "date".
There are a number of codes and a number of dates for each code.

I am using MySQL 5.1 and am intending on doing this a select query only.

basically, I want a distinct list of codes with the latest date for each one.

I thought it would be easy, but after procedures, sub queries, max, distinct, and so on I still can't seem to get it to work.
I've searched the interwebs and there seem to be loads of solutions, but none that actually work.

Thanks for any help you can give me,

frustrated,

Russell
0
Comment
Question by:monkey3
  • 6
  • 5
11 Comments
 

Expert Comment

by:Marshall2056
ID: 24362817
First                               Last
Code | Date                    Nr. | Date
1111   1                          1      02.02.2009
1112   2                          2      02.03.2009

SELECT t2.Date FROM First t1 INNER JOIN Last t2 ON t1.Date = t2.Nr WHERE t1.Date = 1
- This will select Date from table "Last" where Nr = 1 -> 02.02.2009

tho i dont know what u need exactly

if u want select codes for last 10 inputs
SELECT t1.Code, t2.Date FROM First t1 INNER JOIN Last t2 ON t1.Date = t2.Nr ORDER BY DESC LIMIT BY 10


 

0
 
LVL 1

Author Comment

by:monkey3
ID: 24363329
ok, I didn't explain very well then.

so the table is as so:

Metrics
Code | Date
1111   20050101
1112   20050102
1111   20060413
1112   20060419
1113   20070101
...

And I want the following data:
Code | Date
1111   20060413
1112   20060419
1113   20070101

Simply the last date that each code occurred.

Thanks again for your help,
0
 

Expert Comment

by:Marshall2056
ID: 24364434
try use :

SELECT * FROM tbl_name ORDER BY desc LIMIT 3

this will query last 3 biggest  dates (ascending/descending)

if u use some server side code (php for example)

<input type="text" name="number" class="" size="20"> -- HTML

$number = $_POST['number'];
SELECT * FROM tbl_name ORDER BY desc LIMIT $number
0
 
LVL 1

Author Comment

by:monkey3
ID: 24365435
Unfortunately there may well be thousands of 'codes' and we will definitely not know how many there are.

I need to do something like:

select distinct (code) from metrics
for each record
   select code, max(date)


I'm sure I need to use Cursors, but I just can't get it to work.
0
 

Expert Comment

by:Marshall2056
ID: 24365526
DISTINCT clause can be considered as a special case of GROUP BY. For example, the following two queries are equivalen

from http://dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html
0
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

 
LVL 1

Author Comment

by:monkey3
ID: 24366272
yes it can.

So do you understand what I need now?
This has to be an sql statement that will return every code and the date they last occurred.

Thanks,
0
 

Expert Comment

by:Marshall2056
ID: 24367151
Well, my english is little bad so i apologize for this first :P

if i understand :

SELECT DISTINCT code
FROM metrics
WHERE date BETWEEN '4/13/06' AND '1/1/07';

but if u want return just LAST date use ORDER BY date DESC and LIMIT 1
0
 
LVL 1

Author Comment

by:monkey3
ID: 24372292
lol, no problem :)

But I think you missed the point.

******
For each code:
Get the last date it occurred.
******

I think the code you have suggested is too simplified.
It will need to be run at many points during the day so cannot contain static dates or codes.
0
 

Expert Comment

by:Marshall2056
ID: 24372849
hm, yes i think i missed the point tho i dont know exactly what u need ..

how u add data ..what u read and why u read those data..

u tell me it cannot contain static dates ..so then its about some shop or something ..when u read with bar code reader and sell some article code and date are updated..or something similar

sorry for my eng
0
 
LVL 1

Author Comment

by:monkey3
ID: 24373017
This table contains tens of thousands of codes and the dates those codes occurred.
(Think of it as a table of error codes if you like)

The data is added by a separate process which I cannot control.

I need to give a list of the codes that occurred and the most recent occurrence of those codes.

The query cannot contain static dates.
0
 
LVL 1

Accepted Solution

by:
monkey3 earned 0 total points
ID: 24373792
SIMPLE!!!

ok, it was just about getting the query in the correct order...

select max(date), code from metrics
group by code

thanks all the same :)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

13 Experts available now in Live!

Get 1:1 Help Now