sql - Identify open cases for each week during a year -
i trying produce report identifies client cases open during each week of year. have following sql returns clients indicator on whether case open during week 1 of our calendar. client has 2 aspects identifies if case open - mov_start_date , esu_start date should greater end date of period, , mov_end_date/esu_start date should either null or greater start date of period.
the below code works, thought copy left join wk1 , rename wk2 return information week 2 i'm getting error relating ambiguously named columns. additionally, i'm guessing having 52 (one each week) left joins on report isn't particularly advisable, again i'm wondering if there better way of achieving this?
select a.esu_per_gro_id, a.esu_id, a.status, b.mov_id, b.mov_start_date, b.mov_end_date, a.esu_start_date, a.esu_end_date, ls.cls_desc, nvl2(wk1.prd_period_num,'y','n') "week1" left join b on b.mov_per_gro_id = a.esu_per_gro_id left join ls on ls.cls_code = a.status left join o_periods wk1 on b.mov_start_date < wk1.prd_end_date , (b.mov_end_date null or b.mov_end_date > wk1.prd_start_date) , a.esu_start_date < wk1.prd_end_date , (a.esu_end_date null or a.esu_end_date > wk1.prd_start_date) , prd_cal_id = 'e1190' , wk1.prd_period_num = 1 , wk1.prd_year = 2012 b.mov_start_date not null , a.status <> ('x')
hopefully have provided enough information, if not, happy answer questions. thanks!
sample data (produced above query)
p id esu_id status mov_id m_start m_end desc week1 1 esu1 new 1m 01/01/2012 boo y 2 esu2 new 2m 01/03/2012 boo n
desired output (week1 - week 52)
p id esu_id status mov_id m_start m_end desc week1 week2 1 esu1 new 1m 01/01/2012 boo y y 2 esu2 new 2m 01/03/2012 boo n n
i suspect reason creating wk2 join wk1 didn't work column prd_cal_id didn't have table alias on it. however, guessed, 52 joins not going perform well. try following:
select a.esu_per_gro_id, a.esu_id, a.status, b.mov_id, b.mov_start_date, b.mov_end_date, a.esu_start_date, a.esu_end_date, ls.cls_desc, 'week' || trim(to_char(pd.prd_period_num)) week_desc left join b on b.mov_per_gro_id = a.esu_per_gro_id left join ls on ls.cls_code = a.status left join o_periods pd on b.mov_start_date < pd.prd_end_date , (b.mov_end_date null or b.mov_end_date > pd.prd_start_date) , a.esu_start_date < pd.prd_end_date , (a.esu_end_date null or a.esu_end_date > pd.prd_start_date) b.mov_start_date not null , a.status <> ('x') , pd.prd_cal_id = 'e1190' , pd.prd_year = 2012 order week_desc
this produces different results original query, having week_desc instead of trying create 52 different columns, 1 each week, think perform better.
share , enjoy.
Comments
Post a Comment