简体   繁体   English

SQL根据计算列的最小值获取整行

[英]SQL Get entire row based on minimum value of calculated column

I have a table with multiple values of bed ID with different statuses, something like this. 我有一张桌子,床身ID有多个不同的状态,就像这样。

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. 我想要一个查询,根据状态值为每个bed_id获取一行。
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) (StatusOrder值是正确的,但行的其余部分与具有最小StatusOrder值的行不对应)

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: 我已经阅读了MySQL MIN / MAX所有行并尝试了这个:

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) (还尝试用完整的CASE语句替换StatusOrder)

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. 但基本上我需要访问每个bed_id对应于具有最低StatusOrder(由我的case语句确定)的行的整行。

Using MySQL 5.5 使用MySQL 5.5

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} 下面的查询工作正常,但最好考虑使用status_code替换列状态和代码,并具有单独的表status(status_code,description) {denormalise}

And indexing ( term_id,statUs_code ). 和索引( 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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