一条SQL语句,用于基于年份计算多列总数。

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

One SQL statement for multiple column totals based on year

问题

SELECT
   COUNT(DISTINCT CASE WHEN Year = 2020 THEN Country END) AS "2020",
   COUNT(DISTINCT CASE WHEN Year = 2021 THEN Country END) AS "2021",
   COUNT(DISTINCT Country) AS Total_Unique_Countries
FROM MYTABLE
英文:

I'm trying to return the total number of unique countries listed for each year, and the total number of unique countries in the entire table. The table is formatted like this:

Country | Year | State | V1 | V2 |
 US       2020      NY      9    2
 US       2020      MA      3    6
 CA       2020      MAN     2    8
 CA       2020      ONT     5    1
 AU       2020      TAS     7    2
 AU       2020      VIC     3    3
 US       2021      NY      2    0
 US       2021      MA      8    2
 AU       2021      TAS     4    1
 AU       2021      VIC     5    2

I want my query to return this:

2020  |  2021 | Total_Unique_Countries
3         2               3

I tried:

SELECT
   SUM(CASE WHEN YEAR=2020 THEN 1 ELSE 0 END) AS "2020",
   SUM(CASE WHEN YEAR=2021 THEN 1 ELSE 0 END) AS "2021",
   COUNT(DISTINCT COUNTRY) AS Total_Unique_Countries
FROM MYTABLE GROUP BY YEAR

The result:

2020 | 2021 | Total_Unique_Countries
6       0               3
0       4               2

答案1

得分: 0

在一个公用表达式(CTE)中,您可以首先消除重复项,然后进行计数。

WITH CTE as (SELECT DISTINCT "Country", "Year" FROM MYTABLE)
SELECT
   SUM(CASE WHEN "Year"=2020 THEN 1 ELSE 0 END) AS "2020",
   SUM(CASE WHEN "Year"=2021 THEN 1 ELSE 0 END) AS "2021",
   COUNT(DISTINCT "Country") AS Total_Unique_Countries
FROM CTE
2020 2021 total_unique_countries
3 2 3

这是代码部分的翻译,您可以在执行该代码时得到相应的结果。

<details>
<summary>英文:</summary>

You can first elimnate the duplicates in a CTE and then count


WITH CTE as (SELECT
DISTINCT "Country", "Year" FROM MYTABLE)
SELECT
SUM(CASE WHEN "Year"=2020 THEN 1 ELSE 0 END) AS "2020",
SUM(CASE WHEN "Year"=2021 THEN 1 ELSE 0 END) AS "2021",
COUNT(DISTINCT "Country") AS Total_Unique_Countries
FROM CTE

| 2020 | 2021 | total\_unique\_countries |
|-----:|-----:|-----------------------:|
| 3 | 2 | 3 |
&gt; ``` status
&gt; SELECT 1
&gt; ```

[fiddle](https://dbfiddle.uk/1QrVzLYE)


</details>



# 答案2
**得分**: 0


SELECT
   COUNT(DISTINCT CASE WHEN YEAR=2020 THEN COUNTRY END) AS "2020",
   COUNT(DISTINCT CASE WHEN YEAR=2021 THEN COUNTRY END) AS "2021",
   COUNT(DISTINCT COUNTRY) AS Total_Unique_Countries
FROM MYTABLE

这应该给您所需的结果。

<details>
<summary>英文:</summary>

    SELECT
       COUNT(DISTINCT CASE WHEN YEAR=2020 THEN COUNTRY END) AS &quot;2020&quot;,
       COUNT(DISTINCT CASE WHEN YEAR=2021 THEN COUNTRY END) AS &quot;2021&quot;,
       COUNT(DISTINCT COUNTRY) AS Total_Unique_Countries
    FROM MYTABLE

This should give you the result you are looking for.

</details>



huangapple
  • 本文由 发表于 2023年2月16日 02:51:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464248.html
匿名

发表评论

匿名网友

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

确定