在oracle中逐日获取上个月的数据sql
Get data of last Month day by day in oracle sql
我想每天获取上个月的数据,我可以获得最近 30 天的数据,但我只想要月份,因为它可能少于或多于 30 天,
这是获取最近 30 天的查询
SELECT Trunc(timestamp),
Count(*)
FROM table1
WHERE Trunc(timestamp) > Trunc(sysdate - 30)
GROUP BY Trunc(timestamp)
ORDER BY 1;
此外,我在 shell 脚本中使用它,如果我可以在脚本中创建一个变量并将其放入查询
获取从当月开始到今天的数据:
SELECT TRUNC(timestamp) AS day,
COUNT(*)
FROM table1
WHERE timestamp >= TRUNC(SYSDATE, 'MM')
AND timestamp < TRUNC(SYSDATE) + INTERVAL '1' DAY
GROUP BY TRUNC(timestamp)
ORDER BY day
获取上个月同一天到今天的数据:
SELECT TRUNC(timestamp) AS day,
COUNT(*)
FROM table1
WHERE timestamp >= ADD_MONTHS(TRUNC(SYSDATE), -1)
AND timestamp < TRUNC(SYSDATE) + INTERVAL '1' DAY
GROUP BY TRUNC(timestamp)
ORDER BY day
db<>fiddle here
我想每天获取上个月的数据,我可以获得最近 30 天的数据,但我只想要月份,因为它可能少于或多于 30 天,
这是获取最近 30 天的查询
SELECT Trunc(timestamp),
Count(*)
FROM table1
WHERE Trunc(timestamp) > Trunc(sysdate - 30)
GROUP BY Trunc(timestamp)
ORDER BY 1;
此外,我在 shell 脚本中使用它,如果我可以在脚本中创建一个变量并将其放入查询
获取从当月开始到今天的数据:
SELECT TRUNC(timestamp) AS day,
COUNT(*)
FROM table1
WHERE timestamp >= TRUNC(SYSDATE, 'MM')
AND timestamp < TRUNC(SYSDATE) + INTERVAL '1' DAY
GROUP BY TRUNC(timestamp)
ORDER BY day
获取上个月同一天到今天的数据:
SELECT TRUNC(timestamp) AS day,
COUNT(*)
FROM table1
WHERE timestamp >= ADD_MONTHS(TRUNC(SYSDATE), -1)
AND timestamp < TRUNC(SYSDATE) + INTERVAL '1' DAY
GROUP BY TRUNC(timestamp)
ORDER BY day
db<>fiddle here