一些sql

--關聯更新
update t_stf_duty_info  t set dept_id =(select dept_id from t_stf_base where t.employee_id=employee_code);
--查詢重複記錄
select employee_code from t_stf_base group by employee_code having count(*)>1;--重複記錄
--大小寫轉換
UPDATE 表 SET 字段 = upper(字段), 字段 = LOWER(字段)  


---查詢一個人兩條最新記錄
 select * from (
   select count(1) as num,employee_id from T_STF_POSITION where is_last_='1' group by employee_id)
   where num=2;
   
---年齡字段的更新
 update t_stf_base set age_=(to_char(sysdate,'yyyy')-to_char(BIRTHDAY,'yyyy'))
 
--日期更新
update  表名  set   日期 = to_date('','yyyymmdd') where ..............
update t_stf_base  t set birthday =(select birthday from TEXT_BIRTHDAY where t.employee_id=employee_id);


--分組前面列幾個字段,group by 後面也要列幾個字段
select t.name,t.dept_id,t.sex from t_stf_base t group by t.name,t.dept_id,t.sex having count(name)>1


-------------獲得當前年月日
select to_char(SYSDATE, 'YYYY') || '年' || to_char(SYSDATE, 'MM') || '月' ||
       to_char(SYSDATE, 'DD') || '日' as currentDate
  from t_stf_base --當前日期
  
例如:
select t.employee_id,t.name,t.sex,t.id_code,t.employee_code,a.BEGIN_DATE_,a.END_DATE_,
to_char(SYSDATE, 'YYYY') || '年' || to_char(SYSDATE, 'MM') || '月' ||
       to_char(SYSDATE, 'DD') || '日' as currentDate
from t_stf_base t
left join T_STF_CONTRACT_INFO a 
on t.employee_id=a.employee_id_
where t.employee_id='6900'  
  
---------------------------------------------------------
學習經歷最新字段賦值:
--第一步
update t_stf_edu_exp set HIGHEST_EDU=''
update t_stf_edu_exp set HIGHEST_DEGREE=''
--第二步
 update t_stf_edu_exp
      set highest_edu = 1
    where edu_id in (select edu_id
                       from (select *
                               from (select t_stf_edu_exp.*,
                                            row_number() over(partition by employee_Id order by END_DATE DESC) cn
                                       from t_stf_edu_exp where end_date is not null)
                              where cn = 1))
--第三步
update t_stf_edu_exp set HIGHEST_EDU='0' where HIGHEST_EDU is null
update t_stf_edu_exp set HIGHEST_DEGREE=HIGHEST_EDU
------------------------------------------
職稱新增最新字段賦值
 update T_STF_TECHNIC_INFO
      set is_last = 1
    where num_id in (select num_id
                       from (select *
                               from (select T_STF_TECHNIC_INFO.*,
                                            row_number() over(partition by employee_Id order by ASSESS_DATE DESC) cn
                                       from T_STF_TECHNIC_INFO)
                              where cn = 1))


---------------------------------------------
行政職務最新字段賦值
 update t_Stf_Duty
      set duty_status = 1
    where id in (select id
                       from (select *
                               from (select t_Stf_Duty.*,
                                            row_number() over(partition by employee_Id order by START_DATE DESC) cn
                                       from t_Stf_Duty)

                              where cn = 1))

-------------------------------------------------------

oracle根據出生日期算年齡:
select floor(months_between(to_date(concat(extract(year from sysdate),'-10-31'),'YYYY-MM-DD'),to_date(生日的日期,'yyyy-mm-dd'))/12)  from table_name


floor 向下取整
months_between 日期相差的月份數
concat字字符串連接
extract(year from sysdate) 返回當前日期的年份




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章