简体   繁体   中英

SQL Server Compare two rows to identify ID

Here is what I am trying to do. I have one column of data that is the ID of every person. I have a second column of data that is the ID of just supervisors. I also have a third column of data that identifies an ID as staff or faculty.

I need to take the Supervisor column and compare it against the ID column. When the supervisor's ID is located then I need to identify that row as a staff of faculty supervisor in a separate column. If the ID is not in the supervisor column then they just need to be marked as staff or faculty based off of the third column.

So the three columns that I have are ID, Supervisor ID and Class Type.

Any help would be appreciated.

Here is the code that I currently have

select distinct ODS_PERSON.ID "Cient_ID",
       ODS_PERSON.LAST_NAME "Last_Name",
       CASE
       WHEN H17_PERSON.NICKNAME  is not null
       THEN H17_PERSON.NICKNAME
       ELSE ODS_PERSON.FIRST_NAME
       END "First_Name",
       H17_PERSON.H17_PER_USERNAME + '@highpoint.edu' "Email",
       CASE
       WHEN ODS_HRPER.HRP_EFFECT_TERM_DATE  is null
       THEN '1'
       ELSE '0'
       END "User_Status",
       CASE
       WHEN SPT_POSITION.POS_CLASS = 'FACL' AND (ODS_PERSON.ID = SPT_PERPOS.PERPOS_SUPERVISOR_HRP_ID)
       THEN 'FACSUP'
       ELSE 'NOPE'
       END "Employee_Type",
       SPT_PERPOS.PERPOS_SUPERVISOR_HRP_ID "Manager",
       SPT_POSITION.DEPARTMENT_DESC "Department",
       SPT_PERPOS.PERPOS_POS_SHORT_TITLE "Position_Title",
       SPT_POSITION.POS_CLASS "Position_Class"
  from ( ( ( ( ( SPT_PERPOSWG SPT_PERPOSWG left join ODS_HRPER ODS_HRPER on SPT_PERPOSWG.PPWG_HRP_ID = ODS_HRPER.HRPER_ID ) left join SPT_PERPOS SPT_PERPOS on SPT_PERPOSWG.PPWG_HRP_ID = SPT_PERPOS.PERPOS_HRP_ID ) left join SPT_PERSTAT SPT_PERSTAT on SPT_PERPOSWG.PPWG_HRP_ID = SPT_PERSTAT.PERSTAT_HRP_ID ) left join ODS_PERSON ODS_PERSON on SPT_PERPOSWG.PPWG_HRP_ID = ODS_PERSON.ID ) left join SPT_POSITION SPT_POSITION on SPT_PERPOS.PERPOS_POSITION_ID = SPT_POSITION.POSITION_ID ) left join H17_PERSON H17_PERSON on SPT_PERPOSWG.PPWG_HRP_ID = H17_PERSON.ID
 where ODS_HRPER.HRP_EFFECT_TERM_DATE is null
       and SPT_PERPOS.PERPOS_END_DATE is null
 order by ODS_PERSON.ID
SELECT ID, Supervisor_ID, Class_Type,
CASE
WHEN SuperVisor_ID is null and Class_Type is not null THEN Class_Type
WHEN (SuperVisor_ID = ID or SuperVisor_ID is not null) THEN 'Supervisor'
END
from tableID

Not sure if this is what you're looking for. In the future it would be helpful to know how many tables are involved, and if the data types in each column can be compared directly or if you have to cast the data types.

You may also want to put whether join conditions are necessary to get the information and what columns the tables would join on.

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM