简体   繁体   English

COUNT个同一列的多种类型

[英]COUNT multiple types of same column

In my current query: 在我当前的查询中:

SELECT COUNT(WC.ID) AS "Regions" 
FROM WHOLE_FEATURES_PDB_CHAINS AS WC 
;

I COUNT(WC.ID) AS "Regions" . 我将COUNT(WC.ID) AS "Regions" However, we have multiple regions with WC.Type can be 1,2,3,4 . 但是,我们有多个具有WC.Type的区域,可以是1,2,3,4 I need to count each type occurrence into COUNT(WC.ID) AS "Region_1" , COUNT(WC.ID) AS "Region_2" ... depending on WC.Type . 我需要将每种类型出现的次数都计入COUNT(WC.ID) AS "Region_1"COUNT(WC.ID) AS "Region_2" ...,具体取决于WC.Type Is there any way to solve this in one query? 有什么办法可以在一个查询中解决这个问题? I am looking at MySQL IF , yet do not know how to integrate it into the count function. 我正在查看MySQL IF ,但不知道如何将其集成到count函数中。

I need it to be in one row (the shown query here is reduced, it's a larger query) 我需要将其放在一行中(此处显示的查询已减少,这是一个较大的查询)

SELECT COUNT(WC.ID) AS "Region_1" , COUNT(WC.ID) AS "Region_2" ...

Here is the complete query if anyone is interested: 如果有人感兴趣,这是完整的查询:

SELECT PCS.PDB_id, PCS.Chain, PPA.ENSEMBL_start, PPA.ENSEMBL_end, PPA.eValue, PIN.TITLE AS "pdbTitle", COUNT(WC.ID) AS "Regions" 
FROM PDB_Chains AS PCS 
LEFT JOIN WHOLE_FEATURES_PDB_CHAINS AS WC ON WC.PDB_CHAIN_ID = PCS.idPDB_chains, PDB_protein_alignment PPA, PDB_INFOS PIN 
WHERE PCS.idPDB_chains = PPA.idPDB_Chains 
AND PCS.PDB_id = PIN.PDB_ID 
AND PPA.idProteins = (SELECT idProteins from Proteins WHERE ENSEMBL_protein_id = "'+submittedID+'") 
GROUP BY PCS.PDB_id, PCS.Chain ORDER BY PCS.PDB_id;

Here's the working solutin based on your kind answers 这是根据您的喜好而定的解决方案

SELECT PIN.TITLE AS "pdbTitle", COUNT(CASE WHEN WC.STRUCTURAL_FEATURES_ID = 1 then 1 end) AS "PPInterface" , COUNT(CASE WHEN WC.STRUCTURAL_FEATURES_ID = 4 then 1 end) AS "flexibleRegions" 
FROM PDB_Chains AS PCS LEFT JOIN WHOLE_FEATURES_PDB_CHAINS AS WC ON WC.PDB_CHAIN_ID = PCS.idPDB_chains, PDB_protein_alignment PPA, PDB_INFOS PIN 
WHERE PCS.idPDB_chains = PPA.idPDB_Chains 
AND PCS.PDB_id = PIN.PDB_ID 
AND PPA.idProteins = (SELECT idProteins from Proteins WHERE ENSEMBL_protein_id = "ENSP00000256078.4") 
GROUP BY PCS.PDB_id, PCS.Chain ORDER BY PCS.PDB_id;
Select
...
...
sum(if WC.ID = 1 then 1 else 0) as Region1,
sum(if WC.ID = 2 then 1 else 0) as Region2,
sum(if WC.ID = 3 then 1 else 0) as Region3,
sum(if WC.ID = 4 then 1 else 0) as Region4

Might do what you want. 可能会做您想要的。

You can use case when statement inside your aggregate function. 您可以在聚合函数中使用case when语句。

Try this . 尝试这个 。

count(case when WC.type = 1 then 1 end) as region_1, similarly repeat for another column. count(WC.type = 1然后1结束的情况)作为region_1,类似地对另一列重复。

You can use GROUP BY with COUNT to get the required result, eg: 您可以将GROUP BYCOUNT以获取所需的结果,例如:

SELECT WC.Type, COUNT(WC.ID) AS "Regions" 
FROM WHOLE_FEATURES_PDB_CHAINS AS WC 
GROUP BY WC.Type;

Update 更新资料

If you want the counts as pivoted column for each region then you can write inner SELECT queries, eg: 如果要将计数作为每个区域的枢轴列,则可以编写内部SELECT查询,例如:

SELECT
 (SELECT COUNT(ID) FROM WHOLE_FEATURES_PDB_CHAINS WHERE type = 1) AS "Region_1",
 (SELECT COUNT(ID) FROM WHOLE_FEATURES_PDB_CHAINS WHERE type = 2) AS "Region_2",
other_column
FROM WHOLE_FEATURES_PDB_CHAINS AS WC
WHERE <some condition>;

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

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