查询第一个元素出现的 select 行
Query to select row with first occurrence of an element
我有以下 SQL 查询来自我正在尝试编译的一些数据:
SELECT State, CountyName, Month, SUM(Sum_Confirmed) AS Max_Sum_Confirmed
FROM `covid_by_countynmonth`
GROUP BY State, Month
ORDER BY State, Max_Sum_Confirmed DESC
它给了我以下 table:
我怎样才能让它只显示每个州的第一条记录?
我想要这样的东西(我通过添加 WHERE Month="July" 得到它,但这只是为了这个例子):
显然这只适用于 postgresql。
将 DISTINCT ON 用于 select 不同的值,如下所示:
SELECT DISTINCT ON (COLUMN_OF_INTEREST) COLUMN_ALIAS, ...
然后初始查询变成这样:
SELECT DISTINCT ON (SUB_.State) SUB_.State, SUB_.Month, SUB_.Max_Sum_Confirmed
FROM (SELECT State, Month, SUM(Sum_Confirmed) AS Max_Sum_Confirmed
FROM `covid_by_countynmonth`
GROUP BY State, Month) AS SUB_ ORDER BY SUB_.State, SUB_.Max_Sum_Confirmed DESC
How can I have it so that it only shows the first record of each State?
使用window函数:
SELECT sm.*
FROM (SELECT State, Month,
SUM(Sum_Confirmed) AS Max_Sum_Confirmed,
ROW_NUMBER() OVER (PARTITION BY State ORDER BY SUM(SUM_Confirmed) DESC) as seqnum
FROM `covid_by_countynmonth`
GROUP BY State, Month
) sm
WHERE seqnum = 1;
ORDER BY State, Max_Sum_Confirmed DESC ;
CountyName
似乎是多余的所以我删除了它。
这是一个包含两个输出的示例:来自月的值和来自州的 SUM
SELECT
c.State , c.CountyName
, MONTHNAME(minMonth) as 'Month'
, c.Sum_Confirmed -- SUM othe State der
, f.Sum_Confirmed -- only from the first Month
FROM (
SELECT
State, CountyName
, MIN(str_to_date(`Month`,'%M')) as minMonth
, SUM(Sum_Confirmed) as Sum_Confirmed
FROM covid_by_countynmonth
GROUP BY State
) AS c
LEFT JOIN covid_by_countynmonth AS f ON c.State = f.State AND f.`Month` = MONTHNAME(minMonth);
样本
MariaDB [Bernd]> SELECT * FROM covid_by_countynmonth;
+----+-------+------------+---------+---------------+
| id | State | CountyName | Month | Sum_Confirmed |
+----+-------+------------+---------+---------------+
| 1 | AR | Arkansas | July | 21502 |
| 2 | AR | Arkansas | June | 13244 |
| 3 | LA | Acadia | July | 40500 |
| 4 | LA | Acadia | May | 11997 |
| 5 | LA | Acadia | January | 999 |
+----+-------+------------+---------+---------------+
5 rows in set (0.10 sec)
MariaDB [Bernd]> SELECT
-> c.State , c.CountyName
-> , MONTHNAME(minMonth) as 'Month'
-> , c.Sum_Confirmed -- SUM othe State der
-> , f.Sum_Confirmed -- only from the first Month
-> FROM (
-> SELECT
-> State, CountyName
-> , MIN(str_to_date(`Month`,'%M')) as minMonth
-> , SUM(Sum_Confirmed) as Sum_Confirmed
-> FROM covid_by_countynmonth
-> GROUP BY State
-> ) AS c
-> LEFT JOIN covid_by_countynmonth AS f ON c.State = f.State AND f.`Month` = MONTHNAME(minMonth);
+-------+------------+---------+---------------+---------------+
| State | CountyName | Month | Sum_Confirmed | Sum_Confirmed |
+-------+------------+---------+---------------+---------------+
| AR | Arkansas | June | 34746 | 13244 |
| LA | Acadia | January | 53496 | 999 |
+-------+------------+---------+---------------+---------------+
2 rows in set (0.05 sec)
MariaDB [Bernd]>
我有以下 SQL 查询来自我正在尝试编译的一些数据:
SELECT State, CountyName, Month, SUM(Sum_Confirmed) AS Max_Sum_Confirmed
FROM `covid_by_countynmonth`
GROUP BY State, Month
ORDER BY State, Max_Sum_Confirmed DESC
它给了我以下 table:
我怎样才能让它只显示每个州的第一条记录?
我想要这样的东西(我通过添加 WHERE Month="July" 得到它,但这只是为了这个例子):
显然这只适用于 postgresql。
将 DISTINCT ON 用于 select 不同的值,如下所示:
SELECT DISTINCT ON (COLUMN_OF_INTEREST) COLUMN_ALIAS, ...
然后初始查询变成这样:
SELECT DISTINCT ON (SUB_.State) SUB_.State, SUB_.Month, SUB_.Max_Sum_Confirmed
FROM (SELECT State, Month, SUM(Sum_Confirmed) AS Max_Sum_Confirmed
FROM `covid_by_countynmonth`
GROUP BY State, Month) AS SUB_ ORDER BY SUB_.State, SUB_.Max_Sum_Confirmed DESC
How can I have it so that it only shows the first record of each State?
使用window函数:
SELECT sm.*
FROM (SELECT State, Month,
SUM(Sum_Confirmed) AS Max_Sum_Confirmed,
ROW_NUMBER() OVER (PARTITION BY State ORDER BY SUM(SUM_Confirmed) DESC) as seqnum
FROM `covid_by_countynmonth`
GROUP BY State, Month
) sm
WHERE seqnum = 1;
ORDER BY State, Max_Sum_Confirmed DESC ;
CountyName
似乎是多余的所以我删除了它。
这是一个包含两个输出的示例:来自月的值和来自州的 SUM
SELECT
c.State , c.CountyName
, MONTHNAME(minMonth) as 'Month'
, c.Sum_Confirmed -- SUM othe State der
, f.Sum_Confirmed -- only from the first Month
FROM (
SELECT
State, CountyName
, MIN(str_to_date(`Month`,'%M')) as minMonth
, SUM(Sum_Confirmed) as Sum_Confirmed
FROM covid_by_countynmonth
GROUP BY State
) AS c
LEFT JOIN covid_by_countynmonth AS f ON c.State = f.State AND f.`Month` = MONTHNAME(minMonth);
样本
MariaDB [Bernd]> SELECT * FROM covid_by_countynmonth;
+----+-------+------------+---------+---------------+
| id | State | CountyName | Month | Sum_Confirmed |
+----+-------+------------+---------+---------------+
| 1 | AR | Arkansas | July | 21502 |
| 2 | AR | Arkansas | June | 13244 |
| 3 | LA | Acadia | July | 40500 |
| 4 | LA | Acadia | May | 11997 |
| 5 | LA | Acadia | January | 999 |
+----+-------+------------+---------+---------------+
5 rows in set (0.10 sec)
MariaDB [Bernd]> SELECT
-> c.State , c.CountyName
-> , MONTHNAME(minMonth) as 'Month'
-> , c.Sum_Confirmed -- SUM othe State der
-> , f.Sum_Confirmed -- only from the first Month
-> FROM (
-> SELECT
-> State, CountyName
-> , MIN(str_to_date(`Month`,'%M')) as minMonth
-> , SUM(Sum_Confirmed) as Sum_Confirmed
-> FROM covid_by_countynmonth
-> GROUP BY State
-> ) AS c
-> LEFT JOIN covid_by_countynmonth AS f ON c.State = f.State AND f.`Month` = MONTHNAME(minMonth);
+-------+------------+---------+---------------+---------------+
| State | CountyName | Month | Sum_Confirmed | Sum_Confirmed |
+-------+------------+---------+---------------+---------------+
| AR | Arkansas | June | 34746 | 13244 |
| LA | Acadia | January | 53496 | 999 |
+-------+------------+---------+---------------+---------------+
2 rows in set (0.05 sec)
MariaDB [Bernd]>