1

For example I have the following database entries:

timestamp | value1 | value 2
----------    
1452|5|7
1452|1|6
1452|2|7
1623|1|2
1623|5|6
1623|4|5
1623|4|7
1855|1|2

Now I want to have a sql query which returns me value1 only for the timestamp which is availble the most. Therefore it should return only the timestamp 1623 and it's values. I was first thinking of count, but that will return only the number of the availability and not the entries.

4

2 回答 2

2
select *
  from T
 inner join (select timestamp 
               from T 
              group by timestamp 
           order by count(*) desc 
           limit 1) t2
  on T.timestamp = t2.timestamp
于 2013-09-10T08:32:05.143 回答
0
WITH CTE AS (
  SELECT *, COUNT(timestamps) OVER (PARTITION BY value1, timestamps) AS cnt
  FROM mytable
  ), cte2 as (select *, row_number() over (partition by value1 order by cnt DESC, timestamps) as Rn FROM cte)

SELECT value1, timestamps , cnt FROM CTE2 WHERE Rn = 1;
于 2013-09-10T08:37:28.570 回答