sql-server - 如果 SQL 中的不同列包含多行,如何识别人员或 ID
问题描述
我有一个表,其中一个人在另一列中多次包含相同的值。
例如:
person product portal count indicator
-----------------------------------------------
1 10 5 2 y
1 10 6 2 y
1 15 7 1 y
select person, count(person) over(partition by product)
from table A
如果一个人多次包含相同的产品,那么在这种情况下,我想识别这样的人群并想给它某种类型的指标。
在上面的示例中,产品 10 出现了多次,所以我想为所有行的人提供一个指示符。
解决方案
WITH C AS
(SELECT person, product, count(1) as Duplicate_product
FROM [table] A
GROUP BY person, product),
B AS
(SELECT DISTINCT person
FROM [table] A
GROUP BY person, product
HAVING COUNT(1) > 1)
SELECT t.person, t.product, t.portal,
CASE WHEN B.person is null THEN 'N' ELSE 'Y' END AS indicator, ISNULL(C.Duplicate_product, 1) AS [count]
FROM [table] AS t
INNER JOIN C ON t.person = C.person AND t.product = C.product
LEFT JOIN B ON t.person = B.person;
推荐阅读
- java - 在 Mac 上运行 apache Kafka 的问题
- json - 指定要通过 JSON 返回的视图
- function - 潜在增长模型:使用 semPaths 函数
- r - 在 tibble 中对数字进行计数、排序和过滤
- java - 等待列表的正确方法
> 表示所有操作都已完成 - windows - 有没有一种简单的方法可以通过命令提示符重命名多个文件(按照命名约定)?
- asp.net-core - 是否可以返回一个字典数组?
- python - Asyncio 异步运行一个函数
- flask - Flask Web 应用程序在 Heroku 中崩溃,在我的计算机服务器中很好
- java - Java 中如何在不使用 Arrays 类(例如,不排序)的情况下找到数组中出现频率最高的值?