简体   繁体   English

Oracle(SQL 语句)- 将 CASE 与 WHERE CLAUSE 结合使用

[英]Oracle (SQL Statements) - Using CASE with WHERE CLAUSE

Dear Experts, I preparing an SQL report to display the data with Format_Check validations using CASE.尊敬的专家,我准备了一份 SQL 报告,以使用 CASE 显示带有 Format_Check 验证的数据。 I have provided the example,issues and expectation below:我在下面提供了示例、问题和期望:

Table Structure: enter image description here表结构:在此处输入图像描述

SQL Statement: SQL 声明:

SELECT 

ID,PRODUCT,COMMENTS

CASE WHEN SUBSTR(COMMENTS,1,35)=REGEXP_SUBSTR (COMMENTS,'(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\s)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\s)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\s)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)',1,1)
AND
LENGTH(TRIM(REGEXP_SUBSTR (COMMENTS, '(\S*)(\s)', 1, 2)))=8 
AND
REGEXP_LIKE(TRIM(REGEXP_SUBSTR (COMMENTS, '(\S*)(\s)', 1, 1)),'(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)')
THEN 'Correct Format'
ELSE 'Incorrect_Format'
END AS Format_Check

FROM TEST_TABLE

SQL Output: enter image description here SQL Output:在此处输入图像描述

SQL Output Explanation: SQL Output 说明:

  1. Checks whether COMMENTS column is 35 Character long, also checks the format numbers and spaces.检查 COMMENTS 列是否为 35 个字符长,还检查格式数字和空格。 SUBSTR(COMMENTS,1,35)=REGEXP_SUBSTR (COMMENTS,'(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\s)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\s)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\s)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)',1,1) SUBSTR(COMMENTS,1,35)=REGEXP_SUBSTR (COMMENTS,'(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\s) (\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\s)(\d)(\d)(\d)(\ d)(\d)(\d)(\d)(\d)(\s)(\d)(\d)(\d)(\d)(\d)(\d)(\d) (\d)',1,1)

  2. Checks whether the comments after first space is 8 characters long LENGTH(TRIM(REGEXP_SUBSTR (COMMENTS, '(\S*)(\s)', 1, 2)))=8检查第一个空格后的注释是否为 8 个字符长 LENGTH(TRIM(REGEXP_SUBSTR (COMMENTS, '(\S*)(\s)', 1, 2)))=8

  3. Checks the first 8 characters is digit/number.检查前 8 个字符是数字/数字。 REGEXP_LIKE(TRIM(REGEXP_SUBSTR (COMMENTS, '(\S*)(\s)', 1, 1)),'(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)') REGEXP_LIKE(TRIM(REGEXP_SUBSTR (COMMENTS, '(\S*)(\s)', 1, 1)),'(\d)(\d)(\d)(\d)(\d)(\d )(\d)(\d)')

Expectation:期待:

I need only the 'Incorrect Format' comments to be displayed when the SQL is executed, I don't wish to see the 'Correct Format' comments.我只需要在执行 SQL 时显示“格式不正确”注释,我不希望看到“格式正确”注释。 However I need your advise on whether this case be used with WHERE clause to display only incorrect format COMMENTS alone.但是,我需要您就这种情况是否与 WHERE 子句一起使用以仅显示不正确的格式 COMMENTS 提出建议。 I referred previous queries related to WHERE and CASE, however I can't figure out on how pass my case in WHERE clause.我提到了与 WHERE 和 CASE 相关的先前查询,但是我无法弄清楚如何在 WHERE 子句中传递我的案例。 Thanks for your help.谢谢你的帮助。

If you want to display the incorrect format columns, you can wrap your query with another one:如果要显示不正确的格式列,可以用另一个包装查询:

SELECT id, product, comment, format_check
  FROM (
        SELECT id, product, comment, CASE WHEN .... END AS format_check
          FROM test_table
       )
 WHERE format_check = 'Incorrect_Format';

BTW, your question is easier to answer if you show your table and data with SQL instead of an image, for instance like:顺便说一句,如果您使用 SQL 而不是图像显示表格和数据,您的问题会更容易回答,例如:

CREATE TABLE test_table (id NUMBER, product VARCHAR2(10), comments VARCHAR2(50));
INSERT INTO  test_table VALUES (1,'Laptop', '00000001 01012020 02022020 03032020');
INSERT INTO  test_table VALUES (1,'PC',     '   00000001 01012020 02022020 0034');

Furthermore, I believe the regular expression can be simplified.此外,我相信正则表达式可以简化。 Please provide a bit more detail if you're interested...如果您有兴趣,请提供更多详细信息...

If you do not wish to see the correct format in the result at all then you can omit the calculation of the format in select clause.如果您根本不希望在结果中看到正确的格式,那么您可以省略select子句中的格式计算。 Rather use it in Where clause as follows:而是在Where子句中使用它,如下所示:

SELECT ID,
       PRODUCT,
       COMMENTS,
      'Incorrect_Format' AS format_check
  FROM TEST_TABLE
 WHERE CASE WHEN SUBSTR(COMMENTS,1,35)=REGEXP_SUBSTR (COMMENTS,'(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\s)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\s)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\s)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)',1,1)
AND
LENGTH(TRIM(REGEXP_SUBSTR (COMMENTS, '(\S*)(\s)', 1, 2)))=8 
AND
REGEXP_LIKE(TRIM(REGEXP_SUBSTR (COMMENTS, '(\S*)(\s)', 1, 1)),'(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)')
THEN 'Correct Format'
ELSE 'Incorrect_Format'
END AS = 'Incorrect_Format'

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

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