mysql - Can SQL query do this? -


i have table "audit" "description" column, "record_id" column , "record_date" column. want select records description matches 1 of 2 possible strings (say, "new%" or "arch%") record_id in each of 2 matches each other. need calculate difference in days between record_date of each other.

for instance, table may contain:

id   description    record_id    record_date 1    new sub        1000         04/14/13 2    mod            1000         04/14/13 3    archived       1000         04/15/13 4    new sub        1001         04/13/13 

i want select rows 1 , 3 , calculate number of days between 4/15 , 4/14 determine how long took go new archived record (1000). both new , archived entry must present record counted (i don't care ones haven't been archived). make sense , possible calculate in sql query? don't know beyond basic sql.

i using mysql workbench this.

the following untested, should work asuming given record_id can show once "new sub" , "archived"

select n.id new_id       ,a.id archive_id       ,record_id       ,n.record_date new_date       ,a.record_date archive_date       ,datediff(a.record_date, n.record_date) days_between   audit n   join audit using(record_id)  n.description = 'new sub'    , a.description = 'archieved'; 

i changed or and, because thought wanted nr of days between records archived.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -