java - How to select the field value if field in all child records are the same -
i don't know if i'm wording question correctly, here goes.
this web application using java, oracle, hibernate.
i have 2 tables in 1 (items) many (tasks) relationship.
items
- item_id name
- active_status
- etc
tasks
- task_id
- item_id
- active_status
- progress_status
- etc
the item's status made of statuses of of tasks. here's logic...
- if item status canceled or on hold...return item active status
- if there no tasks, return completed
- if tasks active , not superseded, then
- ...return not started if tasks not started
- ...return completed if tasks completed
- ...return on hold if tasks on hold
- otherwise return started
i want using sql , map field in hibernate mapping file.
i've tried many things on past several days, , can't seem work. tried grouping records , if 1 record found, return status. i've used decode, case, etc.
here few examples of things i've tried. in second example 'not single group group function' error.
any thoughts?
select decode(i.active_status_id, 'oh', i.active_status_id, 'ca', i.active_status_id,t.progress_status_id) tasks t left outer join items on i.item_id = t.item_id t.item_id = 10927815 , t.active_status_id = 'ac' , t.active_status_id != 'su' group i.active_status_id, t.progress_status_id; select case when (count(*) = 1) progress_status_id else 'st' end (select progress_status_id tasks t t.item_id = 10927815 , (t.active_status_id = 'ac' , t.active_status_id != 'su') group t.progress_status_id)
perhaps somthing this
select item_id , case when active_status in ('canceled', 'on hold') active_status when t_num = 0 'completed' when flag_all_active = 1 , flag_all_not_started = 1 'not started' when flag_all_active = 1 , flag_all_completed = 1 'completed' when flag_all_active = 1 , flag_all_on_hold = 1 'on hold' else 'started' end ( select i.item_id , i.active_status , sum(case when t.task_id null 0 else 1 end ) t_num , min( case t.active_status when 'ac' 1 else 0 end ) flag_all_active , min( case t.progress_status_id when 'not starten' 1 else 0 end ) flag_all_not_started , min( case t.progress_status_id when 'completed' 1 else 0 end ) flag_all_completed , min( case t.progress_status_id when 'on hold' 1 else 0 end ) flag_all_on_hold items left outer join tasks t on (t.item_id = i.item_id ) group i.item_id ) ;
Comments
Post a Comment