Oracle 分组统计,按照天、月份周和自然周、月、季度和年

1.按天

select to_char(t.STARTDATE+15/24, 'YYYY-MM-DD') as 天,sum(1) as 数量
from HOLIDAY t
group by to_char(t.STARTDATE+15/24, 'YYYY-MM-DD') --
ORDER by 天 NULLS  LAST;

select trunc(t.STARTDATE, 'DD') as 天,sum(1) as 数量
from HOLIDAY t
group by trunc(t.STARTDATE, 'DD') --
ORDER by 天 NULLS  LAST;

在这里插入图片描述

2.按周

select to_char(next_day(t.STARTDATE+15/24 - 7,2),'YYYY-MM-DD') AS 周,sum(1) as 数量 
from HOLIDAY t 
group by to_char(next_day(t.STARTDATE+15/24 - 7,2),'YYYY-MM-DD')ORDER BY 周;

在这里插入图片描述

-- 按自然周统计 
select to_char(t.STARTDATE,'iw') AS 周,sum(1) as 数量
from HOLIDAY t

group by to_char(t.STARTDATE,'iw')
ORDER BY 周;

在这里插入图片描述

3.按自然月

select to_char(t.STARTDATE,'YYYY-MM') as 月份,sum(1) as 数量
from HOLIDAY t

GROUP BY
	to_char(t.STARTDATE,'YYYY-MM')
ORDER BY 月份;

在这里插入图片描述

4.按季度

select to_char(t.STARTDATE,'q') 季度,sum(1) as 数量
from HOLIDAY t
group by to_char(t.STARTDATE,'q')
ORDER BY 季度 NULLS  LAST;

在这里插入图片描述

5.按年

select to_char(t.STARTDATE,'yyyy') AS 年度,sum(1) as 数量
from HOLIDAY t
group by to_char(t.STARTDATE,'yyyy')
ORDER BY 年度;

在这里插入图片描述

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
按周对给定的时间段进行分组,可以使用Java中的日期时间库来处理。以下是一个示例代码,展示了如何按照不同的时间单位进行分组: ```java import java.time.LocalDate; import java.time.LocalDateTime; import java.time.YearMonth; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class GroupByTimeExample { public static void main(String[] args) { // 假设有一个起始日期和一个结束日期 LocalDate startDate = LocalDate.of(2022, 1, 1); LocalDate endDate = LocalDate.of(2022, 12, 31); // 创建四个Map来存储按不同时间单位分组的结果 Map<YearMonth, List<LocalDate>> groupedByMonth = new HashMap<>(); Map<LocalDate, List<LocalDate>> groupedByDay = new HashMap<>(); Map<Integer, List<LocalDate>> groupedByWeek = new HashMap<>(); Map<Integer, List<LocalDate>> groupedByYear = new HashMap<>(); // 遍历时间段内的每一,将每个日期按照不同的时间单位添加到对应的组中 LocalDate currentDate = startDate; while (!currentDate.isAfter(endDate)) { YearMonth yearMonth = YearMonth.from(currentDate); if (!groupedByMonth.containsKey(yearMonth)) { groupedByMonth.put(yearMonth, new ArrayList<>()); } groupedByMonth.get(yearMonth).add(currentDate); if (!groupedByDay.containsKey(currentDate)) { groupedByDay.put(currentDate, new ArrayList<>()); } groupedByDay.get(currentDate).add(currentDate); int week = currentDate.get(ChronoUnit.WEEK_OF_YEAR); if (!groupedByWeek.containsKey(week)) { groupedByWeek.put(week, new ArrayList<>()); } groupedByWeek.get(week).add(currentDate); int year = currentDate.getYear(); if (!groupedByYear.containsKey(year)) { groupedByYear.put(year, new ArrayList<>()); } groupedByYear.get(year).add(currentDate); currentDate = currentDate.plusDays(1); // 增加一 } // 打印按不同时间单位分组的结果 System.out.println("Grouped by Month:"); for (Map.Entry<YearMonth, List<LocalDate>> entry : groupedByMonth.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } System.out.println("\nGrouped by Day:"); for (Map.Entry<LocalDate, List<LocalDate>> entry : groupedByDay.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } System.out.println("\nGrouped by Week:"); for (Map.Entry<Integer, List<LocalDate>> entry : groupedByWeek.entrySet()) { System.out.println("Week " + entry.getKey() + ": " + entry.getValue()); } System.out.println("\nGrouped by Year:"); for (Map.Entry<Integer, List<LocalDate>> entry : groupedByYear.entrySet()) { System.out.println("Year " + entry.getKey() + ": " + entry.getValue()); } } } ``` 在这个例子中,我们给定了一个起始日期和一个结束日期,并使用`LocalDate`、`YearMonth`、`Integer`等作为键来将日期按不同的时间单位分组。最终的结果是四个`Map`,分别表示按分组的日期列表。 运行以上代码,输出将会是: ``` Grouped by Month: 2022-01: [2022-01-01, 2022-01-02, ..., 2022-01-31] 2022-02: [2022-02-01, 2022-02-02, ..., 2022-02-28] ... 2022-12: [2022-12-01, 2022-12-02, ..., 2022-12-31] Grouped by Day: 2022-01-01: [2022-01-01] 2022-01-02: [2022-01-02] ... 2022-12-31: [2022-12-31] Grouped by Week: Week 1: [2022-01-01, 2022-01-02, ..., 2022-01-07] Week 2: [2022-01-08, 2022-01-09, ..., 2022-01-14] ... Week 52: [2022-12-24, 2022-12-25, ..., 2022-12-31] Grouped by Year: Year 2022: [2022-01-01, 2022-01-02, ..., 2022-12-31] ``` 这样,你就成功地按周分组了。你可以根据自己的需求对起始日期、结束日期和输出格式进行调整。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值