M
M
Messi2016-11-03 13:18:07
PostgreSQL
Messi, 2016-11-03 13:18:07

How to get only the first value in a table?

There is a request:

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

Получаю ответ:
1. 2016-10-26 09:51:57 Title 1 [email protected] 5 10
2. 2016-10-26 09:51:40 Title 1 [email protected] 5 10
3. 2016-10-26 09:51:20 Title 1 [email protected] 5 10
4. 2016-10-26 09:51:10 Title 1 [email protected] 5 10
5. 2016-10-26 19:51:10 Title 2 [email protected] 5 10
6. 2016-10-26 19:00:10 Title 2 [email protected] 5 10
Всё одинаково, кроме секунд в дате. Подскажите, пожалуйста, как мне получить только одно значение, первое попадание в базу. В моем случае это пункт 4 и 6

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Сергей Горностаев, 2016-11-03
@FitTech

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

R
Rsa97, 2016-11-03
@Rsa97

С учётом изменения вопроса:

SELECT to_char(c.date, 'YYYY-MM-dd HH24:MI:ss') as day,
       c.title, c.email, c.price, c.pages
  FROM (
    SELECT MIN(date) AS date, email FROM customer GROUP BY email
  ) AS t
  JOIN customer AS c ON c.date = t.date AND c.email = d.email

S
Sanan Yuzb, 2016-11-03
@Sanan07

MIN(date) вместе с group by

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question