Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Get entire row based on minimum value of calculated column

Tags:

sql

mysql

I have a table with multiple values of bed ID with different statuses, something like this.

PID     |  term_id   |  student_id   |  bed_id   | status    |  Comment
--------+------------+---------------+-----------+-----------+----------------
1       |  29        |  1234         |  751      | Canceled  |  Not this one
2       |  29        |  1234         |  751      | Active    |  This one
3       |  29        |  531          |  752      | Active    |  This one too
4       |  29        |  823          |  752      | Canceled  |  Not this one either
5       |  29        |  525          |  753      | Canceled  |  But this one too

I want a query to get a single row for each bed_id based on the value of status.
I've tried:

SELECT *,MIN(CASE sample.status
                         WHEN 'Arrived' THEN 1
                         WHEN 'Active' THEN 2
                         WHEN 'Pending Approval' THEN 3
                         WHEN 'Pending Confirmation' THEN 4
                         WHEN 'Pending Manual' THEN 5
                         WHEN 'Denied' THEN 6
                         WHEN 'Canceled' THEN 7
                END) AS StatusOrder
FROM sample
WHERE (sample.term_id = 29)
GROUP BY bed_id

But it gives me:

PID  |  term_id |  student_id |  bed_id | status    |  Comment           | StatusOrder
------------------------------------------------------------------------------------
1    |  29      |  1234       |  751    | Canceled  |  Not this one      | 2
3    |  29      |  531        |  752    | Active    |  This one too      | 2
5    |  29      |  525        |  753    | Canceled  |  But this one too  | 7

(The StatusOrder value is right, but the rest of the row does not correspond to the row with minimum StatusOrder value)

What I want is:

PID  |  term_id |  student_id |  bed_id | status    |  Comment           | StatusOrder
------------------------------------------------------------------------------------
2    |  29      |  1234       |  751    | Active    |  This one          | 2
3    |  29      |  531        |  752    | Active    |  This one too      | 2
5    |  29      |  525        |  753    | Canceled  |  But this one too  | 7

I've read MySQL MIN/MAX all row and also tried this:

SELECT *,MIN(CASE sample.status
                         WHEN 'Arrived' THEN 1
                         WHEN 'Active' THEN 2
                         WHEN 'Pending Approval' THEN 3
                         WHEN 'Pending Confirmation' THEN 4
                         WHEN 'Pending Manual' THEN 5
                         WHEN 'Denied' THEN 6
                         WHEN 'Canceled' THEN 7
                END) AS StatusOrder
FROM  sample
WHERE ( 
        (sample.term_id = 29) AND (
        StatusOrder = CASE sample.status
                         WHEN 'Arrived' THEN 1
                         WHEN 'Active' THEN 2
                         WHEN 'Pending Approval' THEN 3
                         WHEN 'Pending Confirmation' THEN 4
                         WHEN 'Pending Manual' THEN 5
                         WHEN 'Denied' THEN 6
                         WHEN 'Canceled' THEN 7
                END)
       )
GROUP BY bed_id

But this produces an error. (Also tried replacing StatusOrder with the full CASE statement)

NOTE: I've simplified the actual table which has many more columns. But basically I need access to the whole row that corresponds to the row with the lowest StatusOrder (determined by my case statement) for each bed_id.

Using MySQL 5.5

like image 660
scotru Avatar asked Dec 29 '13 05:12

scotru


Video Answer


1 Answers

The Below Query works, but it's better to consider replace the column status with status_code with the code, and have a separate table status(status_code,description) {denormalise}

And indexing (term_id,statUs_code).

by this, we can just self join. Instead creating a view like this.

SELECT * FROM
(SELECT *,(CASE sample.status
                         WHEN 'Arrived' THEN 1
                         WHEN 'Active' THEN 2
                         WHEN 'Pending Approval' THEN 3
                         WHEN 'Pending Confirmation' THEN 4
                         WHEN 'Pending Manual' THEN 5
                         WHEN 'Denied' THEN 6
                         WHEN 'Canceled' THEN 7
                END) AS status_code FROM SAMPLE
  WHERE sample.term_id = 29
) my_view1,
(SELECT BED_ID,MIN(CASE sample.status
                         WHEN 'Arrived' THEN 1
                         WHEN 'Active' THEN 2
                         WHEN 'Pending Approval' THEN 3
                         WHEN 'Pending Confirmation' THEN 4
                         WHEN 'Pending Manual' THEN 5
                         WHEN 'Denied' THEN 6
                         WHEN 'Canceled' THEN 7
                END) AS status_code FROM SAMPLE
 WHERE sample.term_id = 29
 GROUP BY BED_ID
) my_view2
WHERE my_view1.bed_id = my_view2.bed_id
 AND  my_view1.status_code = my_view2.status_code
like image 125
Maheswaran Ravisankar Avatar answered Nov 04 '22 01:11

Maheswaran Ravisankar