SQL:在多列上使用GROUP BY和MAX

时间:2021-12-01 12:27:43

Hey, I have an issue with an SQL Query. Lets take this example data

嘿,我有一个SQL查询的问题。让我们以此示例数据为例

itemID  catID  attrib1  attrib2
  1       1       10       5   
  2       1       10       7
  3       1        5      10
  4       2       18      15

I want to return the best item for each category (with attrib1 having priority over attrib2)

我想为每个类别返回最佳项目(attrib1优先于attrib2)

Obviously, "SELECT catID, MAX(attrib1), MAX(attrib2) FROM test_table GROUP BY catID" doesn't work since it will return 10 & 10 for the 1st cat.

显然,“SELECT catID,MAX(attrib1),MAX(attrib2)FROM test_table GROUP BY catID”不起作用,因为它将为第一只猫返回10和10。

So is there anyway to tell MySQL to select max value from attrib2 row but only consider the ones where attrib1 is also max value ? i.e return the following data

那么有没有告诉MySQL从attrib2行选择最大值但只考虑attrib1也是最大值的那些?即返回以下数据

 catID  attrib1  attrib2
   1       10       7   
   2       18      15

4 个解决方案

#1


6  

You can get the best attrib1 values, and then join in the attrib2 values and get the best of those for each attrib1 value:

您可以获得最佳的attrib1值,然后加入attrib2值并获得每个attrib1值的最佳值:

select t2.catID, t2.attrib1, max(t2.attrib2)
from
(
  select catID, max(attrib1) as attrib1
  from test_table
  group by catID
) t1
inner join test_table t2 on t2.catID = t1.catID and t2.attrib1 = t1.attrib1
group by t2.catID, t2.attrib1

#2


1  

SELECT tt.catId, tt.attrib1, MAX(tt.attrib2)
FROM   test_table tt
GROUP BY tt.catID, tt.attrib1
WHERE  tt.attrib1 = (SELECT MAX(t2.attrib1) FROM test_table t2 WHERE t2.catID = tt.catID)

#3


1  

Use:

SELECT x.catid,
       x.max_attrib1 AS attrib1,
       (SELECT MAX(attrib2)
          FROM YOUR_TABLE y
         WHERE y.catid = x.catid
           AND y.attrib1 = x.max_attrib1) AS attrib2
  FROM (SELECT t.catid,
               MAX(t.attrib1) AS max_attrib1
          FROM YOUR_TABLE t
      GROUP BY t.catid) x

#4


-1  

SELECT catID, max1, max2 FROM
((SELECT Max(attrib1) as max1, catID GROUP BY attrib1) as t1
INNER JOIN
(SELECT MAX(attrib2) as max2, catID GROUP BY attrib2) as t2
ON t1.catID = t2.catID) as t3

#1


6  

You can get the best attrib1 values, and then join in the attrib2 values and get the best of those for each attrib1 value:

您可以获得最佳的attrib1值,然后加入attrib2值并获得每个attrib1值的最佳值:

select t2.catID, t2.attrib1, max(t2.attrib2)
from
(
  select catID, max(attrib1) as attrib1
  from test_table
  group by catID
) t1
inner join test_table t2 on t2.catID = t1.catID and t2.attrib1 = t1.attrib1
group by t2.catID, t2.attrib1

#2


1  

SELECT tt.catId, tt.attrib1, MAX(tt.attrib2)
FROM   test_table tt
GROUP BY tt.catID, tt.attrib1
WHERE  tt.attrib1 = (SELECT MAX(t2.attrib1) FROM test_table t2 WHERE t2.catID = tt.catID)

#3


1  

Use:

SELECT x.catid,
       x.max_attrib1 AS attrib1,
       (SELECT MAX(attrib2)
          FROM YOUR_TABLE y
         WHERE y.catid = x.catid
           AND y.attrib1 = x.max_attrib1) AS attrib2
  FROM (SELECT t.catid,
               MAX(t.attrib1) AS max_attrib1
          FROM YOUR_TABLE t
      GROUP BY t.catid) x

#4


-1  

SELECT catID, max1, max2 FROM
((SELECT Max(attrib1) as max1, catID GROUP BY attrib1) as t1
INNER JOIN
(SELECT MAX(attrib2) as max2, catID GROUP BY attrib2) as t2
ON t1.catID = t2.catID) as t3