我每天将数据插入数据库中。我需要基于时间戳检索单列数据。我需要根据时间戳显示今天用法和昨天用法。
我的实际表格
name usage timestamp
prod 200 2019-01-08 09:22:53.364366
Test 100 2019-01-08 09:22:53.364366
qality 50 2019-01-08 09:22:53.364366
prod 270 2019-01-09 08:22:53.364366
Test 300 2019-01-09 08:22:53.364366
qality 90 2019-01-09 08:22:53.364366
Expecting output:
name usage(yesterday) usage(Today) timestamp
prod 200 270 2019-01-09 08:22:53.364366
Test 100 300 2019-01-09 08:22:53.364366
qality 50 90 2019-01-09 08:22:53.364366
答案 0 :(得分:0)
尝试按名称进行汇总,然后检查时间戳记:
SELECT
name,
MAX(CASE WHEN timestamp::date = NOW()::date - INTERVAL '1 DAY'
THEN usage END) AS u_yesterday,
MAX(CASE WHEN timestamp::date = NOW()::date
THEN usage END) AS u_today,
MAX(timestamp) AS timestamp
FROM yourTable
GROUP BY
name;