简体   繁体   中英

MySQL select MAX value from subquery only returns one results from outer query. Why?

(Using MariaDB 5.5
I am aware this is old but it's a 3rd party server I can't update it )

I have a table of data with a date/year column. I need to list all the values based on a reference id but only with the most recent year.

Based on my recent question about subqueries I thought this was going to be a simple case of using a subquery with a MAX() selection, but that only ever returns one results. Why? And how do I get the full set of results.

The table:

 create table ee_stats
(
    stat_id int(8) unsigned auto_increment
        primary key,
    ee_id smallint(6) unsigned null,
    ref_id int null comment 'external table reference.',
    date_year year null,
    value_counter int(8) unsigned null,
    value_percentage double(6,3) unsigned null 
)

The data

stat_id | ee_id | ref_id | date_year | value_counter | value_percentage
------------------------------------------------------------------------
    301 |   32  |   14   |      2020 |      0        |         0
    302 |   32  |   1    |      2020 |      0        |         0
    303 |   32  |   21   |      2020 |      0        |         0
    304 |   32  |   22   |      2020 |      0        |         0
    305 |  464  |   17   |      2020 |      40       |         3
    306 |  464  |   18   |      2020 |      0        |         0
    307 |  464  |   20   |      2020 |      0        |         0
    308 |  464  |   2    |      2020 |      87       |        6.6
    309 |  464  |   19   |      2020 |      0        |         0
    310 |  464  |   7    |      2020 |      15       |        1.1
    311 |  464  |   10   |      2020 |      29       |        2.2
    312 |  464  |   11   |      2019 |      29       |        2.2
    313 |  464  |   16   |      2019 |      13       |        1.0
    314 |  464  |   7    |      2019 |     116       |        8.8
    315 |  464  |   19   |      2019 |      71       |        5.3
    316 |  464  |   4    |      2019 |      67       |         5

Intended result is to select all of the values with ee_id = 464 and the most recent year on record ( 2020 ).

My Query

SELECT es.stat_id, es.ee_id, es.ref_id, es.date_year, es.value_count,
       es.value_percentage, eed.descr
FROM ee_stats es
         LEFT JOIN ee_descriptor eed on es.ref_id = eed.id
WHERE es.ee_id = 464 AND es.date_year = (
    SELECT MAX(zz.date_year) FROM ee_stats zz WHERE zz.ee_id = es.ee_id
    )

But this only returns the first outer result:

 305 | 464 | 17 | 2020 | 40 | 3

My attempts

The subquery correctly returns the value 2020 when run independently (substituting the outer value). This question is very similar but that answer seems to be exactly what should be working here.

The works:

SELECT es.stat_id, es.ee_id, es.ref_id, es.date_year, es.value_count,
       es.value_percentage, eed.descr
FROM ee_stats es
         LEFT JOIN ee_descriptor eed on es.ref_id = eed.id
WHERE es.ee_id = 464 AND es.date_year = '2020'

And This works:

SELECT es.stat_id, es.ee_id, es.ref_id, es.date_year, es.value_count,
       es.value_percentage, eed.descr
FROM ee_stats es
         LEFT JOIN ee_descriptor eed on es.ref_id = eed.id
WHERE es.ee_id = 464 AND es.date_year = (
    SELECT MAX(zz.date_year) FROM ee_stats zz WHERE zz.ee_id = '464'
    )

but somehow the dynamic outer reference to the ee_id value only returns one result on the SQL.

Intended result:

    305 |  464  |   17   |      2020 |      40       |         3
    306 |  464  |   18   |      2020 |      0        |         0
    307 |  464  |   20   |      2020 |      0        |         0
    308 |  464  |   2    |      2020 |      87       |        6.6
    309 |  464  |   19   |      2020 |      0        |         0
    310 |  464  |   7    |      2020 |      15       |        1.1
    311 |  464  |   10   |      2020 |      29       |        2.2

I got it!

I found I had to CAST the MAX entity to being the correct format; so I had to cast it to a DATE format.

CAST(MAX(zz.date_year) AS DATE)

This works.

SELECT es.stat_id, es.ee_id, es.ref_id, es.date_year, es.value_count,
       es.value_percentage, eed.descr
FROM ee_stats es
         LEFT JOIN ee_descriptor eed on es.ref_id = eed.id
WHERE es.ee_id = 464 AND es.date_year = (
    SELECT CAST(MAX(zz.date_year) AS DATE) FROM ee_stats zz WHERE zz.ee_id = es.ee_id
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM