Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select only rows with max date

In clickhouse table I have multiple rows for one _id. What I want is to get only one row for each _id where column _status_set_at has its max value. That's what I am currently at:

SELECT _id, max(_status_set_at), count(_id)
FROM pikta.candidates_states
GROUP BY _id

output

Because I can't use max() function at WHERE clause, how to workaround this problem? count(_id) shows how many rows are there for each _id, if the query is correct, it should show 1. Also, as far as I concerned, there is no ON clause in Clickhouse database.

UPD: there is ON clause in Clickhouse

like image 486
Graygood Avatar asked Aug 16 '18 13:08

Graygood


People also ask

How do I SELECT the maximum date row in SQL?

How do I get max date records in SQL? Select row with max date per user using MAX() function Another way to get the latest record per user is using inner queries and Max() function. Max() function, when applied on a column, gives the maximum value of that column.

Where date is Max in SQL?

The maximum valid date for a SqlDateTime structure is December 31, 9999.

Can we use max with * in SQL?

Try using this SQL SELECT statement: SELECT * FROM employees WHERE department_id=30 AND salary = (SELECT MAX(salary) FROM employees WHERE department_id=30); This will return the employee information for only the employee in department 30 that has the highest salary.


3 Answers

Your query returns what you need - only one row for each _id where column _status_set_at has its max value. You do not need to change anything in your original query.

count(_id) shows how many rows for each _id in the original table, but not in a query result. Query result has only one row for each _id because you group by _id.

This query shows that in your query result there is only one row for each _id

SELECT _id, max_status_set_at, count(_id) FROM (
SELECT _id, max(_status_set_at) max_status_set_at
FROM pikta.candidates_states
GROUP BY _id) t
GROUP BY _id

If you need apply a condition on max(_status_set_at) you can use HAVING

like image 130
Mikhail Avatar answered Sep 18 '22 10:09

Mikhail


If you want to you your max clause on where statement maybe this will work

SELECT * from (SELECT _id, max(_status_set_at) as [MaxDate], count(_id) as [RepeatCount]
    FROM pikta.candidates_states
    GROUP BY _id) t WHERE t.MaxDate = '@parameter'
like image 41
Harun KARATAŞ Avatar answered Sep 21 '22 10:09

Harun KARATAŞ


Solution - 1 :

SELECT Z._id,
       Z._status_set_at
  FROM 
(
SELECT _id, 
       _status_set_at, 
       max(_status_set_at) OVER ( PARTITION BY _id ORDER BY _status_set_at DESC ) AS rnk
FROM pikta.candidates_states
) Z
WHERE Z.rnk = 1;

Solution - 2 :

SELECT A._id,
       A._status_set_at  
  FROM pikta.candidates_states A
CROSS JOIN
       ( 
         SELECT _id, 
                MAX(_status_set_at) AS max_status_set_dt         
           FROM pikta.candidates_states
         GROUP BY _id
        ) B
WHERE A._id = B._id
  AND A._status_set_at = B.max_status_set_dt; 
like image 26
Teja Avatar answered Sep 21 '22 10:09

Teja