第一種人員表跟日期表(人員輪流值班)sql
上面是按照指定到了人員值班,自動查找的人員值班,如下是代碼優化
--用戶表中字段:id,login_name.數據以下: select id,login_name from Au_user; --假設指定用戶從id=54開始輪流值班,而且按id升序輪流值班。則先對用戶表進行序號排序 select row_number()over(order by seq asc, id asc )-1 as RowNum,* from ( select id,login_name,0 as seq from au_user where id>=54 union ALL select id,login_name,1 as seq from au_user where id<54 ) t1 -- 假設日期表爲:temp_table_date select * from temp_table_date; -- 假設日期爲2至10號,從小日期至大日期輪流值日: --給日期表設置序號 select RowNum%(select count(1) from Au_user) as r_row_num,date_field from ( select row_number()over(order by date_field asc )-1 as RowNum,date_field from temp_table_date ) t2 ; --組合起來,結果以下: select * from ( select row_number()over(order by seq asc, id asc )-1 as RowNum,id,login_name from ( select id,login_name,0 as seq from au_user where id>=54 union ALL select id,login_name,1 as seq from au_user where id<54 ) t1 ) V1 LEFT JOIN ( select RowNum%(select count(1) from Au_user) as r_row_num,date_field from ( select row_number()over(order by date_field asc )-1 as RowNum,date_field from temp_table_date ) t2 ) V2 ON V1.RowNum=V2.r_row_num ORDER BY V2.date_field ASC
第二種,有班級,人員表關聯班級,所產生的值班數據blog
select r1.class_id,r1.class_name,r1.CURRENT_USER_ID,R2.ID AS Login_id,r2.login_name,r1.date_field from ( select v1.class_id,v1.class_name,V1.CURRENT_USER_ID,v1.date_field,v2.user_count,v1.RowNum,mod(v1.RowNum,v2.user_count) as class_mod from (select t1.ID as class_id,t1.CLASS_NAME,t1.CURRENT_USER_ID,t2.RowNum,t2.DATE_FIELD from view_table_class t1 , (select row_number()over(order by date_field asc )-1 as RowNum,date_field from temp_table_date) t2 ) v1 left join (select class_id,count(1) as user_count from AU_USER group by CLASS_ID) v2 on v1.class_id =v2.class_id ) r1 left join ( select row_number()over(PARTITION by CLASS_ID order by ORDER_ desc ,id asc )-1 as RowNum,v.* from ( select t1.id,t1.LOGIN_NAME,t1.CLASS_ID,t2.CLASS_NAME,t2.CURRENT_USER_ID, case when t1.id>=t2.CURRENT_USER_ID then 1 else 0 end as ORDER_ from au_user t1 left join view_table_class t2 on t1.CLASS_ID=t2.ID ) v ) r2 on R1.CLASS_ID=R2.CLASS_ID AND r1.class_mod=r2.RowNum order by r1.class_id ASC,DATE_FIELD ASC
-- 查詢結果 select r1.class_id,r1.class_name,r1.CURRENT_USER_ID,R2.ID AS Login_id,r2.login_name,r1.date_field from ( select v1.id as class_id,v1.class_name,V1.CURRENT_USER_ID,v1.date_field,v2.user_count,v1.RowNum,mod(v1.RowNum,v2.user_count) as class_mod from ( select row_number()over(PARTITION by id order by date_field asc )-1 as RowNum,t1.*,t2.* from view_table_class t1,temp_table_date t2 ) v1 left join (select class_id,count(1) as user_count from AU_USER group by CLASS_ID) v2 on v1.id =v2.class_id ) r1 left join ( select row_number()over(PARTITION by CLASS_ID order by ORDER_ desc ,id asc )-1 as RowNum,v.* from ( select t1.id,t1.LOGIN_NAME,t1.CLASS_ID,t2.CLASS_NAME,t2.CURRENT_USER_ID, case when t1.id>=t2.CURRENT_USER_ID then 1 else 0 end as ORDER_ from au_user t1 left join view_table_class t2 on t1.CLASS_ID=t2.ID ) v ) r2 on R1.CLASS_ID=R2.CLASS_ID AND r1.class_mod=r2.RowNum order by r1.class_id ASC,DATE_FIELD ASC
原創做品,轉載請在明顯位置寫明原創地址排序