2015-12-12 95 views
2

對MAX一旦SQL INNER JOIN基於時間戳

兩次修訂後的修正:剩餘9個表,除了報告的標題往往被稱之爲「什麼」。

我有大約10個表結構如下:

reports (165k rows) 
+-----------+-----------+ 
| identifier| category | 
+-----------+-----------+ 
| 1   | fixed  | 
| 2   | wontfix | 
| 3   | fixed  | 
| 4   | invalid | 
| 5   | later  | 
| 6   | wontfix | 
| 7   | duplicate | 
| 8   | later  | 
| 9   | wontfix | 
+-----------+-----------+ 
status (300k rows, all identifiers from reports come up at least once) 
+-----------+-----------+----------+ 
| identifier| time  | what  | 
+-----------+-----------+----------+ 
| 1   | 12  | RESOLVED | 
| 1   | 9   | NEW  | 
| 2   | 7   | ASSIGNED | 
| 3   | 10  | RESOLVED | 
| 5   | 4   | REOPEN | 
| 7   | 9   | ASSIGNED | 
| 4   | 9   | ASSIGNED | 
| 7   | 11  | RESOLVED | 
| 8   | 3   | NEW  | 
| 4   | 3   | NEW  | 
| 7   | 6   | NEW  | 
+-----------+-----------+----------+ 

priority (300k rows, all identifiers from reports come up at least once) 
+-----------+-----------+----------+ 
| identifier| time  | what  | 
+-----------+-----------+----------+ 
| 3   | 12  | LOW  | 
| 1   | 9   | LOW  | 
| 9   | 2   | HIGH  | 
| 8   | 7   | HIGH  | 
| 3   | 10  | HIGH  | 
| 5   | 4   | MEDIUM | 
| 4   | 9   | MEDIUM | 
| 4   | 3   | LOW  | 
| 7   | 9   | LOW  | 
| 7   | 11  | HIGH  | 
| 8   | 3   | LOW  | 
| 6   | 12  | MEDIUM | 
| 7   | 6   | LOW  | 
| 6   | 9   | HIGH  | 
| 2   | 6   | HIGH  | 
| 2   | 1   | LOW  | 
+-----------+-----------+----------+ 

我需要的是:

reportsfinal (165k rows) 
+-----------+-----------+--------------+------------+ 
| identifier| category | what11  | what22 | 
+-----------+-----------+--------------+------------+ 
| 1   | fixed  | RESOLVED  | LOW  | 
| 2   | wontfix | ASSIGNED  | HIGH  | 
| 3   | fixed  | RESOLVED  | LOW  | 
| 4   | invalid | ASSIGNED  | MEDIUM  | 
| 5   | later  | REOPEN  | MEDIUM  | 
| 6   | wontfix |    | MEDIUM  | 
| 7   | duplicate | RESOLVED  | HIGH  | 
| 8   | later  | NEW   | HIGH  | 
| 9   | wontifx |    | HIGH  | 
+-----------+-----------+--------------+------------+ 

也就是說,reports(後查詢= reportsfinal)作爲基礎表我必須從9個其他表中添加一列或兩列。 identifier是關鍵,但在某些表中,identifier出現多次。在這些情況下,我只想使用最高時間的條目。 我嘗試了幾個查詢,但都沒有工作。如果可能的話,我想用這種方法運行一個查詢來從其他9個表中獲取不同的列。

我試圖基於下面的答案是什麼:

select T.identifier, 
 
     T.category, 
 
     t.what AS what11, 
 
     t.what AS what22 from (
 
    select R.identifier, 
 
    R.category, 
 
    COALESCE(S.what,'NA')what, 
 
    COALESCE(P.what,'NA')what, 
 
    ROW_NUMBER()OVER(partition by R.identifier,R.category ORDER by (select null))RN 
 
    from reports R 
 
    LEFT JOIN bugstatus S 
 
    ON S.identifier = R.identifier 
 
    LEFT JOIN priority P 
 
    ON P.identifier = s.identifier 
 

 
    GROUP BY R.identifier,R.category,S.what,P.what)T 
 
    Where T.RN = 1 
 
    ORDER BY T.identifier;

這給了錯誤:

Error: near "(": syntax error.

回答

1

對於每個關聯表只使用基於子查詢謂詞確定具體的時間戳...

單字母標記R,S和P爲表報告的,狀態和優先級分別

Select r.Identifier, r.category, 
    coalesce(s.what, 'NA') status, 
    coalesce(p.what, 'NA') priority 
From reports r 
    left join status s 
    on s.identifier = r.identifier 
     and s.time = 
      (Select max(time) from status 
      where identifier = r.identifier) 
    left join priority p 
    on p.identifier = r.identifier 
     and p.time = 
      (Select max(time) from priority 
      where identifier = r.identifier); 

定義別名問題:爲什麼您將Statuspriority的列重命名爲What?您可能還需要somethingdatainformation。至少原來的名字(statusprio)溝通了一些東西。單詞What是沒有意義的。

注意。我將what11what12的別名的編輯反轉(解開),因爲這些名稱是毫無意義的。

+0

我收到 '錯誤:不明確的列名:時間' – JohnDavison

+1

固定的錯誤:不明確的列名稱:時間' –

+0

我看到你關於標題「什麼」的觀點。我沒有這樣定義它們,它們在csv文件中是這樣的。但是你是對的,我可以在創建SQLite數據庫時給他們更多有意義的名字。查詢現在已經運行了4個小時,但仍然沒有完成。我認爲必須有一個查詢來獲得數據更快?@Charles Bretana – JohnDavison

1

基本上你需要一個在選擇列表中相關的子查詢。

從臀部,是這樣的:使用ROW_NUMBER

Select a.Identifier 
,a.Category 
,(select process 
    from status where status.identifier = a.Identifer order by time desc limit 1) Process 
,(select prio 
    from priority where priorty.identifier = a.Identifer order by time desc limit 1) prio 
From Reports a 
+0

@peter:「a」是什麼意思?是a =報告?此外,TOP 1在SQLite中不起作用,我讀到LIMIT 1的作品。 喜歡我沒有得到一個錯誤,但查詢仍在運行(我有30萬行): 「選擇reports.identifier ,reports.current_resolution,reports.current_status ,(選擇bugstatus其中bugstatus什麼 。通過時間戳降序 \t LIMIT 1)標識符= reports.identifier順序什麼 ,(選擇什麼 從優先其中priority.identifier = reports.identifier由時間戳降序 \t LIMIT 1)爲了什麼 從報告;' – JohnDavison

+0

是的,我說它是從臀部,我用T-SQL,我認爲你使用Sqllite ..所以限制1似乎是等效的。是的,我打算將報告別名爲A,但沒有。你是說它的工作,或者它不? – PilotBob

+0

是的,我正在使用SQLite。那麼,在查詢結束時是否需要「From reports a」?我的提案的其餘部分是否包含新標頭?非常感謝@ PilotBob – JohnDavison

0

作品根據您的假設數據

select T.identifier, 
     T.category, 
     what AS what11, 
     what AS what22 from (
    select R.identifier, 
    r.category, 
    COALESCE(S.what,'NA')what, 
    COALESCE(P.what,'NA')what, 
    ROW_NUMBER()OVER(partition by R.identifier,r.category ORDER by (select null))RN 
    from reports R left join status S 
    ON S.identifier = R.identifier 
    LEFT JOIN Priority P 
    ON P.identifier = s.identifier 

    GROUP BY R.identifier,r.category,S.what,P.what)T 
    Where T.RN = 1 
    ORDER BY T.identifier 
+0

T代表什麼或定義在哪裏? 「ORDER by(select null))RN」RN代表什麼或代表哪裏定義?非常感謝! – JohnDavison

+0

我已經用作派生表,T是別名,只不過是別名。 RN是Row_number,並且選擇Null以進行排序目的@JohnDavison – mohan111

+0

1)您可以使用小寫字母和大寫字母作爲別名和表名。這很重要嗎? 2)表中沒有明確的「NA」,但是空白。代碼是否需要更改? 3)我改變了標題爲「什麼」的後續表格狀態和優先級。如果所有標題都被稱爲「什麼」,那麼這是一個問題嗎? 我得到錯誤:「Error:near」(「:syntax error」。@ mohan111 – JohnDavison