sql - 相互比较行并根据条件仅保留一行
问题描述
我有如下要求:
如果存在超过 1 条评论(姓名、姓氏、门、金额的组)并且其中一条包含,NULL
则仅保留带有NULL
评论的记录并丢弃其他评论。
如果NULL
不是其中之一,并且注释包括NOT AVAILABLE
and REQUIRES
。保留NOT AVAILABLE
丢弃REQUIRES
。
Name Lastname Comment Amount Door
------------------------------------------------------------
John R. NULL 250 1
John R. NULL 250 1
John R. New design is available 250 1
John R. Not available 250 2
John R. Requires additional comment 250 2
John R. XYZ 200 3
John R. Requires more information 200 4
John R. Requires more information 200 4
John R. Requires more information 200 4
John R. ABC 200 4
结果应如下所示:
Name Lastname Comment Amount Door
-------------------------------------------------------------
John R. NULL 250 1
John R. Not available 250 2
John R. XYZ 200 3
John R. Requires more information 200 4
John R. Requires more information 200 4
John R. Requires more information 200 4
John R. ABC 200 4
它应该只检查那些评论超过 1 的行。问题陈述是这样的,它应该检查NULL
评论和不可用的评论并丢弃其他的,但如果这两个不存在,那么数据应该保持原样。
我正在尝试编写 CTE 以获得结果,但不确定如何比较评论部分。像下面的东西
WITH RNs AS
(
SELECT
name,
lastname,
door,
package,
DENSE_RANK() OVER (PARTITION BY name,lastname, comment, amount, door
ORDER BY name, lastname, amount, door ASC) AS RN
FROM
test
)
解决方案
我敢肯定,有人可能会想出一些更优雅的东西,但是这会使用您的示例数据产生所需的输出。
这会根据您对分组分类的要求进行分区,以便按组按顺序对行进行排序,并在每个组内另外按第二个排序标准对not available/requires
评论进行排名。
然后它会为每组创建一个总和,以计算每组的出现次数null/not available
。
然后它从每个组中选择第一行,或者在没有多次出现 null/not available 的情况下选择第一行。
with cte as (
select *, Row_Number() over (
partition by name, lastname, amount, door
order by case when comment like 'not available%' then 1 else case when comment like 'requires%' then 2 else 0 end end
) rn,
Sum(case when comment is null or comment like 'not available%' then 1 else 0 end) over (partition by name, lastname, amount, door) gp
from test
)
select [Name], Lastname, Comment, Amount, Door
from cte
where rn=1 or gp=0
order by door, comment desc
推荐阅读
- python - 如何用 numpy 命令替换列表理解?
- javascript - 我如何使用该值来计算?“10/10”
- uppaal - 如果 Uppaal 去掉相对时间怎么办?
- java - 使用 AES/CBC/NOPADDING 在 Node 中加密并使用相同算法在 JAVA 中解密会产生一些垃圾,例如 e�J�,�d�|*�5Ҝ��
- ruby-on-rails - 找不到带有 'id'=test 的产品
- android - 位图 getpixel 总是返回一个负数
- php - 如何为 Laravel 搜索过滤器功能创建查询语句?
- sql - Power bi 代码作为预计算迁移到 SQL
- javascript - 如何使用 Microsoft Graph 对 Vue.js 渐进式 Web 应用程序进行身份验证
- javascript - 如何从中间开始对数组进行排序?