检查更大的行值,然后按ID对每一行进行相同的值分组

时间:2014-08-27 07:47:59

标签: sql sql-server

我是这个数据库:

╔═════════╦══════╦══════════╦═══════════╦════╦═════════╗
║ ID_Elab ║ Step ║ ID_Progr ║ ID_Causal ║ GI ║ Minutes ║
╠═════════╬══════╬══════════╬═══════════╬════╬═════════╣
║       8 ║    1 ║        8 ║        19 ║  0 ║     480 ║
║       8 ║    2 ║     1391 ║        19 ║  0 ║     480 ║
║       8 ║    3 ║     1781 ║        19 ║  0 ║     480 ║
║      10 ║    1 ║       10 ║        50 ║  0 ║     480 ║
║      10 ║    1 ║       43 ║        14 ║  0 ║     210 ║
║      10 ║    2 ║       99 ║        50 ║  0 ║     480 ║
║      10 ║    2 ║      100 ║        14 ║  0 ║     210 ║
║      10 ║    3 ║      124 ║        50 ║  0 ║     480 ║
║      10 ║    3 ║      125 ║        72 ║  0 ║     120 ║
║      10 ║    3 ║      126 ║        73 ║  0 ║      90 ║
║      11 ║    1 ║        8 ║        19 ║  0 ║     480 ║
║      11 ║    2 ║     1391 ║        19 ║  0 ║     480 ║
╚═════════╩══════╩══════════╩═══════════╩════╩═════════╝

我需要针对每组ID检查哪个Step值越大,然后选择具有该Step值的特定组的每一行。

上表将成为:

╔═════════╦══════╦══════════╦═══════════╦════╦═════════╗
║ ID_Elab ║ Step ║ ID_Progr ║ ID_Causal ║ GI ║ Minutes ║
╠═════════╬══════╬══════════╬═══════════╬════╬═════════╣
║       8 ║    3 ║     1781 ║        19 ║  0 ║     480 ║
║      10 ║    3 ║      124 ║        50 ║  0 ║     480 ║
║      10 ║    3 ║      125 ║        72 ║  0 ║     120 ║
║      10 ║    3 ║      126 ║        73 ║  0 ║      90 ║
║      11 ║    2 ║     1391 ║        19 ║  0 ║     480 ║
╚═════════╩══════╩══════════╩═══════════╩════╩═════════╝

我已尝试关注this question,这是我的查询结果:

SELECT *
FROM testVela  a
JOIN (
            SELECT ID_Elab, MAX(Step) AS Step, ID_Progr, ID_Causal, GI, Minutes
            FROM testVela 
            GROUP BY ID_Elab, ID_Progr, ID_Causal, Minutes
        ) b
ON a.ID_Elab = b.ID_Elab AND a.Step = b.Step

但是这个查询返回的内容完全错误......我该怎么办?

6 个答案:

答案 0 :(得分:4)

create table #test_table(
    ID_Elab int,
    Step int,
    ID_Progr int,
    ID_Casusal int,
    GI int,
    Minutes int
)
insert into #test_table
select 8, 1, 8, 19, 0, 480 union all 
select 8, 2, 1391, 19, 0, 480 union all 
select 8, 3, 1781, 19, 0, 480 union all 
select 10, 1, 10, 50, 0, 480 union all
select 10, 1, 43, 14, 0, 210 union all 
select 10, 2, 99, 50, 0, 480 union all 
select 10, 2, 100, 14, 0, 210 union all 
select 10, 3, 124, 50, 0, 480 union all 
select 10, 3, 125, 72, 0, 120 union all 
select 10, 3, 126, 73, 0, 90 union all 
select 11, 1, 8, 19, 0, 480 union all 
select 11, 2, 1391, 19, 0, 480 

;with cte as(
    select
        *,
        rn = rank() over(partition by ID_Elab order by step desc)
    from #test_table
)
select
    ID_Elab,
    Step,
    ID_Progr,
    ID_Casusal,
    GI,
    Minutes
from cte
where
    rn = 1

drop table #test_table

答案 1 :(得分:2)

mysql> select * FROM tst where step = (select max(step) from tst as B where tst.ID_Elab = B.ID_Elab);
+---------+------+----------+-----------+----+---------+
| ID_Elab | Step | ID_Progr | ID_Causal | GI | Minutes |
+---------+------+----------+-----------+----+---------+
|       8 |    3 |     1781 |        19 |  0 |     480 |
|      10 |    3 |      124 |        50 |  0 |     480 |
|      10 |    3 |      125 |        72 |  0 |     120 |
|      10 |    3 |      126 |        73 |  0 |      90 |
|      11 |    2 |     1391 |        19 |  0 |     480 |
+---------+------+----------+-----------+----+---------+
5 rows in set (0.01 sec)

答案 2 :(得分:1)

你已经在那里,只需要删除一些字段。

SELECT *
FROM testVela  a
JOIN (
            SELECT ID_Elab, MAX(Step) AS Step
            FROM testVela 
            GROUP BY ID_Elab
        ) b
ON a.ID_Elab = b.ID_Elab AND a.Step = b.Step

答案 3 :(得分:1)

简单的解决方案

select * 
from testVela tbl 
where tbl.Step = (select Max(Step) from testVela   subtbl where subtbl.ID_Elab = tbl.ID_Elab) order by  tbl.ID_Elab

答案 4 :(得分:0)

在SQL中你会尝试这个

select 
    ID_Elab,
    Step,
    ID_Progr,
    ID_Casusal,
    GI,
    Minutes from 
(SELECT rank() over (partition by ID_Elab order by step desc) as rn,*
FROM testVela) as tbl 
where rn=1

答案 5 :(得分:-2)

请尝试以下代码:

create table temp(
        ID_Elab int,
        Step int,
        ID_Progr int,
        ID_Casusal int,
        GI int,
        Minutes int
    )
    insert into temp
    select 8, 1, 8, 19, 0, 480 union all 
    select 8, 2, 1391, 19, 0, 480 union all 
    select 8, 3, 1781, 19, 0, 480 union all 
    select 10, 1, 10, 50, 0, 480 union all
    select 10, 1, 43, 14, 0, 210 union all 
    select 10, 2, 99, 50, 0, 480 union all 
    select 10, 2, 100, 14, 0, 210 union all 
    select 10, 3, 124, 50, 0, 480 union all 
    select 10, 3, 125, 72, 0, 120 union all 
    select 10, 3, 126, 73, 0, 90 union all 
    select 11, 1, 8, 19, 0, 480 union all 
    select 11, 2, 1391, 19, 0, 480 


    select 
        ID_Elab,
        Step,
        ID_Progr,
        ID_Casusal,
        GI,
        Minutes from 
    (SELECT row_number  over (partition by ID_Elab order by step desc) as rn,*
    FROM temp) as tbl 
    where rn=1
    drop table temp