简体   繁体   中英

Select rows with same ID but has different values in another column

I have a table (PS_JOB) that contains User data. An USER ID (USRID) can have more than 1 row in this table, and therefore can have different values in another column (in same table) called PER_ORG. When the same USRID has more than 1 row and if those rows contain the values in PER_ORG or 'EMP' and 'CWR' than I only want to display the 'EMP' record. It has to be this specific combination though, as I still want to include scenarios where USRLID's only contain 1 row in PS_JOB with the value 'CWR' in PER_ORG, and also USRID's with just 1 row with the value 'EMP' in PER_ORG (Also it is possible other values could later be added to this field, but for now I just have these two values attributed to this column).

Below is a query I have that will identify USRID's that have rows with more than 1 distinct PER_ORG value. I'm having trouble incorporating this into my larger query (2nd below), as I still want to pull in other USRID's that don't fall into this scenario.

This SQL will display all rows where the USRID has more than 1 row with different PER_ORG values. I would like to modify it to look specifically for when USRID's have distinct values of 'EMP' AND 'CWR' :

 SELECT *
 FROM PS_JOB B
 WHERE B.USRID IN (SELECT USRID FROM PS_JOB GROUP BY USRID HAVING 
 COUNT(DISTINCT PER_ORG) > 1 )

 AND EFFDT = (SELECT MAX(B_ED.EFFDT) FROM PS_JOB B_ED 
    WHERE B.USRID = B_ED.USRID 
      AND B.USR_RCD = B_ED.USR_RCD 
      AND B_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10)) 
 AND B.ACTION <> 'TER'
--AND PER_ORG = 'EMP'
ORDER BY USRID

I would like to apply similar logic from the above as a case in the below query as I still want to capture additional data (that doesn't fall into the above scenario):

   SELECT A.IMMUN_CODE, B.COMPANY, COUNT(*) AS 'COUNT_OF_EXAMS'
   FROM PS_HS_IMMUN A 
   LEFT OUTER JOIN (PS_JOB B INNER JOIN PS_EMPLMT_SRCH_QRY E1 ON (B.USRID = 
    E1.USRID AND B.EMPL_RCD = E1.EMPL_RCD )) ON A.USRID = B.USRID

   WHERE B.EFFDT = 
    (SELECT MAX(B_ED.EFFDT) FROM PS_JOB B_ED 
     WHERE B.USRID = B_ED.USRID 
     AND B.EMPL_RCD = B_ED.EMPL_RCD 
     AND B_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10)) 
   AND B.EFFSEQ = 
    (SELECT MAX(B_ES.EFFSEQ) FROM PS_JOB B_ES 
     WHERE B.USRID = B_ES.USRID 
     AND B.EMPL_RCD = B_ES.EMPL_RCD 
     AND B.EFFDT = B_ES.EFFDT)
     AND A.HISTORY_ONLY <> 'Y' 
   GROUP BY  A.IMMUN_CODE,  B.COMPANY 

   UNION 

   SELECT 'ZTOTAL', '', COUNT ( D.USRID) 
   FROM (PS_HS_IMMUN D 
   LEFT OUTER JOIN (PS_JOB E INNER JOIN PS_EMPLMT_SRCH_QRY E1 ON (E.USRID = 
    E1.USRID AND E.EMPL_RCD = E1.EMPL_RCD )) ON D.USRID = E.USRID) 
   WHERE ( ( E.EFFDT = 
    (SELECT MAX(E_ED.EFFDT) FROM PS_JOB E_ED 
     WHERE E.USRID = E_ED.USRID 
      AND E.EMPL_RCD = E_ED.EMPL_RCD 
      AND E_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10)) 
    AND E.EFFSEQ = 
     (SELECT MAX(E_ES.EFFSEQ) FROM PS_JOB E_ES 
      WHERE E.USRID = E_ES.USRID 
      AND E.EMPL_RCD = E_ES.EMPL_RCD 
      AND E.EFFDT = E_ES.EFFDT) 
    AND D.HISTORY_ONLY <> 'Y' ))

Hopefully my requirement makes sense, let me know if you would like to see example data. I am looking to avoid using Common Table Expressions if possible.

I am not sure if i have understood you correctly. So you want all records from the PS_job table, then for all records having multiple values in the per_org column you want to prioritize 'EMP' value, but then have other values pull through for records that have only one value?

If so this is how i would start:

    SELECT 
    [USRID],
    [PER_ORG] = COALESCE([PER_ORG],[PER_ORG1])

    FROM (SELECT DISTINCT 
          [USRID],
          [PER_ORG] = CASE WHEN [PER_ORG] = 'EMP' THEN 'EMP' ELSE NULL END,
          [PER_ORG1] 
          FROM [PS_JOB] AS [T1]

          LEFT JOIN (SELECT DISTINCT [USRID],[PER_ORG1] = CASE WHEN PER_ORG 
           = 'CWR' THEN 'CWR' ELSE NULL END) AS [T2]
           [T1].[USRID] = [T2].[USRID]) AS [DATA]

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