SQL根据计算列的最小值获取整个行 [英] SQL Get entire row based on minimum value of calculated column

查看:687
本文介绍了SQL根据计算列的最小值获取整个行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张表格,其中有多个具有不同状态的床位ID值,诸如此类.

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

我希望查询根据状态值为每个bed_id获取单个行.
我已经尝试过:

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

但这给了我

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

(StatusOrder值正确,但其余行与StatusOrder值最小的行不对应)

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

我想要的是:

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

我已阅读 MySQL MIN/MAX全部行 并尝试了这个:

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

但这会产生错误. (还尝试用完整的CASE语句替换StatusOrder)

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

注意:我已经简化了包含更多列的实际表.但是基本上,我需要访问与每个bed_id具有最低StatusOrder(由我的case语句确定)的行相对应的整行.

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.

使用MySQL 5.5

Using MySQL 5.5

推荐答案

以下查询有效,但最好考虑使用代码用status_code替换列状态,并使用单独的表status(status_code,description) {denormalise}

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}

并索引(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

这篇关于SQL根据计算列的最小值获取整个行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆