Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I tried the techniques here:
http://www.onlamp.com/pub/a/onlamp/excerpt/mysqlckbk/index3.html?page=2
it looks like this:
SELECT a.brand, a.date AS newdate, b.date AS olddate, DATEDIFF(a.date,b.date) AS diff FROM( SELECT brand, date, ( SELECT count(*) FROM t_detail_hkjc WHERE brand=T.brand AND date<T.date )+1 AS rank1 FROM test AS T ) AS a INNER JOIN ( SELECT brand, date, ( SELECT count(*) FROM t_detail_hkjc WHERE brand=T1.brand AND date<T1.date ) AS rank2 FROM test2 AS T1 ) AS b ON (a.rank1=b.rank2) AND (a.brand = b.brand) ORDER BY a.brand, a.date DESC ;
This does work like you suggest by creating the T and T1 alias/copy tables then you line up the sequence numbers (rank1 and rank2) and process the columns of interest.

The problem is it takes about 1 hour to return the query on about 120,000 records... Even merely sequencing the brands alone (the inner Select count(*) part) takes about 7 minutes). I think the SQL is slow because the whole table is searched each time to find the next in sequence. By using my perl program and executing the Select to return the table in the sorted order you know the next record is the one you are looking for. It runs in about 30secs - 1min on the same 120k record table and writes the answer to a new table. Fortunately the data table is only updated about once per week so the query table can be generated immediately after.

When I started out looking to solve this problem I thought it would be a relatively easy piece of SQL to solve a very common problem, e.g. calculate the time between time stamps in a database file for example. But in SQL it seems not;) - I'm no expert in SQL found it tough going. I wish I'd thought of the perl solution earlier!

In reply to Re^2: find difference in dates/items in same column by Anonymous Monk
in thread find difference in dates/items in same column by zerocred

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-23 12:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found