|
@@ -120,3 +120,93 @@ update smth set a = a * 2;
|
|
|
select species_name from species
|
|
|
order by length(species_name) desc, species_name limit 5;
|
|
|
|
|
|
+-- -----------------------------------------------------------------
|
|
|
+-- 11.28 Работа со перечислениями
|
|
|
+
|
|
|
+create table color (
|
|
|
+ name varchar(22) primary key check (name in (
|
|
|
+ 'адовый красный',
|
|
|
+ 'ядерный зеленый',
|
|
|
+ 'подозрительная мурена'
|
|
|
+ ))
|
|
|
+);
|
|
|
+
|
|
|
+create table color (
|
|
|
+ name text check (length(name) between 4 and 20)
|
|
|
+);
|
|
|
+
|
|
|
+-- -----------------------------------------------------------------
|
|
|
+-- 11.29 Работа со временем
|
|
|
+
|
|
|
+select to_char(created_at, 'DD.MM.YYYY HH24:MI:SS') "дата инцидента",
|
|
|
+ message "сообщение" from log;
|
|
|
+
|
|
|
+insert into log (created_at, message) values
|
|
|
+ ('2024-07-31 20:19:51'::timestamp, 'Произошло страшное: сломался приём платежа #1128'),
|
|
|
+ ('2024-07-31 17:19:52'::timestamp, 'Трындец с отправкой почты, похоже, почтовый сервер недоступен!'),
|
|
|
+ ('2024-07-31 17:20:53'::timestamp, 'Ужас, всё работает!');
|
|
|
+
|
|
|
+
|
|
|
+drop table if exists log;
|
|
|
+create temp table log(
|
|
|
+ log_id bigint generated always as identity,
|
|
|
+ created_at timestamp not null,
|
|
|
+ message text not null
|
|
|
+);
|
|
|
+
|
|
|
+insert into log (created_at, message) values
|
|
|
+ ('2024-06-02 20:20:20'::timestamp, 'Это событие за прошлый год'),
|
|
|
+ ('2025-06-02 20:20:20'::timestamp, 'Это событие за текущий год');
|
|
|
+
|
|
|
+table log;
|
|
|
+
|
|
|
+select * from log where extract(month from(created_at)) = extract(month from now())
|
|
|
+and extract(year from(created_at)) = extract(year from now());
|
|
|
+
|
|
|
+-- Вернуть данные за этот месяц
|
|
|
+select * from log
|
|
|
+where date_trunc('month', created_at) = date_trunc('month', current_timestamp)
|
|
|
+order by log_id;
|
|
|
+
|
|
|
+-- Вернуть данные за последнюю неделю
|
|
|
+select * from log where created_at >= current_date - interval '7 days'
|
|
|
+
|
|
|
+-- Удалить данные за последние 30 дней
|
|
|
+delete from log
|
|
|
+where created_at < current_date - intercal '30 days';
|
|
|
+
|
|
|
+-- Прибавить месяц
|
|
|
+select '2024-01-31'::date + '1 month'::interval;
|
|
|
+
|
|
|
+-- Прибавить год
|
|
|
+select '2024-02-29'::date + '1 year'::interval;
|
|
|
+
|
|
|
+-- Эти запросы вернут одинаковое значение
|
|
|
+select '2024-02-28'::date - '1 year'::interval;
|
|
|
+select '2024-02-29'::date - '1 year'::interval;
|
|
|
+
|
|
|
+-- Запрос вернет ответ типа integer
|
|
|
+select '2024-02-01'::date - '2023-01-01'::date d;
|
|
|
+
|
|
|
+-- Запрос вернет ответи типа interval
|
|
|
+select '2024-02-01'::timestamp - '2023-01-01'::timestamp d;
|
|
|
+
|
|
|
+-- Форматирование даты
|
|
|
+select
|
|
|
+ to_char(start, 'DD.MM.YYYY') start,
|
|
|
+ to_char(finish, 'DD.MM.YYYY') finish
|
|
|
+ extract(day from (finish - start) delta_in_days
|
|
|
+from records order by start, finish;
|
|
|
+
|
|
|
+-- С помощью функции age можно вывести дельту
|
|
|
+select
|
|
|
+ to_char(start, 'DD.MM.YYYY') start,
|
|
|
+ to_char(finish, 'DD.MM.YYYY') finish
|
|
|
+ age(finish, start) delta
|
|
|
+from records order by start, finish;
|
|
|
+
|
|
|
+-- Достает email тех у кого в апреле день рождения
|
|
|
+select email from employees
|
|
|
+where extract('month' from birthday)=4
|
|
|
+order by email;
|
|
|
+
|