There is a request:

SELECT to_char(date, 'YYYY-MM-dd HH24:MI:ss') as day, title, email, price, pages FROM customer 

I get the answer:
1. 2016-10-26 09:51:57 Title 1 my@email.com 5 10
2. 2016-10-26 09:51:40 Title 1 my@email.com 5 10
3. 2016-10-26 09:51:20 Title 1 my@email.com 5 10
4. 2016-10-26 09:51:10 Title 1 my@email.com 5 10
5. 2016-10-26 19:51:10 Title 2 my2@email.com 5 10
6. 2016-10-26 19:00:10 Title 2 my2@email.com 5 10

All the same, except for the seconds in the date. Tell me, please, how can I get only one value, the first hit in the database. In my case, this is point 4 and 6.

    4 answers 4

     SELECT to_char(MIN(date), 'YYYY-MM-dd HH24:MI:ss') as day, title, email, price, pages FROM customer GROUP BY title, email, price, pages 

      If you need to get only one value, the first hit in the database - use limit 1 and order by :

       SELECT to_char(date, 'YYYY-MM-dd HH24:MI:ss') as day, title, email, price, pages FROM customer LIMIT 1 ORDER BY day; 

      If you need to get not the last record, but several, then:

       SELECT DISTINCT email, day, title, price, pages FROM (SELECT to_char(date, 'YYYY-MM-dd HH24:MI:ss') as day, title, email, price, pages FROM customer ORDER BY day); 
      • one
        You misunderstood the task, apparently. The man asks to get the last entry, but several - Trymount
      • one
        Doesn’t your second example show everything again?) By seconds, they are still different in the day column - Trymount

      Try using SELECT TOP 1

        I think it will look like this:

         SELECT to_char(date, 'YYYY-MM-dd HH24:MI') as day, title, email, price, pages FROM customer ORDER BY day GROUP BY day, title, email, price, pages 

        But the decision is also not entirely correct, since there may be such an example of initial data:

         1. 2016-10-26 09:51:57 Title 1 my@email.com 5 10 2. 2016-10-26 09:52:01 Title 1 my@email.com 5 10 

        And my example will not work. PS not sure if you can get the date without seconds.