将多行查询结果合并为一行的方法

huangapple go评论58阅读模式
英文:

how to combine results of query of multiple rows into one row

问题

Here's the translated query:

我有这个查询的结果。

select distinct "CVE_ID","V19_200","V20","V20_100","V21","V21_100","V21_200" 
from cve_table 
where "CVE_ID"='CVE-2022-22965';

我想要的是这样的。

CVE_ID        | V19_200 | V20 | V20_100 | V21 | V21_100 | V21_200 
----------------|---------|-----|---------|-----|---------|---------
CVE-2022-22965 | t       | t   | t       | t   | t       | f

我尝试过使用`UNION``UNION ALL`,但对我来说没有起作用,或者我没有找到正确的方法。

Please note that the translated SQL code retains the code parts and format from your original query.

英文:

I have this results from this query.

select distinct "CVE_ID","V19_200","V20","V20_100","V21","V21_100","V21_200" 
from cve_table 
where "CVE_ID"='CVE-2022-22965';
CVE_ID V19_200 V20 V20_100 V21 V21_100 V21_200
CVE-2022-22965 f f f f f f
CVE-2022-22965 f f f f t f
CVE-2022-22965 f f f t f f
CVE-2022-22965 f f t f f f
CVE-2022-22965 f t f f f f
CVE-2022-22965 t f f f f f

What I want is this.

CVE_ID V19_200 V20 V20_100 V21 V21_100 V21_200
CVE-2022-22965 t t t t t f

I have tried to UNION and UNION ALL but it didn't work for me or I couldn't do it the right way.

答案1

得分: 2

使用BOOL_ORGROUP BY如下所示:

SELECT "CVE_ID", 
       BOOL_OR("V19_200") as "V19_200", 
       BOOL_OR("V20") as "V20", 
       BOOL_OR("V20_100") as "V20_100", 
       BOOL_OR("V21") as "V21", 
       BOOL_OR("V21_100") as "V21_100", 
       BOOL_OR("V21_200") as "V21_200" 
FROM cve_table 
WHERE "CVE_ID" = 'CVE-2022-22965' 
GROUP BY "CVE_ID";
英文:

Use BOOL_OR and GROUP BY as follows:

SELECT "CVE_ID", 
       BOOL_OR("V19_200") as "V19_200", 
       BOOL_OR("V20") as "V20", 
       BOOL_OR("V20_100") as "V20_100", 
       BOOL_OR("V21") as "V21", 
       BOOL_OR("V21_100") as "V21_100", 
       BOOL_OR("V21_200") as "V21_200" 
FROM cve_table 
WHERE "CVE_ID" = 'CVE-2022-22965' 
GROUP BY "CVE_ID";

huangapple
  • 本文由 发表于 2023年5月14日 15:50:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定