根据条件选择组中的不同行

Selecting Different Rows in a Group based on condition

我在 selecting 组中的行时遇到以下问题: 我有来自不同数据源的记录,每个数据源都有分配给它的优先级。 每当我有两个具有不同数据源的相同记录时,我想 select 具有最低优先级的记录。 每当我有来自同一数据源的重复记录,但 VALUE 列中有不同的条目时,我想 select 带有 't' 的行(如果有多个,取第一个)。

这是一个示例数据集:

预期结果:

到目前为止,我所做的是 select 那些在组内优先级最低的条目。 这是一个代码示例:

WITH Product AS (
SELECT 1 id, 'A' code, 'f' value, 'S1' datasource, 1 prio FROM DUAL 
UNION ALL
SELECT 1 id, 'B' code, 'f' value, 'S2' datasource, 2 prio FROM DUAL 
UNION ALL 
SELECT 3 id, 'C' code, 'f' value, 'S3' datasource, 3 prio FROM DUAL 
UNION ALL 
SELECT 2 id, 'D' code, 'f' value, 'S1' datasource, 1 prio FROM DUAL 
UNION ALL 
SELECT 2 id, 'E' code, 't' value, 'S1' datasource, 1 prio FROM DUAL 
),
extended_product AS
(
SELECT p.*, ROW_NUMBER() OVER(PARTITION BY p.ID ORDER BY p.PRIO ASC) AS ROW_NUMBER
FROM Product p 
)
SELECT ep.*
FROM extended_product ep
WHERE ROW_NUMBER = 1;

我不知道如何做到这两个条件,select 最低优先级并且如果组内的所有条目都具有相同的数据源,则只采用具有 't' 的那些(并采用如果有几个,第一个)。

我想你很接近了。 row_number 遗漏了 value 部分:

ROW_NUMBER () OVER (PARTITION BY p.ID ORDER BY p.PRIO ASC, VALUE DESC) AS ROW_NUMBER

SQL> WITH
  2     Product
  3     AS
  4        (SELECT 1 id, 'A' code, 'f' VALUE, 'S1' datasource, 1 prio FROM DUAL
  5         UNION ALL
  6         SELECT 1 id, 'B' code, 'f' VALUE, 'S2' datasource, 2 prio FROM DUAL
  7         UNION ALL
  8         SELECT 3 id, 'C' code, 'f' VALUE, 'S3' datasource, 3 prio FROM DUAL
  9         UNION ALL
 10         SELECT 2 id, 'D' code, 'f' VALUE, 'S1' datasource, 1 prio FROM DUAL
 11         UNION ALL
 12         SELECT 2 id, 'E' code, 't' VALUE, 'S1' datasource, 1 prio FROM DUAL),
 13     extended_product
 14     AS
 15        (SELECT p.*,
 16                ROW_NUMBER ()
 17                   OVER (PARTITION BY p.ID ORDER BY p.PRIO ASC, VALUE DESC) AS ROW_NUMBER
 18           FROM Product p)
 19    SELECT ep.*
 20      FROM extended_product ep
 21     WHERE ep.ROW_NUMBER = 1
 22  ORDER BY ID, PRIO;

        ID C V DA       PRIO ROW_NUMBER
---------- - - -- ---------- ----------
         1 A f S1          1          1
         2 E t S1          1          1
         3 C f S3          3          1

SQL>

正如 littlefoot 在他们的回答中提到的那样,根据您的要求,如果特定 ID 的相同优先级超过 1 行,我们将另外按 VALUE(降序)排序。

WITH Product AS 
(
SELECT 1 id, 'A' code, 'f' value, 'S1' datasource, 1 prio 
UNION ALL
SELECT 1 id, 'B' code, 'f' value, 'S2' datasource, 2 prio 
UNION ALL 
SELECT 3 id, 'C' code, 'f' value, 'S3' datasource, 3 prio 
UNION ALL 
SELECT 2 id, 'D' code, 'f' value, 'S1' datasource, 1 prio 
UNION ALL 
SELECT 2 id, 'E' code, 't' value, 'S1' datasource, 1 prio 
),
extended_product AS
(
  SELECT 
    p.*,
    RANK() OVER (PARTITION BY p.ID ORDER BY p.PRIO ASC, value DESC) as rank_priority
  FROM Product p 
)
SELECT ep.* FROM extended_product ep WHERE rank_priority = 1;