Search code examples
sqlpostgresqlgreatest-n-per-group

Sql DISTINCT remove column from result


I have simple PostgreSQL query, that do query events by recent date, but I wanted to remove this Distinct column(time) from result

SELECT DISTINCT time, sensor_id, event_type, value from events ORDER BY sensor_id

+---------------------+---+---+-----+
| 2014-02-13 12:42:00 | 2 | 2 |   5 |
| 2014-02-13 13:19:57 | 2 | 4 | -42 |
| 2014-02-13 13:32:36 | 2 | 3 |  54 |
| 2014-02-13 14:48:30 | 2 | 2 |   2 |
| 2014-02-13 12:54:39 | 3 | 2 |   7 |
+---------------------+---+---+-----+

need result like this

+---+---+-----+
| 2 | 2 |   5 |
| 2 | 4 | -42 |
| 2 | 2 |   2 |
| 2 | 3 |  54 |
| 3 | 2 |   7 |
+---+---+-----+ 

Solution

  • I think you mean something like:

    SELECT sensor_id, event_type, value 
    from   (
              SELECT DISTINCT time, sensor_id, event_type, value from events
           ) A 
    ORDER BY sensor_id