sql - SQL Count first occurrence
问题描述
My original database looks like that:
TYPE CONTRACT_ID
a 101011
c 101012
b 101011
b 101012
a 101011-1
c 101012
I am trying to get data, grouped by TYPE, counting unique CONTRACT_ID, but some contracts have subcontracts, like 101011 has subcontract 101011-1. All of them have to be counted as one contract.
I have tried distinct and it works but only partially because those subcontracts still being counted as unique entrees.
SELECT TYPE, count(distinct CONTRACT_ID) as countocc
FROM db_address
group by TYPE
I expect output like that:
TYPE countocc
a 1
b 2
c 1
解决方案
完全忽略分包合同怎么样?当您拥有潜艇时,您似乎拥有父合同:
SELECT TYPE, count(distinct CONTRACT_ID) as countocc
FROM db_address
WHERE CONTRACT_ID NOT LIKE '%-%'
GROUP BY TYPE;
推荐阅读
- android - android:背景不适用于主题为 Theme.MaterialComponents.DayNight.DarkActionBar 的按钮
- scala - 在 spark scala 中获取 s3 目录的大小
- r - 如何在 NMDS 排序中绘制超过 2 个维度?
- dynamic - PowerBI中基于Dynamic Measure的Dynamic Bin
- linux - Linux 中的任何操作系统触发器以由用户手动检测系统时间/时区更改
- dockerfile - 如果 xx 或 xx,否则... dockerfile 中的条件
- reactjs - 当使用 Jest & Enzyme 加载 = false 时,如何模拟 Graphql 查询并将加载程序设置为 false 以测试反应组件?
- npm - npm 安装:npm 错误!https://registry.npmjs.org/axios 上的无效 json 响应正文 原因:JSON 输入意外结束
- elasticsearch - 有没有办法在弹性搜索中按字段 A 对前 N 个项目进行排序,然后按字段 B 排序
- avrdude - 大苏尔 M1 上的 Avrdude