我试图在同一个表格中比较员工数据的两个日期范围(即6/1/2018到6/4/2018)。我对表进行了自我连接以比较每列,如果列不符合条件,那么我希望它显示为NULL。我注意到,当它们应该
时,并非所有列都显示为NULLselect emp1.RRD,emp1.EMP_ID,emp1.JOB_REQ,emp1.POS_NUM,emp1.CANDIDATE_ID,emp1.EMP_STATUS,emp1.WORKER_TYPE,emp1.CONT_SUP_NAME,emp1.REMOTE_FLAG,emp1.emp_NAME,
emp1.FIRST_NAME,emp1.LAST_NAME,emp1.RECENT_HIRE_DATE,emp1.ORIG_HIRE_DATE,emp1.DEPT_NUM,emp1.DEPT_NAME,emp1.FUNCTIONAL_GROUP,emp1.PAY_TYPE,emp1.JOB_TITLE,
emp1.JOB_CODE,emp1.JOB_FAMILY,emp1.JOB_GROUP,emp1.JOB_FUNCTION,emp1.WORK_PHONE,emp1.EMAIL,emp1.EMP_MGR_LVL,emp1.ANNUAL_RATE,emp1.HRLY_RATE,emp1.EMP_TYPE,
emp1.TIME_TYPE,emp1.EXEMPT,emp1.EXPECTED_HOURS,emp1.COMP_GRADE,emp1.WORK_LOC_ID,emp1.WORK_LOC_NAME,emp1.WORK_LOC_ADD_1,emp1.WORK_LOC_ADD_2,emp1.WORK_LOC_CITY,
emp1.WORK_LOC_STATE,emp1.WORK_LOC_ZIP,emp1.HOME_ADD_1,emp1.HOME_ADD_2,emp1.HOME_ADD_CITY,emp1.HOME_ADD_STATE,emp1.HOME_ADD_ZIP,emp1.HOME_ADD_ST,emp1.LOA,
emp1.REPORTS_TO,emp1.REPORTS_TO_EMP_ID,emp1.REPORTS_TO_EMAIL,emp1.REPORTS_TO_MGR_LVL,emp1.emp_rank,emp1.MGR_LVL_NUM,emp1.HIERARCHY_TIER,emp1.TIER_1,
emp1.TIER_2,emp1.TIER_3,emp1.TIER_4,emp1.TIER_5,emp1.TIER_6,emp1.Snapshot_FTE,emp1.Period_FTE,emp1.Info_Source,emp1.Calendar_period,emp2.RRD,emp2.EMP_ID,
emp2.JOB_REQ,emp2.POS_NUM,emp2.CANDIDATE_ID,emp2.EMP_STATUS,emp2.WORKER_TYPE,emp2.CONT_SUP_NAME,emp2.REMOTE_FLAG,emp2.emp_NAME,emp2.FIRST_NAME,emp2.LAST_NAME,
emp2.RECENT_HIRE_DATE,emp2.ORIG_HIRE_DATE,emp2.DEPT_NUM,emp2.DEPT_NAME,emp2.FUNCTIONAL_GROUP,emp2.PAY_TYPE,emp2.JOB_TITLE,emp2.JOB_CODE,emp2.JOB_FAMILY,
emp2.JOB_GROUP,emp2.JOB_FUNCTION,emp2.WORK_PHONE,emp2.EMAIL,emp2.EMP_MGR_LVL,emp2.ANNUAL_RATE,emp2.HRLY_RATE,emp2.EMP_TYPE,emp2.TIME_TYPE,emp2.EXEMPT,
emp2.EXPECTED_HOURS,emp2.COMP_GRADE,emp2.WORK_LOC_ID,emp2.WORK_LOC_NAME,emp2.WORK_LOC_ADD_1,emp2.WORK_LOC_ADD_2,emp2.WORK_LOC_CITY,emp2.WORK_LOC_STATE,
emp2.WORK_LOC_ZIP,emp2.HOME_ADD_1,emp2.HOME_ADD_2,emp2.HOME_ADD_CITY,emp2.HOME_ADD_STATE,emp2.HOME_ADD_ZIP,emp2.HOME_ADD_ST,emp2.LOA,emp2.REPORTS_TO,
emp2.REPORTS_TO_EMP_ID,emp2.REPORTS_TO_EMAIL,emp2.REPORTS_TO_MGR_LVL,emp2.emp_rank,emp2.MGR_LVL_NUM,emp2.HIERARCHY_TIER,emp2.TIER_1,emp2.TIER_2,emp2.TIER_3,
emp2.TIER_4,emp2.TIER_5,emp2.TIER_6,emp2.Snapshot_FTE,emp2.Period_FTE,emp2.Info_Source,emp2.Calendar_period,
CASE
WHEN emp1.JOB_REQ <> emp2.JOB_REQ THEN emp2.JOB_REQ
WHEN emp1.POS_NUM <> emp2.POS_NUM THEN emp2.POS_NUM
WHEN emp1.CANDIDATE_ID <> emp2.CANDIDATE_ID THEN emp2.CANDIDATE_ID
WHEN emp1.EMP_STATUS <> emp2.EMP_STATUS THEN emp2.EMP_STATUS
WHEN emp1.WORKER_TYPE <> emp2.WORKER_TYPE THEN emp2.WORKER_TYPE
WHEN emp1.CONT_SUP_NAME <> emp2.CONT_SUP_NAME THEN emp2.CONT_SUP_NAME
WHEN emp1.REMOTE_FLAG <> emp2.REMOTE_FLAG THEN emp2.REMOTE_FLAG
WHEN emp1.emp_NAME <> emp2.emp_NAME THEN emp2.emp_NAME
WHEN emp1.FIRST_NAME <> emp2.FIRST_NAME THEN emp2.FIRST_NAME
WHEN emp1.LAST_NAME <> emp2.LAST_NAME THEN emp2.LAST_NAME
---WHEN emp1.RECENT_HIRE_DATE <> emp2.RECENT_HIRE_DATE THEN emp2.RECENT_HIRE_DATE
---WHEN emp1.ORIG_HIRE_DATE <> emp2.ORIG_HIRE_DATE THEN emp2.ORIG_HIRE_DATE
WHEN emp1.DEPT_NUM <> emp2.DEPT_NUM THEN emp2.DEPT_NUM
WHEN emp1.DEPT_NAME <> emp2.DEPT_NAME THEN emp2.DEPT_NAME
WHEN emp1.FUNCTIONAL_GROUP <> emp2.FUNCTIONAL_GROUP THEN emp2.FUNCTIONAL_GROUP
WHEN emp1.PAY_TYPE <> emp2.PAY_TYPE THEN emp2.PAY_TYPE
WHEN emp1.JOB_TITLE <> emp2.JOB_TITLE THEN emp2.JOB_TITLE
WHEN emp1.JOB_CODE <> emp2.JOB_CODE THEN emp2.JOB_CODE
WHEN emp1.JOB_FAMILY <> emp2.JOB_FAMILY THEN emp2.JOB_FAMILY
WHEN emp1.JOB_GROUP <> emp2.JOB_GROUP THEN emp2.JOB_GROUP
WHEN emp1.JOB_FUNCTION <> emp2.JOB_FUNCTION THEN emp2.JOB_FUNCTION
WHEN emp1.WORK_PHONE <> emp2.WORK_PHONE THEN emp2.WORK_PHONE
WHEN emp1.EMAIL <> emp2.EMAIL THEN emp2.EMAIL
WHEN emp1.EMP_MGR_LVL <> emp2.EMP_MGR_LVL THEN emp2.EMP_MGR_LVL
---WHEN emp1.ANNUAL_RATE <> emp2.ANNUAL_RATE THEN emp2.ANNUAL_RATE
---WHEN emp1.HRLY_RATE <> emp2.HRLY_RATE THEN emp2.HRLY_RATE
WHEN emp1.EMP_TYPE <> emp2.EMP_TYPE THEN emp2.EMP_TYPE
WHEN emp1.TIME_TYPE <> emp2.TIME_TYPE THEN emp2.TIME_TYPE
WHEN emp1.EXEMPT <> emp2.EXEMPT THEN emp2.EXEMPT
---WHEN emp1.EXPECTED_HOURS <> emp2.EXPECTED_HOURS THEN emp2.EXPECTED_HOURS
WHEN emp1.COMP_GRADE <> emp2.COMP_GRADE THEN emp2.COMP_GRADE
WHEN emp1.WORK_LOC_ID <> emp2.WORK_LOC_ID THEN emp2.WORK_LOC_ID
WHEN emp1.WORK_LOC_NAME <> emp2.WORK_LOC_NAME THEN emp2.WORK_LOC_NAME
WHEN emp1.WORK_LOC_ADD_1 <> emp2.WORK_LOC_ADD_1 THEN emp2.WORK_LOC_ADD_1
WHEN emp1.WORK_LOC_ADD_2 <> emp2.WORK_LOC_ADD_2 THEN emp2.WORK_LOC_ADD_2
WHEN emp1.WORK_LOC_CITY <> emp2.WORK_LOC_CITY THEN emp2.WORK_LOC_CITY
WHEN emp1.WORK_LOC_STATE <> emp2.WORK_LOC_STATE THEN emp2.WORK_LOC_STATE
WHEN emp1.WORK_LOC_ZIP <> emp2.WORK_LOC_ZIP THEN emp2.WORK_LOC_ZIP
WHEN emp1.HOME_ADD_1 <> emp2.HOME_ADD_1 THEN emp2.HOME_ADD_1
WHEN emp1.HOME_ADD_2 <> emp2.HOME_ADD_2 THEN emp2.HOME_ADD_2
WHEN emp1.HOME_ADD_CITY <> emp2.HOME_ADD_CITY THEN emp2.HOME_ADD_CITY
WHEN emp1.HOME_ADD_STATE <> emp2.HOME_ADD_STATE THEN emp2.HOME_ADD_STATE
WHEN emp1.HOME_ADD_ZIP <> emp2.HOME_ADD_ZIP THEN emp2.HOME_ADD_ZIP
WHEN emp1.HOME_ADD_ST <> emp2.HOME_ADD_ST THEN emp2.HOME_ADD_ST
WHEN emp1.LOA <> emp2.LOA THEN emp2.LOA
WHEN emp1.REPORTS_TO <> emp2.REPORTS_TO THEN emp2.REPORTS_TO
WHEN emp1.REPORTS_TO_EMP_ID <> emp2.REPORTS_TO_EMP_ID THEN emp2.REPORTS_TO_EMP_ID
WHEN emp1.REPORTS_TO_EMAIL <> emp2.REPORTS_TO_EMAIL THEN emp2.REPORTS_TO_EMAIL
WHEN emp1.REPORTS_TO_MGR_LVL <> emp2.REPORTS_TO_MGR_LVL THEN emp2.REPORTS_TO_MGR_LVL
---WHEN emp1.emp_rank <> emp2.emp_rank THEN emp2.emp_rank
---WHEN emp1.MGR_LVL_NUM <> emp2.MGR_LVL_NUM THEN emp2.MGR_LVL_NUM
---WHEN emp1.HIERARCHY_TIER <> emp2.HIERARCHY_TIER THEN emp2.HIERARCHY_TIER
WHEN emp1.TIER_1 <> emp2.TIER_1 THEN emp2.TIER_1
WHEN emp1.TIER_2 <> emp2.TIER_2 THEN emp2.TIER_2
WHEN emp1.TIER_3 <> emp2.TIER_3 THEN emp2.TIER_3
WHEN emp1.TIER_4 <> emp2.TIER_4 THEN emp2.TIER_4
WHEN emp1.TIER_5 <> emp2.TIER_5 THEN emp2.TIER_5
WHEN emp1.TIER_6 <> emp2.TIER_6 THEN emp2.TIER_6
---WHEN emp1.Snapshot_FTE <> emp2.Snapshot_FTE THEN emp2.Snapshot_FTE
---WHEN emp1.Period_FTE <> emp2.Period_FTE THEN emp2.Period_FTE
WHEN emp1.Info_Source <> emp2.Info_Source THEN emp2.Info_Source
---WHEN emp1.Calendar_period <> emp2.Calendar_period THEN emp2.Calendar_period
ELSE NULL
END
from emp_main as emp1, emp_main as emp2
where emp1.rrd = '2018-06-04' and emp2.rrd = '2018-06-01'
and emp1.emp_id = emp2.emp_id;
并且由于以下错误,有几行出错:
1.从字符串转换日期和/或时间时,转换失败。 ---当emp1.RECENT_HIRE_DATE&lt;&gt; emp2.RECENT_HIRE_DATE那么emp2.RECENT_HIRE_DATE ---当emp1.ORIG_HIRE_DATE&lt;&gt; emp2.ORIG_HIRE_DATE那么emp2.ORIG_HIRE_DATE
有人可以帮忙吗?我试过CAST / CONVERT行,但我显然有错误的语法。整个SQL查询如下所示。