You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Oracle中如何将日期截断至分钟以实现分组统计?

Hey there! In Oracle, there are a couple of straightforward ways to truncate a date down to the minute dimension—perfect for grouping your data effectively. Let me break down the most practical approaches for you:

Oracle's TRUNC() function supports the 'MI' format parameter, which lets you truncate a date straight to the nearest minute. This returns a proper DATE type value, which is ideal for grouping since it’s efficient and avoids string-formatting quirks.

Example query:

SELECT TRUNC(SYSDATE, 'MI') AS date_min FROM dual;

If your current time is 2024-05-20 14:35:42, this will return 2024-05-20 14:35:00—exactly the truncated minute you need.

2. Convert via string (if you prefer this workflow)

If you’re already comfortable using TO_CHAR(), you can format the date to exclude seconds, then convert it back to a DATE type. Just make sure to use hh24 instead of hh to avoid 12-hour format ambiguity:

SELECT TO_DATE(TO_CHAR(SYSDATE, 'mm-dd-yyyy hh24:mi'), 'mm-dd-yyyy hh24:mi') AS date_min FROM dual;

Note that this adds an extra conversion step, so it’s slightly less performant than the TRUNC() method—stick with option 1 unless you have a specific reason to use this.

3. Grouping with the truncated date

Once you have the truncated minute, using it for grouping is straightforward. Here’s an example of grouping records by minute and counting them:

SELECT 
  TRUNC(your_date_column, 'MI') AS minute_group,
  COUNT(*) AS total_records
FROM your_table
GROUP BY TRUNC(your_date_column, 'MI')
ORDER BY minute_group;

内容的提问来源于stack exchange,提问作者Mariana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 06:57:32