简体   繁体   English

选择具有一个值但没有另一个值的行

[英]Selecting Rows That Have One Value but Not Another

I need a get some rows of two tables with join that shoud have one value in a column (1407) but shouldn't have other value (1403)我需要得到两个表的一些行,它们应该在一列中有一个值(1407)但不应该有其他值(1403)

These is the tables and the query:这些是表和查询:

select a.job, a.date, b.group  from log a inner join active_tmp b
on a.jobno=b.jobno and a.no=b.no where b.list = 'N'
AND LOGDATE = TO_CHAR(TRUNC(SYSDATE),'YYYYMMDD')
and a.job not like 'HOUSE%'
and a.job not like 'CAR%' and (errorCode=1047 and errorCode<>1403);


LOG
JOB     DATE        LOGDATE     JOBNO       NO  errorCode
MAM  20220123       20220125       33       22  1047
MAM  20220123       20220125       33       22  1403
DAD  20220122       20220125       11       99  1047
MAM  20220122       20220125       33       22  0323
DAD  20220122       20220125       11       99  0444   




ACTIVE_TMP
JOB      JOBNO          NO    GROUP     LIST  
MAM         33          22    LAPTOP    N     
MAM         33          22    LAPTOP    N     
DAD         11          99    KEY       N     

But I get:
MAM,20220123,LAPTOP
DAD,20220122,KEY

I need:我需要:

DAD,20220122,KEY

Because MAM have both codes (1047 and 1043).因为 MAM 有两个代码(1047 和 1043)。

You need to change the error code logic.您需要更改错误代码逻辑。 Identify what JOB values has 1403 and then exclude those values确定哪些JOB值具有1403 ,然后排除这些值

select distinct a.job, a.date, b.[group]  from LOG a inner join active_tmp b
on a.jobno=b.jobno and a.no=b.no where b.list = 'N'
AND LOGDATE = TO_CHAR(TRUNC(SYSDATE),'YYYYMMDD')
and a.job not like 'HOUSE%'
and a.job not like 'CAR%' and a.job not in (select JOB from log where  errorCode in(1403));

To rephrase, I think you mean "I want to return matching rows that have error code 1047 but for which the same values of jobno, no, list do not have a corresponding row with error code 1403"换句话说,我认为您的意思是“我想返回具有错误代码 1047 但相同的 jobno, no, list 值没有对应的错误代码为 1403 的行的匹配行”

This part is redundant:这部分是多余的:

AND   (errorCode = 1047 AND errorCode <> 1403);

If you are saying errorCode must be 1047, you are also saying it is not equal to 1403.如果你说 errorCode 必须是 1047,你也说它不等于 1403。

I think you want to select some rows into some result set, then check that there's not another row that disqualifies one of the selected rows from the final result.我认为您想将 select 一些行放入某个结果集中,然后检查是否没有另一行使选定行与最终结果不合格。

So,所以,

SELECT a.job,
       a.date,
       b.group
FROM _log a
  INNER JOIN _active_tmp b
          ON a.jobno = b.jobno
         AND a.no = b.no
WHERE b.list = 'N'
AND   LOGDATE = TO_CHAR(CURRENT_TIMESTAMP,'YYYYMMDD')
AND   a.job NOT LIKE 'HOUSE%'
AND   a.job NOT LIKE 'CAR%'
AND   a.errorCode = 1047
AND   NOT EXISTS (SELECT 1
                  FROM _log c
                    INNER JOIN _active_tmp d
                            ON c.jobno = d.jobno
                           AND c.no = d.no
                  WHERE a.job = c.job
                  AND   a.date = c.date
                  AND   b.group = d.group
                  AND   c.errorCode = 1403)

We select the rows that satisfy the join and have error code 1047 then subtract from that set those rows that also satisfy the join but have error code 1403. You could possibly make this more terse using CTE or a temp table, but this works too.我们 select 满足联接并具有错误代码 1047 的行,然后从该集合中减去那些也满足联接但具有错误代码 1403 的行。您可以使用 CTE 或临时表使其更简洁,但这也有效。

Note I had to change a few things to make it work in my engine (Postgres), so you may have to change a few things back to Oracle.请注意,我必须更改一些内容才能使其在我的引擎 (Postgres) 中运行,因此您可能需要将一些内容更改回 Oracle。

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

相关问题 选择列中只有一个值且具有另一个公用值的所有行 - Selecting all rows with only one value in column with another common value 从表中选择一个字段具有相同值的行 - Selecting rows from a table that have the same value for one field 仅选择一个字段的最大值,并按另一字段分组的行 - Selecting only rows with the highest value of one field, grouped by another field 当甚至一列具有值时选择具有一列的行或具有 NULL 值的列 - Selecting Rows with one column or Columns with NULL value when even one have value 根据另一个行值选择匹配的行 - Selecting rows that match based on another rows value SQL select 具有一个特定值但在同一表中没有另一个的行 - SQL select rows that have one specific value but not another in the same table 选择一列中相同但在另一列中不同的行 - selecting rows that are the same in one column, but different in another 从一个表中选择行,而在MYSQL中从另一个表中选择值和顺序 - Selecting rows from one table where value and order from another in MYSQL SQL选择其中一列的值在另一条件列中相同的行 - SQL selecting rows where one column's value is common across another criteria column 从一个表中选择表行,其中另一个表中的表列的值为x - Selecting table rows from one table where value of table column in another table is x
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM