mySQL Largest number by group

牧云@^-^@ 提交于 2019-12-13 10:53:34

问题


I have messed around with this code for quite sometime. First thing I have my SQL table setup as char instead of decimal because I don't want the number to always show a decimal value unless it has a decimal value (Is there another way to do this?). I change it to decimal within my code but I believe this is where the problem lies. I am trying to pull the largest fish per user per species but it isn't working out.

SELECT * FROM (SELECT * FROM entries ORDER BY CAST(weight AS DECIMAL(9,3)) DESC) tmp WHERE username = :user GROUP BY species

EDIT

I have changed my table to use decimal(10,3) instead of char and have been trying this formula

SELECT * FROM (SELECT * FROM entries ORDER BY CAST(weight AS INT) DESC) tmp WHERE username = 'BooF' GROUP BY species

But for some reason it returns this

username    location       species  date    length  weight  timestamp   id
BooF    Muskellunge Lake    Black Crappie   2014-08-31  9.125   0.37    2014-12-20 10:48:06 13
BooF    Black Lake  Largemouth Bass 2014-07-03  16.75   2.62    2014-12-20 10:49:00 2
BooF    Muskellunge Lake    Northern Pike   2014-08-31  32.75   6.86    2014-12-20 10:49:37 14
BooF    Lake Bonaparte  Rock Bass   2014-09-27  7   0.30    2014-12-20 10:50:50 57
BooF    Lake Ozonia Smallmouth Bass 2014-08-15  13  1.19    2014-12-20 10:51:14 1
BooF    Stark Falls Reservoir   Walleye 2014-08-15  16  0.97    2014-12-20 10:51:37 49
BooF    Lake Bonaparte  Yellow Perch    2014-09-27  8.5 0.40    2014-12-20 10:52:01 56

This is the table I am working with

username  location      species     date        length  weight  timestamp id
BooF    Lake Ozonia Smallmouth Bass 2014-08-15  13.000  1.190   2014-12-20 10:51:14 1
BooF    Black Lake  Largemouth Bass 2014-07-03  16.750  2.620   2014-12-20 10:49:00 2
BooF    Muskellunge Lake    Largemouth Bass 2014-08-31  12.000  1.000   2014-08-31 22:04:42 7
BooF    Muskellunge Lake    Largemouth Bass 2014-08-31  16.000  2.000   2014-08-31 22:04:42 8
BooF    Muskellunge Lake    Largemouth Bass 2014-08-31  14.000  1.000   2014-08-31 22:04:42 9
BooF    Muskellunge Lake    Largemouth Bass 2014-08-31  16.000  2.000   2014-08-31 22:04:42 10
BooF    Muskellunge Lake    Largemouth Bass 2014-08-31  14.000  2.000   2014-08-31 22:04:42 11
BooF    Muskellunge Lake    Largemouth Bass 2014-08-31  16.000  2.000   2014-08-31 22:05:53 12
BooF    Muskellunge Lake    Black Crappie   2014-08-31  9.125   0.370   2014-12-20 10:48:06 13
BooF    Muskellunge Lake    Northern Pike   2014-08-31  32.750  6.860   2014-12-20 10:49:37 14
BooF    Narrow Lake Northern Pike   2014-03-15  20.000  2.000   2014-09-01 11:08:21 15
BooF    Narrow Lake Largemouth Bass 2014-03-15  14.000  1.000   2014-09-01 11:08:21 16
BooF    Butterfield Lake    Largemouth Bass 2014-05-26  19.000  3.000   2014-09-01 11:08:21 17
BooF    Butterfield Lake    Largemouth Bass 2014-05-26  17.000  2.000   2014-09-01 11:08:21 18
BooF    Red Lake    Northern Pike   2014-06-21  22.000  2.000   2014-09-01 11:08:21 19
BooF    Black Lake  Largemouth Bass 2014-07-03  15.000  2.000   2014-09-01 11:12:08 20
BooF    Black Lake  Largemouth Bass 2014-07-03  15.000  2.000   2014-09-01 11:12:08 21
BooF    Black Lake  Largemouth Bass 2014-07-02  17.000  2.000   2014-09-01 11:12:08 22
BooF    Black Lake  Largemouth Bass 2014-07-01  15.000  2.000   2014-09-01 11:12:08 23
BooF    Black Lake  Largemouth Bass 2014-07-01  15.000  2.000   2014-09-01 11:12:08 24
BooF    Black Lake  Largemouth Bass 2014-06-30  19.250  4.100   2014-12-20 10:53:17 25
BooF    Black Lake  Northern Pike   2014-06-29  26.750  3.940   2014-12-20 10:52:38 26
BooF    Stark Falls Reservoir   Walleye 2014-08-15  16.000  0.970   2014-12-20 10:51:37 49
BooF    Lake Bonaparte  Yellow Perch    2014-09-27  8.500   0.400   2014-12-20 10:52:01 56
BooF    Lake Bonaparte  Rock Bass   2014-09-27  7.000   0.300   2014-12-20 10:50:50 57

As you can see after the query it posts a largemouth bass for black lake weighing 2.62 pounds but on my data sheet it shows the largest bass at 4.1 so this is where I am stuck.


回答1:


In general ORDER BY in a sub-query makes no sense. (It only does when combined with FETCH FIRST/LIMIT/TOP etc.)

The solution is to use a correlated sub-query to find the heaviest fish for the "main query"'s current row's username, location, species combination. If it's a tie, both rows will be returned.

SELECT *
FROM entries e1
WHERE username = :user
  AND CAST(weight AS DECIMAL(9,3)) = (select max(CAST(weight AS DECIMAL(9,3)))
                                      from entries e2
                                      where e1.username = e2.username
                                        and e1.location = e2.location
                                        and e1.species = e2.species)

Note that char for weight is still a bad choice, beacause of that you have to cast both sides when comparing values. Go back to decimal in your table!



来源:https://stackoverflow.com/questions/28026906/mysql-largest-number-by-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!