sql - MYSQL query with complex aggregated results -


here scenario. i've got 2 tables schema this

experience: (id, title)

user_experience: (id, experience_id, created_date)

when user adds experience list, record added in user_experience. want dates when experience achieved milestone of 2, 5 or 10 listers.

required result: id (experience.id), title, created_date, count (no of users listed experience)

if experience achieved 10 listers, result should include records corresponding times when got 2 , 5 listers.

i'm using query getting required result, , looking better "query".

select e.id, e.title, ue_res.created_date, ue_res.count experience e inner join (     select ue.experience_id, (         select count(uee.id) user_experience uee uee.experience_id = ue.experience_id , uee.created_date <= ue.created_date     ) `count`, ue.created_date user_experience ue ) ue_res on e.id = ue_res.experience_id ue_res.uecount in (2,5,10) order e.id,ue_res.created_date asc  

i can't create table achievements because milestones can changed anytime, i'm looking query.

here sample data

sample data experience:

1   |   bunjee jump eifel tower 2   |   surf in rio 3   |   fly on grand canyon 4   |   go mexican resturant 

sample data user_experience:

1   |   1   |   2013-03-01 2   |   4   |   2013-03-02 3   |   1   |   2013-03-03 4   |   3   |   2013-03-04 5   |   2   |   2013-03-05 6   |   3   |   2013-03-06 7   |   4   |   2013-03-07 8   |   1   |   2013-03-08 9   |   1   |   2013-03-09 10  |   1   |   2013-03-10 11  |   3   |   2013-03-11 12  |   2   |   2013-03-12 13  |   2   |   2013-03-13 

result sample data:

1   |   bunjee jump eifel tower    |   2013-03-03  |   2 3   |   fly on grand canyon           |   2013-03-06  |   2 4   |   go mexican resturant       |   2013-03-07  |   2 1   |   bunjee jump eifel tower    |   2013-03-10  |   5 2   |   surf in rio                     |   2013-03-12  |   2 

thanks in advance :)

this query might trick:

set @helper:=0;  set @oldid:=0;  select ue.experience_id id,        e.title,        ue.created_date,        ue.helper        milestone   (select ue.id,                ue.experience_id,                ue.created_date,                case @oldid                  when ue.experience_id ( @helper := @helper + 1 )                  else ( @helper := 1 )                end                            helper,                ( @oldid := ue.experience_id ) oldid           user_experience ue         order  ue.experience_id asc,                   ue.created_date asc) ue        join experience e          on ( e.id = ue.experience_id )  ue.helper in ( 2, 5, 10 );   

pay attention data ordering made using of running variables work.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -