简体   繁体   English

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. 我试图获取按类型分组的数据,并计算唯一的CONTRACT_ID,但是有些合同有分包合同,例如101011有分包合同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

How about ignoring the subcontracts all-together? 如何完全忽略分包合同? You seem to have the parent contract when you have the subs: 当您拥有子合同时,您似乎拥有父合同:

SELECT TYPE, count(distinct CONTRACT_ID) as countocc
FROM db_address
WHERE CONTRACT_ID NOT LIKE '%-%'
GROUP BY TYPE;

Use a CASE statement to count only the part of contract_id before the '-' (if it exists): 使用CASE语句仅计算'-' (如果存在)之前的contract_id部分:

select 
  type,
  count(distinct 
    case 
      when contract_id like '%-%' then 
        substring(contract_id, 1, instr(contract_id, '-') - 1)
      else contract_id
    end
  ) counter
from db_address
group by type

This covers the case (if there is such a case) that a subcontract is in the table but not the main contract. 这涵盖了分包合同在表中但主合同不在表中的情况(如果有这种情况)。
The code works for MySql but all the functions used can be found in any rdbms. 该代码适用于MySql,但可以在任何rdbms中找到所有使用的功能。
See the demo . 参见演示
Results: 结果:

| type | counter |
| ---- | ------- |
| a    | 1       |
| b    | 2       |
| c    | 1       |

The logic might be extracting the part of the string upto the dash character, if exists, and then grouping by type column. 逻辑可能是提取字符串的一部分直到破折号(如果存在),然后按type列分组。 But method differs depending on the DBMS . 但是方法因DBMS异。

If you're using Oracle , consider : 如果您使用的是Oracle ,请考虑:

select type, 
       count( distinct
             case when instr(contract_id,'-') > 0 then
                  substr(contract_id,1,instr(contract_id,'-')-1)
             else
                  contract_id
             end) as countocc
  from db_address d
 group by type 

If SQL Server , then consider : 如果是SQL Server ,请考虑:

select type, 
       count( distinct
             case when charindex('-',contract_id) > 0 then
                  left(contract_id,charindex('-',contract_id)-1)
             else
                  contract_id
             end) as countocc
  from db_address d
 group by type;

If MySQL , then consider : 如果是MySQL ,请考虑:

select type, 
       count(distinct substring(contract_id,1,instr(contract_id,'-')-1)) as countocc
  from db_address d
 group by type;

If PostGRES , then consider : 如果是PostGRES ,请考虑:

select type, 
       count( distinct
             case when strpos(contract_id,'-') > 0 then
                  substr(contract_id,1,strpos(contract_id,'-')-1)
             else
                  contract_id
             end) as countocc
  from db_address d
 group by type;

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

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