[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

for loop type query in mysql

Posted on 2009-05-12
11
Medium Priority
?
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
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
 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
In this article, we’ll look at how to deploy ProxySQL.
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

650 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