简体   繁体   中英

MySQL, ordering GROUP BY

I have a table that has some values in it, along with the time that value was taken against an associated ID from another table.

I am looking to retrieve the latest value for every item in that table, and then order by those latest values.

Here is an SQL fiddle, http://www.sqlfiddle.com/#!2/0be99

And here is text output.

'hist' table

| HIST_ID | HIST_ITEM_ID | HIST_VALUE |  HIST_TIME |
|---------|--------------|------------|------------|
|       1 |            1 |          1 | 1420291000 |
|       2 |            1 |          2 | 1420292000 |
|       3 |            1 |          3 | 1420293000 |
|       4 |            1 |          5 | 1420294000 |
|       5 |            1 |         10 | 1420295000 |
|       6 |            1 |         50 | 1420296000 |
|       7 |            1 |         60 | 1420297000 |
|       8 |            1 |         77 | 1420298000 |
|       9 |            1 |         90 | 1420299000 |
|      10 |            1 |        101 | 1420300000 |
|      11 |            2 |          1 | 1420291000 |
|      12 |            2 |          3 | 1420292000 |
|      13 |            2 |          7 | 1420293000 |
|      14 |            2 |          9 | 1420294000 |
|      15 |            2 |         15 | 1420295000 |
|      16 |            2 |         21 | 1420296000 |
|      17 |            2 |         33 | 1420297000 |
|      18 |            2 |         35 | 1420298000 |
|      19 |            2 |         55 | 1420299000 |
|      20 |            2 |         91 | 1420300000 |

'items' table

| ITEM_ID | ITEM_TITLE |
|---------|------------|
|       1 |       ABCD |
|       2 |     XYZ123 |

So, I can do something like...

select * from hist
inner join items on hist_item_id = item_id
group by hist_item_id
order by hist_value desc

However this returns me a grouping that I cannot order. How can I order this grouping? I had a look at other similar questions on here but was unable to apply their solutions successfully to my query to produce the desire result.

The desired result here would be to return.

HIST_ITEM_ID | ITEM_TITLE | HIST_VALUE |
|------------|------------|------------|
|          1 |       ABCD |        101 |
|          2 |     XYZ123 |         91 |

You can use a join to get the most recent history item. Then you can join back to the history table and the item table to get additional information:

select h.*, i.item_title
from (select hist_item_id,  max(hist_id) as max_hist_id
      from hist
      group by hist_item_id
     ) hh join
     hist h
     on h.hist_id = hh.max_hist_id join
     items i
     on i.item_id = hh.hist_item_id;

Here is a SQL Fiddle.

You should use MAX function and group by the item id. That would look like this:

SELECT i.item_id, i.item_title, MAX(h.hist_value)
FROM items AS i
INNER JOIN hist AS h
  ON i.item_id = h.hist_item_id
GROUP BY i.item_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