简体   繁体   English

在 SQL 中聚合行

[英]Aggregating rows in SQL

I want to select 'Y' or 'N' over 'U'- whenever there is a two-row of the same id.我想在 'U' 上选择 'Y' 或 'N' - 只要有两行相同的 id。 However, I want to retain 'U' when there is only one row of an id.但是,当只有一行 id 时,我想保留 'U'。 Can anyone show an efficient approach to aggregate the following table:任何人都可以展示一种有效的方法来汇总下表:

在此处输入图片说明

into something like this:变成这样:

在此处输入图片说明

I tried the MAX function but it only retains value in alphabetical order and 'U' happens to be in the middle of 'Y' and 'N' therefore MAX function doesn't work as I intended.我尝试了 MAX 函数,但它只按字母顺序保留值,而 'U' 恰好位于 'Y' 和 'N' 的中间,因此 MAX 函数无法按我的预期工作。

Happy to hear your thoughts.很高兴听到你的想法。

You can use window functions:您可以使用窗口函数:

select id, ind
from (
    select t.*, row_number() over(
        partition by id 
        order by case ind
            when 'Y' then 1
            when 'N' then 2
            when 'U' then 3
            else 4           -- is this possible?
        end
    ) rn
    from mytable t
) t
where rn = 1

Alternatively, we can turn the strings to numbers, pick the preferred value, and then translate back to the original string :或者,我们可以将字符串转换为数字,选择首选值,然后转换回原始字符串:

select id,
    case min(
        case ind
            when 'Y' then 1
            when 'N' then 2
            when 'U' then 3
        end
    )
        when 1 then 'Y'
        when 2 then 'N'
        when 3 then 'U'
        else '??'
    end as ind
from mytable
group by id

Another approach is aggregation:另一种方法是聚合:

select id,
       coalesce(max(case when ind = 'Y' then ind end),
                max(case when ind = 'N' then ind end),
                max(case when ind = 'U' then ind end)
               )
from t
group by id;

This simply runs the logic:这只是运行逻辑:

  • If there is a 'Y' , return the 'Y' .如果有'Y' ,则返回'Y'
  • Otherwise, if there is an 'N' return the 'N' .否则,如果有'N'返回'N'
  • Otherwise, if there is a 'U' , return the 'U' .否则,如果有'U' ,则返回'U'

Assuming the fact that you have to select from 'U', 'Y' and 'N' and there are utmost 2 of them present you can simply use Max function with group by.假设您必须从 'U'、'Y' 和 'N' 中进行选择,并且存在最多2 个,您可以简单地将Max函数与 group by 一起使用。

SELECT id, MAX(Ind)
FROM mytable
GROUP BY id
order by id

This query will work with most of the databases.此查询适用于大多数数据库。 Be careful with using the above, though its simple and small but it has a lot of constraints.小心使用上面的,虽然它简单小,但它有很多限制。 Do test it thoroughly before going into production and take all the test cases into account.在投入生产之前彻底测试它并考虑所有测试用例。

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

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