简体   繁体   English

如何使用单个列值的多个子字符串选择 SQL 表中的行?

[英]How can I select rows in an SQL table by using multiple substrings of a single column value?

I am using Oracle SQL Developer to query a database of cases, and I need to use three different identifiers to select the correct row for each case;我正在使用 Oracle SQL Developer 查询案例数据库,我需要使用三个不同的标识符为每个案例选择正确的行; however, my problem is that two of the identifiers are contained in the same text string, and I can't figure out how to parse them to use in the query.但是,我的问题是两个标识符包含在同一个文本字符串中,我不知道如何解析它们以在查询中使用。

In the following table, the identifiers I need to use are:在下表中,我需要使用的标识符是:

  1. ID-1 , characters 3–6 in the "Case" column (eg, "1001"), ID-1 ,“Case”列中的字符 3–6(例如,“1001”),
  2. ID-2 , characters 8–9 in the "Case" column (eg, "01") when they appear (treated as "00" if they don't appear), and ID-2 ,“Case”列中的字符 8–9(例如,“01”)出现时(如果不出现则视为“00”),以及
  3. ID-3 , the value in the "Sequence" column (eg, "672"). ID-3 ,“序列”列中的值(例如,“672”)。

Source Table源表

Case案件 Sequence序列 Value 1值 1 Value 2值 2
AA1001 AA1001 672 672 73 73 195 195
AA1001 AA1001 711 711 73 73 185 185
AA1001-01 AA1001-01 680 680 73 73 185 185
AA1001-02 AA1001-02 685 685 72 72 185 185
AA1001-02 AA1001-02 699 699 72 72 182 182
AB1002 AB1002 676 676 51 51 36 36
AB1002-01 AB1002-01 701 701 48 48 39 39
AB1002-01 AB1002-01 719 719 48 48 35 35
AB1002-02 AB1002-02 707 707 51 51 38 38
AA1003 AA1003 655 655 122 122 416 416
AA1003 AA1003 683 683 113 113 416 416

I want to return one row for each unique ID-1, such that first the greatest value for ID-2 is selected, and then the greatest value for ID-3 in that subset is chosen;我想为每个唯一的 ID-1 返回一行,这样首先选择 ID-2 的最大值,然后选择该子集中ID-3 的最大值; so, the query should return only the following three rows from the table above.因此,查询应仅返回上表中的以下三行。

Result Table结果表

Case案件 Sequence序列 Value 1值 1 Value 2值 2
AA1001-02 AA1001-02 699 699 72 72 182 182
AB1002-02 AB1002-02 707 707 51 51 38 38
AA1003 AA1003 683 683 113 113 416 416

I've tried taking the maximum for ID-2 using the following, but it only returns the rows where ID-2 equals "02".我尝试使用以下方法为 ID-2 取最大值,但它只返回 ID-2 等于“02”的行。

SELECT * 
FROM table 
WHERE SUBSTR(Case,3,4) in ('1001','1002','1003') 
and SUBSTR(Case,8,2) = (SELECT MAX(SUBSTR(Case,8,2)) 
FROM table 
WHERE SUBSTR(Case,3,4) in ('1001','1002','1003'))

(The easiest answer is probably just to add a column for ID-2; however, the source database is strictly read-only, so I can't make that sort of change.) (最简单的答案可能只是为 ID-2 添加一列;但是,源数据库是严格只读的,因此我无法进行此类更改。)

In Oracle 12.1 and higher, you can use the match_recognize clause:在 Oracle 12.1 及更高版本中,您可以使用match_recognize子句:

select case, sequence, value1, value2
from   (
         select t.*, substr(case, 3, 4) as id_1, substr(case, 8, 9) as id_2
         from   table_name t
       )
match_recognize(
  partition by id_1
  order     by id_2 desc nulls last, sequence desc nulls last
  all rows per match
  pattern   (^ x)
  define    x as null is null
);


CASE        SEQUENCE     VALUE1     VALUE2
--------- ---------- ---------- ----------
AA1001-02        699         72        182
AB1002-02        707         51         38
AA1003           683        113        416

You can use the ROW_NUMBER() analytic function and find the SUBSTR ings to partition/order by:您可以使用ROW_NUMBER()分析函数并通过以下方式找到要分区/排序的SUBSTR ings:

SELECT *
FROM   (
  SELECT t.*,
         ROW_NUMBER() OVER (
           PARTITION BY
             SUBSTR(Case, 3, 4)
           ORDER BY
             COALESCE(SUBSTR(Case, 8, 2), '00') DESC,
             Sequence DESC
         ) AS rn
  FROM   table_name t
)
WHERE rn = 1;

Which, for your sample data:其中,对于您的示例数据:

CREATE TABLE table_name (Case, Sequence, Value1, Value2 ) AS
SELECT 'AA1001',    672,  73, 195 FROM DUAL UNION ALL
SELECT 'AA1001',    711,  73, 185 FROM DUAL UNION ALL
SELECT 'AA1001-01', 680,  73, 185 FROM DUAL UNION ALL
SELECT 'AA1001-02', 685,  72, 185 FROM DUAL UNION ALL
SELECT 'AA1001-02', 699,  72, 182 FROM DUAL UNION ALL
SELECT 'AB1002',    676,  51,  36 FROM DUAL UNION ALL
SELECT 'AB1002-01', 701,  48,  39 FROM DUAL UNION ALL
SELECT 'AB1002-01', 719,  48,  35 FROM DUAL UNION ALL
SELECT 'AB1002-02', 707,  51,  38 FROM DUAL UNION ALL
SELECT 'AA1003',    655, 122, 416 FROM DUAL UNION ALL
SELECT 'AA1003',    683, 113, 416 FROM DUAL;

Outputs:输出:

CASE案件 SEQUENCE序列 VALUE1 VALUE1 VALUE2 VALUE2 RN注册护士
AA1001-02 AA1001-02 699 699 72 72 182 182 1 1
AB1002-02 AB1002-02 707 707 51 51 38 38 1 1
AA1003 AA1003 683 683 113 113 416 416 1 1

db<>fiddle here db<> 在这里摆弄

暂无
暂无

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

相关问题 如何从sql表中选择一行中具有相同值的多行 - How to select a single row from an sql table with multiple rows having same value in a column PL / SQL:如何从3个表中选择行并通过使用特定的列值重新排列行? - PL/SQL:How I can select the rows from the 3 tables and rearrange the rows by using particular column value? 如何从SQL的单个列中选择满足多个条件的行? - How to select rows that meets multiple criteria from a single column in SQL? 如果列的值为 0,我如何选择表的行,否则如果 sql 中的值不为 0,则进行左连接? - How do i select rows of table if value of a column is 0 else do left join if value is not 0 in sql? 我怎样才能 select 多行与同一个表中的其他行的计数在一个单独的列中具有它们的主键? - How can I select multiple rows with the count of other rows in the same table that has their primary keys in a separate column? sql-从同一列中选择多个子字符串 - sql - Select multiple substrings from same column 如何根据顺序从子表/联接表的多行中选择结果集中的一行? - How can I select from multiple rows of a child/join table into a single row in the resultset based on a sequence? SQL选择具有多行值的列的总和 - SQL select sum of column with value in multiple rows 使用SQL Server将多行转换为单列 - Multiple Rows to single column using SQL Server 如何根据列值在单个列中显示表的多个列? - How can i show multiple columns of a table in a single column based on a column value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM