2014-08-27 49 views
1

采取与相同值的分组中的每一行我有这个数据库:检查行更大的价值,然后通过ID

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

但这个查询返回的东西完全错了...怎么可能我做?

+0

结果是什么? – NoDataFound 2014-08-27 07:54:21

+0

只是原来的一个,每步值设置为最大... – 2014-08-27 07:54:39

回答

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

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

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 
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) 
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 
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 
-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 
+0

它的只是复制粘贴wewesthemenace查询 – Navneet 2014-08-27 08:54:08

+0

对不起..我没有粘贴我的代码... – Adi 2014-08-27 09:00:09