Руслан Абсалямов, правильно, потому что в вопросе не указано что это для MySQL. Попробуй так:
SELECT *
FROM (SELECT
ticket.name as ticket_name
, tm.text as message_text
, tm.created_at as message_dt
, @row_number:=CASE WHEN @user_id = ticket.user_id THEN @row_number + 1 ELSE 1 END as rn
, @user_id:=ticket.user_id as CustomerNumber
FROM
ticket_themes ticket
LEFT JOIN ticket_message tm on ticket.id = tm.theme_id
WHERE ticket.user_id = 69625
ORDER BY tm.created_at DESC, tm.id DESC ) t
WHERE t.rn = 1
Pivot тебе тут не поможет. можно попробовать скриптом указанным ниже, но мне кажется некорректный пример приведен, т.к. не понятно в чем смысл
SELECT TOP (1) WITH TIES *
FROM (
SELECT a column_a, 'q' AS column_name, q as column_value
FROM #t AS t
UNION all
SELECT a, 'w' AS column_name, w
FROM #t AS t
UNION all
SELECT a, 'e' AS column_name, e
FROM #t AS t
) AS g
ORDER BY g.column_value
SELECT *
FROM (SELECT
ticket.name as ticket_name
, tm.text as message_text
, tm.created_at as message_dt
, @row_number:=CASE WHEN @user_id = ticket.user_id THEN @row_number + 1 ELSE 1 END as rn
, @user_id:=ticket.user_id as CustomerNumber
FROM
ticket_themes ticket
LEFT JOIN ticket_message tm on ticket.id = tm.theme_id
WHERE ticket.user_id = 69625
ORDER BY tm.created_at DESC, tm.id DESC ) t
WHERE t.rn = 1