2014-08-27 47 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