2016-12-14 34 views
1

我有一張類似圖片的表格。我想找到最小分配日期和分鐘升級日期組,直到不同的操作員到來。例子中的第一個兩個操作符是相同的,所以我想總結這兩個,因爲不同的操作符在它們後面。如何將相同的有價值的行相加,直到出現不同的值

TROUBLETICKETINDEX   OPERATOR   Assign     Escalate 
    1262084    qbcca0110 12-12-2016 21:11:49 12-12-2016 21:11:49 
    1262084    qbcca0110 12-12-2016 21:16:23 12-12-2016 21:16:23 
    1262084    tss   12-14-2016 1:45:58 12-14-2016 1:45:58 
    1262084    qbcca0110 12-14-2016 10:34:31 12-14-2016 10:34:31 

請問我可以在mysql中嗎?

我這個

select th.TROUBLETICKETINDEX, 
     th.OPERATOR, 
     min(th.HISTORYDATE) Assign, 
     max(th.HISTORYDATE) Escalate 
    from troubleticketshistory th inner join 
     users_usergroups uug on uug.userId = th.TICKETOWNER inner join 
     usergroup ug on ug.userGroupId = uug.userGroupId 
    where th.TROUBLETICKETINDEX=1262084 
group by th.OPERATOR; 

嘗試,但這個給了我

enter image description here

但我找

enter image description here

這是我troubleticketshistory表

HISTORYDATE   OPERATOR TROUBLETICKETINDEX TICKETOWNER USERGROUPINDEX 
12-12-2016 21:11:49 qbcca0110 1262084     24229 12016 
12-12-2016 21:16:23 qbcca0110 1262084     25111 22956 
12-14-2016 1:45:58 tss   1262084     24229 12016 
12-14-2016 10:34:31 qbcca0110 1262084     24229 12016 
+0

能否請您包括以文字形式而不是圖片的樣本數據? –

+0

我的意思是在你的原始文章。 –

+0

請刪除不相關的標籤。 – JSapkota

回答

1

像這樣的東西應該工作:

SELECT TROUBLETICKETINDEX, 
     OPERATOR, 
     MIN(HISTORYDATE) AS Assign, 
     MAX(HISTORYDATE) AS Escalate 
FROM (
    SELECT th.TROUBLETICKETINDEX, 
      th.OPERATOR, 
      th.HISTORYDATE 
      @rn := @rn + 1 AS rn, 
      @grp := IF(@op = OPERATOR, @grp + 1, 
        IF(@op := OPERATOR, 1, 1)) AS grp 
    FROM troubleticketshistory th 
    CROSS JOIN (SELECT @rn := 0, @grp := 0, @op := '') AS vars 
    WHERE th.TROUBLETICKETINDEX = 1262084 
    ORDER BY th.Assign) AS t 
GROUP BY TROUBLETICKETINDEX, OPERATOR, rn - grp 
ORDER BY Assign 

Demo here

+0

任何人都可以解釋子查詢嗎? –

相關問題