[英]MySQL GROUP BY MAX two columns
So Ive got a number of tests in a database, each test belongs to a category and each "group" of tests has a "serial number" (timestamp of sorts). 因此,我在数据库中获得了许多测试,每个测试都属于一个类别,每个“组”的测试都有一个“序列号”(排序的时间戳)。 Each test has a pass or fail result, Like so:
每个测试都有通过或失败的结果,就像这样:
TEST Table
TEST_ID TEST_NAME TEST_CAT TEST_SN RESULT
1 test_a Basic 112233 Pass
2 test_b Basic 112233 Pass
3 test_c Basic 112233 Pass
4 test_d Basic 112233 Fail
5 test_e Basic 112233 Pass
6 test_a Basic 223344 Fail
7 test_b Basic 223344 Pass
8 test_c Basic 223344 Pass
9 test_d Basic 223344 Fail
10 test_w Advacned 112233 Fail
11 test_x Advacned 112233 Pass
12 test_y Advacned 112233 Pass
13 test_z Advacned 112233 Pass
Im trying to craft a query to return only the rows for each category where the TEST_SN
is highest. 我试图制作一个查询,以仅返回
TEST_SN
最高的每个类别的行。 So the result Im trying to return would look like this: 因此,我试图返回的结果如下所示:
TEST_ID TEST_NAME TEST_CAT TEST_SN RESULT
6 test_a Basic 223344 Fail
7 test_b Basic 223344 Pass
8 test_c Basic 223344 Pass
9 test_d Basic 223344 Fail
10 test_w Advacned 112233 Fail
11 test_x Advacned 112233 Pass
12 test_y Advacned 112233 Pass
13 test_z Advacned 112233 Pass
Since There are TEST_SN
s "112233" and "223344" for "Basic", I want to ignore all tests from the earlier (smaller) TEST_SN
(rows 1,2,3,4 and 5). 由于
TEST_SN
的“ Basic”有“ 112233”和“ 223344”,因此我想忽略早期(较小) TEST_SN
(第1,2,3,4和5行)中的所有测试。 Since "112233" is the only TEST_SN
for Advacned, I want to keep all of those rows (10,11,12 and 13). 由于“ 112233”是Advacted的唯一
TEST_SN
,因此我想保留所有这些行(10、11、12和13)。
I can come close with this query: 我可以接近以下查询:
SELECT
t.test_id,
t.test_name,
t.test_cat,
MAX(t.test_sn),
t.result
FROM
car_test t
GROUP BY
t.test_name, t.test_cat
But heres what I get: 但是这就是我得到的:
TEST_ID TEST_NAME TEST_CAT TEST_SN RESULT
1 test_a Basic 223344 Pass
2 test_b Basic 223344 Pass
3 test_c Basic 223344 Pass
4 test_d Basic 223344 Fail
5 test_e Basic 112233 Fail
10 test_w Advanced 112233 Fail
11 test_x Advanced 112233 Pass
12 test_y Advanced 112233 Pass
13 test_z Advanced 112233 Pass
Row 5 is still present. 第5行仍然存在。 I want to
SELECT
only rows that have the MAX
TEST_SN
value for each TEST_CAT
我只想为每个
TEST_CAT
只SELECT
具有MAX
TEST_SN
值的TEST_CAT
Any Ideas? 有任何想法吗?
First, we write a query that gets the highest (MAX) test_sn
for each category ( test_cat
). 首先,我们编写一个查询,以获取每个类别(
test_cat
)的最高(MAX) test_sn
。 Like this: 像这样:
SELECT t.test_cat
, MAX(t.test_sn) AS max_test_sn
FROM car_test t
GROUP BY t.test_cat
Then, we can use that query as an inline view in another query. 然后,我们可以将该查询用作另一个查询的内联视图 。 (The query above returns a resultset. We can reference that in place of a table name.) As a simple demonstration:
(上面的查询返回一个结果集。我们可以用它代替表名。)作为一个简单的演示:
SELECT s.test_cat
, s.max_test_sn
FROM ( SELECT t.test_cat
, MAX(t.test_sn) AS max_test_sn
FROM car_test t
GROUP BY t.test_cat
ORDER BY t.test_cat
) s
ORDER BY s.test_cat
Now, we can add a JOIN to the original table, to retrieve only rows that match a row returned by the inline view query. 现在,我们可以将JOIN添加到原始表中,以仅检索与内联视图查询返回的行匹配的行。 For example:
例如:
SELECT r.test_id
, r.test_name
, r.test_cat
, r.test_sn
, r.result
FROM ( SELECT t.test_cat
, MAX(t.test_sn) AS max_test_sn
FROM car_test t
GROUP BY t.test_cat
ORDER BY t.test_cat
) s
JOIN car_test r
ON r.test_cat = s.test_cat
AND r.test_sn = s.max_test_sn
ORDER
BY r.test_id
You should not use test_name in your group by clause. 您不应在group by子句中使用test_name。 As putting test_name there makes a valid group for test-e & Basic for which the max is 112233. Did you try keeping only "test_cat" in your group by clause?
在放置test_name时,将为test-e和Basic创建一个有效的组,最大组为112233。您是否尝试在group by子句中仅保留“ test_cat”?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.