如何获取第二大列值和列名

How to get second largest column value and column name

如何获得第二大列值及其名称?

我当前的查询大部分是正确的,但在最大和第二大值相同的情况下,我得到的值是错误的。

    select item_code, A, B, C, 
        greatest(A, B, C) as largest1, 
        greatest(case when largest1 = A then 0 else A end,
                 case when largest1 = B then 0 else B end,
                 case when largest1 = C then 0 else C end) as largest2,
        (case largest1 when A then 'A'
                       when B then 'B'
                       when C then 'C' end) as largest1_column_name,
        (case largest2 when A then 'A'
                       when B then 'B'
                       when C then 'C' else 'None' end) as largest2_column_name 
        from table1 

下面是示例 table:

+-----------+----+----+----+
| item_code | A  | B  | C  |
+-----------+----+----+----+
| p1        | 20 | 30 | 40 |
| p2        | 50 | 30 | 10 |
| p3        | 30 | 50 | 10 |
| p4        | 30 | 30 | 30 |
| p5        | 50 | 50 | 10 |
| p6        |  0 |  0 |  0 |
+-----------+----+----+----+

以下是预期输出:

+-----------+----+----+----+----------+----------+----------------------+----------------------+
| item_code | A  | B  | C  | largest1 | largest2 | largest1_column_name | largest2_column_name |
+-----------+----+----+----+----------+----------+----------------------+----------------------+
| p1        | 20 | 30 | 40 |       40 |       30 | C                    | B                    |
| p2        | 50 | 30 | 10 |       50 |       30 | A                    | B                    |
| p3        | 30 | 50 | 10 |       50 |       30 | B                    | A                    |
| p4        | 30 | 30 | 30 |       30 |       30 | A                    | B                    |
| p5        | 50 | 50 | 10 |       50 |       50 | A                    | B                    |
| p6        |  0 |  0 |  0 |        0 |        0 | A                    | B                    |
+-----------+----+----+----+----------+----------+----------------------+----------------------+

这是我从查询中得到的输出(我已将错误标记为评论):

+-----------+----+----+----+----------+-------------+----------------------+----------------------+
| item_code | A  | B  | C  | largest1 |  largest2   | largest1_column_name | largest2_column_name |
+-----------+----+----+----+----------+-------------+----------------------+----------------------+
| p1        | 20 | 30 | 40 |       40 | 30          | C                    | B                    |
| p2        | 50 | 30 | 10 |       50 | 30          | A                    | B                    |
| p3        | 30 | 50 | 10 |       50 | 30          | B                    | A                    |
| p4        | 30 | 30 | 30 |       30 | 0/*wrong*/  | A                    | NULL/*wrong*/        |
| p5        | 50 | 50 | 10 |       50 | 10/*wrong*/ | A                    | C/*wrong*/           |
| p6        |  0 |  0 |  0 |        0 | 0/*wrong*/  | A                    | A/*wrong*/           |
+-----------+----+----+----+----------+-------------+----------------------+----------------------+

这可能通过对行进行逆透视、对值进行排名然后使用条件聚合来更简单地实现。在 Postgres 中,您可以这样表述:

select t.*, x.*
from table1 t1
cross join lateral (
    select 
        min(val) filter(where rn = 1) largest1,
        min(val) filter(where rn = 2) largest2,
        min(col) filter(where rn = 1) largest1_column_name,
        min(col) filter(where rn = 2) largest2_column_name
    from (
        select x.*, dense_rank() over(order by val desc) rn
        from (values ('a', a), ('b', b), ('c', c)) as x(col, val)
    ) x
) x

我在 Snowflake 中尝试了一些细微的变化(listagg 而不是 string_agg),它似乎得到了预期的结果

with cte (item_code, abc, id) as
(select item_code, a, 'a' from table1 union all
 select item_code, b, 'b' from table1 union all
 select item_code, c, 'c' from table1)
 
select item_code, 
       max(case when id='a' then abc end) a,
       max(case when id='b' then abc end) b,
       max(case when id='c' then abc end) c,
       split_part(string_agg(abc::varchar,',' order by abc desc),',',1) largest1,
       split_part(string_agg(abc::varchar,',' order by abc desc),',',2) largest2,
       split_part(string_agg(id,',' order by abc desc),',',1) largest1_col,
       split_part(string_agg(id,',' order by abc desc),',',2) largest2_col
from cte
group by item_code;