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

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

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

问题

Here's the translated query:

  1. 我有这个查询的结果。
  2. select distinct "CVE_ID","V19_200","V20","V20_100","V21","V21_100","V21_200"
  3. from cve_table
  4. where "CVE_ID"='CVE-2022-22965';
  5. 我想要的是这样的。
  6. CVE_ID | V19_200 | V20 | V20_100 | V21 | V21_100 | V21_200
  7. ----------------|---------|-----|---------|-----|---------|---------
  8. CVE-2022-22965 | t | t | t | t | t | f
  9. 我尝试过使用`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.

  1. select distinct "CVE_ID","V19_200","V20","V20_100","V21","V21_100","V21_200"
  2. from cve_table
  3. 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如下所示:

  1. SELECT "CVE_ID",
  2. BOOL_OR("V19_200") as "V19_200",
  3. BOOL_OR("V20") as "V20",
  4. BOOL_OR("V20_100") as "V20_100",
  5. BOOL_OR("V21") as "V21",
  6. BOOL_OR("V21_100") as "V21_100",
  7. BOOL_OR("V21_200") as "V21_200"
  8. FROM cve_table
  9. WHERE "CVE_ID" = 'CVE-2022-22965'
  10. GROUP BY "CVE_ID";
英文:

Use BOOL_OR and GROUP BY as follows:

  1. SELECT "CVE_ID",
  2. BOOL_OR("V19_200") as "V19_200",
  3. BOOL_OR("V20") as "V20",
  4. BOOL_OR("V20_100") as "V20_100",
  5. BOOL_OR("V21") as "V21",
  6. BOOL_OR("V21_100") as "V21_100",
  7. BOOL_OR("V21_200") as "V21_200"
  8. FROM cve_table
  9. WHERE "CVE_ID" = 'CVE-2022-22965'
  10. 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:

确定