Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this:</p> <pre><code>SELECT t.ticket_id, ticket_desc, ht.status FROM ticket AS t INNER JOIN history_ticket AS ht ON t.ticket_id = ht.ticket_id INNER JOIN ( SELECT ticket_id, MAX(history_id) maxid FROM history_ticket GROUP BY ticket_id ) AS ht2 ON ht.history_id = ht2.maxid; </code></pre> <h2><a href="http://sqlfiddle.com/#!2/0d086/3" rel="nofollow">SQL Fiddle Demo</a></h2> <p>This will give you:</p> <pre><code>| TICKET_ID | TICKET_DESC | STATUS | ------------------------------------- | 1 | software | process | | 2 | hardware | solve | | 3 | Problem | solve | </code></pre> <hr> <h2>UPDATE 1</h2> <p>To get the count of messages for each ticket, you can simply include <code>COUNT(history_id) AS sum_message</code> in the subquery like this:</p> <pre><code>SELECT t.ticket_id, ticket_desc, ht.status, ht2.sum_message FROM ticket AS t INNER JOIN history_ticket ht ON t.ticket_id = ht.ticket_id INNER JOIN ( SELECT ticket_id, MAX(history_id) maxid, COUNT(history_id) AS sum_message FROM history_ticket GROUP BY ticket_id ) AS ht2 ON ht.history_id = ht2.maxid; </code></pre> <h2><a href="http://sqlfiddle.com/#!2/0d086/5" rel="nofollow">Updated SQL Fiddle Demo</a></h2> <p>This will give you:</p> <pre><code>| TICKET_ID | TICKET_DESC | STATUS | SUM_MESSAGE | --------------------------------------------------- | 1 | software | process | 1 | | 2 | hardware | solve | 1 | | 3 | Problem | solve | 2 | </code></pre> <hr> <h2>Update 2</h2> <p>If you want to select names for the ids <code>divisi_id</code>, for simple values, you can use the <code>CASE</code> expression for this:</p> <pre><code>SELECT t.ticket_id, ticket_desc, CASE WHEN t.divisi_id = 101 THEN 'Divisi A' WHEN t.divisi_id = 102 THEN 'Divisi B' END AS 'Divisi', ht.status, ht2.sum_message FROM ticket AS t INNER JOIN history_ticket ht ON t.ticket_id = ht.hticket_id INNER JOIN ( SELECT hticket_id, MAX(history_id) maxid, COUNT(history_id) AS sum_message FROM history_ticket GROUP BY hticket_id ) AS ht2 ON ht.history_id = ht2.maxid; </code></pre> <h2><a href="http://sqlfiddle.com/#!2/eba04/6" rel="nofollow">Updated SQL Fiddle Demo</a></h2> <p>This will give you:</p> <pre><code>| TICKET_ID | TICKET_DESC | DIVISI | STATUS | SUM_MESSAGE | -------------------------------------------------------------- | 1 | software | Divisi A | process | 1 | | 2 | hardware | Divisi B | solve | 1 | | 3 | Problem | Divisi A | solve | 2 | </code></pre> <p>For multiple values, you can put them in a temp table, or you can select them in a subquery and join the table to get the name like this:</p> <pre><code>SELECT t.ticket_id, ticket_desc, d.Divisi, ht.status, ht2.sum_message FROM ticket AS t INNER JOIN history_ticket ht ON t.ticket_id = ht.hticket_id INNER JOIN ( SELECT hticket_id, MAX(history_id) maxid, COUNT(history_id) AS sum_message FROM history_ticket GROUP BY hticket_id ) AS ht2 ON ht.history_id = ht2.maxid INNER JOIN ( SELECT 101 AS divisi_id, 'Divisi A' AS Divisi UNION ALL SELECT 102 , 'Divisi B' ... -- here you put other values or you can join a temp table instead ) AS D ON t.divisi_id = D.divisi_id; </code></pre> <h2><a href="http://sqlfiddle.com/#!2/eba04/7" rel="nofollow">Updated SQL Fiddle Demo</a></h2>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload