2016-08-17 70 views
0

類似的問題已經問多次,但沒有一個答案在這種情況下幫助,所以問的問題再次SQL查詢返回重複行與參加

我有這個結構如下表(是不是唯一的密鑰這裏):

--------------------------------------------------------------- 
|a_number | a_status | b_status | ref_id | timepublished  | 
|(text) | (text) | (text) |(text) | (datetime)   | 
--------------------------------------------------------------- 
|1  | U  | U  | re1 | 2016-08-12 13:24:25 | 
|1  | P  | U  | re1 | 2016-08-12 13:24:35 | 
|1  | P  | P  | re1 | 2016-08-12 13:24:45 | 
|2  | U  | U  | re2 | 2016-08-12 12:24:30 | 
|2  | U  | F  | re1 | 2016-08-12 12:24:45 | 
|4  | U  | U  | re3 | 2016-08-12 13:24:30 | 
|4  | U  | U  | re4 | 2016-08-13 15:24:30 | 
--------------------------------------------------------------- 

現在我想解決的問題是獲得每個a_number的最新狀態。所以,正確的輸出應該是:

--------------------------------------------------------------- 
|1  | P  | P  | re1 | 2016-08-12 13:24:45 | 
|2  | U  | F  | re1 | 2016-08-12 12:24:45 | 
|4  | U  | U  | re3 | 2016-08-12 13:24:30 | 
|4  | U  | U  | re4 | 2016-08-13 15:24:30 | 
--------------------------------------------------------------- 

和查詢我的是:

SELECT af.a_number 
    , af.a_status 
    , af.b_status 
    , af.ref_id 
    , af.timepublished 
FROM af_biz af 
JOIN 
(SELECT MAX(timepublished) timepublished, ref_id 
    FROM af_biz GROUP BY ref_id) tmp 
ON tmp.timepublished = af.timepublished AND tmp.ref_id = af.ref_id 
ORDER BY af.a_number; 

但結果我得到遏制這樣的不正確的輸出(注意,時間是最大timepublished但狀態是不同的):

--------------------------------------------------------------- 
|1  | P  | U  | re1 | 2016-08-12 13:24:45 | 
|1  | P  | P  | re1 | 2016-08-12 13:24:45 | 
|2  | U  | U  | re2 | 2016-08-12 12:24:45 | 
|2  | U  | F  | re1 | 2016-08-12 12:24:45 | 
|4  | U  | U  | re3 | 2016-08-12 13:24:30 | 
|4  | U  | U  | re4 | 2016-08-13 15:24:30 | 
--------------------------------------------------------------- 

任何人有任何想法可能是我的查詢錯?

+1

你需要做的'GROUP BY af.a_number' – Sovon

+1

@Sovon,這是無效的GROUP BY - 不允許在較新版本的MySQL ... – jarlh

+0

@Sovon,即使它是有效的,也不應該這樣做......在MySQL中起作用的MySQL擴展提供了擴展,但不能在任何其他RDBMS中工作。 – Rahul

回答

1

請這給一試:

SELECT 
    af.a_number 
, af.a_status 
, af.b_status 
, af.ref_id 
, af.timepublished 
FROM af_biz af 
INNER JOIN 
(
    SELECT 
    a_number, 
    MAX(timepublished) max_timepublished 
    FROM af_biz 
    GROUP BY a_number 
) AS t 
ON af.a_number = t.a_number AND af.timepublished = t.max_timepublished 
ORDER BY af.a_number 

首先你需要按a_number(由於您希望得到每個a_number

以後需要加入匹配timepublisheda_number(不ref_id

0

SELECT af.a_number, af.a_status, af.b_status, af.ref_id, af.timepublished 
 
FROM af_biz af 
 
JOIN 
 
(
 
SELECT MAX(timepublished) timepublished, 
 
afb.a_number+ afb.a_status+ afb.b_status+ afb.ref_id as key 
 
    FROM af_biz afb 
 
GROUP BY afb.a_number+ afb.a_status+ afb.b_status+ afb.ref_id 
 
) tmp 
 
ON tmp.timepublished = af.timepublished 
 
AND tmp.key = (af.a_number+ af.a_status+ af.b_status+ af.ref_id) 
 
ORDER BY af.a_number;

1

老式的,但我已經使用子查詢

這就是:

select 
    testtable.a_number 
    ,testtable.a_status 
    ,testtable.b_status 
    ,testtable.timepublished 
from testtable 
    inner join 
( select 
     a_number 
     ,max(timepublished) as date 
    from 
     testtable 
    group by 
     a_number) as anumberWithMaxDate 
on 
    testtable.a_number = anumberWithMaxDate.a_number and 
    testtable.timepublished = anumberWithMaxDate.date 
0

您所查詢的是表達的東西從你想要什麼不同。顯然你對加入有一些誤解。解釋您當前的查詢:

您生成表單(refid,maxtimepublished)的中間表,並且對於每個refid,您只有一個條目。這是您在連接中的正確表格。連接將匹配每個左行(原始表)與滿足連接條件的每個右行。現在

,最大時間refid=12016-08-12 12:24:45,所以用refid=1timepublished='2016-08-12 12:24:45'左表(原件)的每一行會的結果而告終。這是第三和第五排。 重複refid=2,第四行也會輸出。隨着refid=3最後一行將被輸出。

這些是4行,與您聲稱獲得的輸出不匹配。但這可能只是一個錯誤。

我認爲你必須對你的數據做一些假設。例如:

,每a_number將會有(獨立的ref_id

在這種情況下,任何給定的時間最多隻有一個入口,你可以在你的查詢與a_number替換每ref_id和完成用它。 (這也是1000111年代和作者Abhijit喬杜裏的答案)

+0

我不能假設最多隻有一個條目(使得這個假設成爲一個使問題變得更容易的獨特關鍵)。我編輯了我的問題以反映這一點 – Vivek

0
select 
    A_NUMBER,A_STATUS,B_STATUS,REF_ID,TIMEPUBLISHED 
from 
    TABL 
where 
    TIMEPUBLISHED in (select 
          b.TIMEPUBLISHED 
         from 
          (select 
           A_NUMBER,max(TIMEPUBLISHED) as TIMEPUBLISHED 
          from TABL 
           group by A_NUMBER) b) 
;