简体   繁体   English

选择具有相同ID但另一列中具有不同值的行

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

I have a table (PS_JOB) that contains User data. 我有一个包含用户数据的表(PS_JOB)。 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. 一个用户ID(USRID)在此表中可以有多于一行,因此在另一列(在同一表中)称为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. 当相同的USRID有多于1行并且如果这些行包含PER_ORG或'EMP'和'CWR'中的值时,我只想显示'EMP'记录。 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). 不过,这必须是这种特定的组合,因为我仍然想包括这样的方案,其中USRLID在PS_JOB中仅包含1行,在PER_ORG中包含值'CWR',而USRID在PER_ORG中仅包含1行的值'EMP'(也可能以后可以将其他值添加到该字段中,但是现在我仅将这两个值归于此列)。

Below is a query I have that will identify USRID's that have rows with more than 1 distinct PER_ORG value. 以下是我要查询的查询,该查询将标识具有多于1个不同PER_ORG值的行的USRID。 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. 我在将其合并到我的较大查询中时遇到困难(下面第二条),因为我仍然想引入其他不属于这种情况的USRID。

This SQL will display all rows where the USRID has more than 1 row with different PER_ORG values. 该SQL将显示USRID具有多于1行且具有不同PER_ORG值的所有行。 I would like to modify it to look specifically for when USRID's have distinct values of 'EMP' AND 'CWR' : 我想对其进行修改,使其专门用于USRID具有'EMP'和'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? 因此,您需要PS_job表中的所有记录,然后对于在per_org列中具有多个值的所有记录,您要优先考虑“ EMP”值,但是对于只有一个值的记录,是否需要其他值?

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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何选择第1列中具有相同值但第2列中具有不同值的行? - How to SELECT rows that has same values in column 1 but different values in column 2? 选择具有相同ID但在另一列中具有不同值的行 - Select rows with same id but different value in another column 选择具有相同外键但另一列具有一组值的行 - Select rows with the same foreign key but another column has a set of values 在多行中查找同一列中具有相同值的行,而另一列具有不同值 - Find row that has same value in one column over multiple rows while another column has different values 从表中选择行,其中具有相同id的另一个表中的行在另一列中具有特定值 - Select rows from a table where row in another table with same id has a particular value in another column 选择具有相同ID但在其他列中不同的值 - select values with same id but different in other column MySQL选择具有相同id且在列中具有不同值的不同行,并在另一列中排除特定值 - MySQL Select distinct rows with same id with different value in a column and excluded a specific value in another column 选择其他列中具有相同ID但值不同的行并指定列值 - select rows with same id but different value in other column and specify column values 如何重复根据列值选择行,并且在另一列中具有不同的值 - How to select rows based on column value is repeating and has different values in another column ORACLE SQL:根据另一列中的相同键值选择基于一列中不同值的多行 - ORACLE SQL: select multiple rows basd on different values in one column for a same key value in another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM