sql-server - 选择具有多个非不同父级的重复列
问题描述
我正在创建一个查询,它会给我一个像 Image 2 这样的结果
我创建了以下查询,但它没有给我预期的结果
select v.attributeCd , v.parentObject_id
from Mapping v where v.parentObject_id in (
select distinct(v1.parentObject_id)
from Mapping v1)
group by v.attributeCd , v.parentObject_id
having count(v.attributeCd) > 2 order by attributeCd
解决方案
正如 GMB 提到的,您可以进行窗口计数。
DECLARE @Mapping AS TABLE
(
attributeCd VARCHAR(20)
,parentObject_id INT
)
INSERT INTO @Mapping
(
attributeCd
,parentObject_id
)
VALUES
('AccountNumber',218)
,('AdditionalWeeks',46)
,('AdminCharges',29)
,('AdminCharges',230)
,('AgeCategoryCd',56)
,('AgeCategoryCd',155)
,('AgentDivisionCd',118)
,('AgentNum',275)
,('AgentNum',445)
,('EntryAge',4)
,('ExpiryDt',181)
,('ExpiryDt',184)
,('ExpiryDt',186)
select attributeCd ,parentObject_id
FROM
(select
attributeCd
,parentObject_id
,CNT = COUNT(*) OVER(PARTITION BY attributeCd)
from
@Mapping) AS V
WHERE
V.CNT > 1
ORDER BY
attributeCd
首先了解子查询的结果,以获得查询的全貌。
我希望这有帮助。
推荐阅读
- django - Django 管理面板未正确显示相关外键 - Postgresql
- azure - 即使设置了安全上下文根,Azure 安全中心也会在“容器应以非 root 用户身份运行”时发出高严重性警报
- database - Spring Boot 在 docker 容器中运行时使用 h2 而不是配置的 db
- flutter - Flutter firestore 获取字段包含特定映射键的所有文档
- reactjs - 调度在 UseEffect 内与 Async 函数一起触发
- php - 获取未知属性:humhub\modules\user\models\Group::is_default_group
- firebase - Firebase:应用程序内消息在控制台中显示“错误保存活动”,在网络检查器中显示“呼叫者没有权限”
- python - Youtube API 问题与 python
- list - 使用相同名称时如何避免覆盖列表项?
- java - 如何使用android视频时间戳(java)在某个视频帧触发活动