SQL-запрос для группировки активности пользователя на основе временного диапазона
У меня есть таблица с двумя столбцами, id и ifiedDate. Я хочу запросить эту таблицу, чтобы перечислить активность каждого идентификатора между временным диапазоном. Мой стол выглядит так:
+-----+------------+
| ID | Date |
+-----+------------+
| 1 | 2017.01.19 |
| 1 | 2017.01.18 |
| 1 | 2017.01.10 |
| 1 | 2017.01.09 |
| 2 | 2017.01.19 |
| 2 | 2017.01.18 |
| 2 | 2017.01.10 |
| 2 | 2017.01.09 |
+-----+------------+
Desired output:
+-----+-----------------+------------+-------+
| ID | this week | last week | total |
+-----+-----------------+------------+-------+
| 1 | 2 | 2 | 4 |
| 2 | 2 | 2 | 4 |
+-----+-----------------+------------+-------+
3 ответа
Это как запрос на Oracle SQL
SELECT id,
COUNT(decode(to_char(t.modifydate,'iyyy-iw'),to_char(sysdate,'iyyy-iw'),1)) this_week,
COUNT(decode(to_char(t.modifydate,'iyyy-iw'),to_char(sysdate-7,'iyyy-iw'),1)) prev_week,
COUNT(id) total
FROM test_tbl1 t
GROUP BY id
Это должно работать в MySQL
SELECT `id`,
COUNT(CASE WHEN WEEKOFYEAR(`date`) = WEEKOFYEAR(CURDATE()) THEN 1 END) this_week,
COUNT(CASE WHEN WEEKOFYEAR(`date`) = WEEKOFYEAR(CURDATE())-1 THEN 1 END) prev_week,
COUNT(id) total
FROM `table`
GROUP BY `id`;
Вам нужна условная агрегация, которую можно сделать с помощью filter
пункт в Postgres (с 9.4)
select id,
count(*) filter (where this_week),
count(*) filter (where last_week),
count(*) as total
from (
select id,
date_trunc('week', "date")::date = date_trunc('week', current_date)::date as this_week,
date_trunc('week', "date")::date = date_trunc('week', current_date)::date - 7 as last_week
from data
) t
group by id
order by id;
В качестве альтернативы вы можете использовать to_char()
сократить дату до комбинации неделя / год:
select id,
to_char("date", 'iyyy-iw') = to_char(current_date, 'iyyy-iw') as this_week,
to_char("date", 'iyyy-iw') = to_char(current_date - 7, 'iyyy-iw') as last_week
from data