【问题标题】:Check greater value of row and then take every row with same value grouping by ID检查行的较大值,然后按 ID 对具有相同值的每一行进行分组
【发布时间】:2014-08-27 07:47:58
【问题描述】:

我有这个数据库:

╔═════════╦══════╦══════════╦═══════════╦════╦═════════╗
║ 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

但是这个查询返回的东西完全错误……我该怎么办?

【问题讨论】:

  • 结果如何?
  • 只是原始的,每个 Step 值都设置为最大值...

标签: sql sql-server


【解决方案1】:
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

【讨论】:

    【解决方案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)
    

    【讨论】:

      【解决方案3】:

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

      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
      

      【讨论】:

        【解决方案4】:

        简单的解决方案

        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
        

        【讨论】:

          【解决方案5】:

          在 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
          

          【讨论】:

            【解决方案6】:

            请尝试以下代码:

            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
            

            【讨论】:

            • 它只是复制粘贴 wewestthemenace 查询
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2023-04-02
            • 2023-01-16
            • 2013-02-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-04-06
            相关资源
            最近更新 更多