博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

sqlserver 按日分组

Posted on 2024-10-28 22:21  火冰·瓶  阅读(4)  评论(0编辑  收藏  举报
--根据日期字段AddTime,得到每日的数据量
SELECT
  CAST(AddTime AS DATE) AS riqi, COUNT(*) AS TotalOrders
FROM
  MoJingKaKouRecord
GROUP BY
  CAST(AddTime AS DATE)
  order by riqi desc

  

 

--统计出每日的车牌数量(同一个车牌,同一天只算一次)
select riqi,count(*) as num from 
(
SELECT
  CAST(AddTime AS DATE) AS riqi, COUNT(*) AS TotalOrders
FROM
  MoJingKaKouRecord
  GROUP BY
  CAST(AddTime AS DATE),PlateNo
  ) as t1
  group by riqi
  order by riqi desc

  

 

--指定时间范围内的车牌总数(每个车牌每日只统计一次)
select sum(num) from
(
		select riqi,count(*) as num from 
		(
				SELECT
				  CAST(AddTime AS DATE) AS riqi, COUNT(*) AS num
				FROM
				  MoJingKaKouRecord
				GROUP BY
				  CAST(AddTime AS DATE),PlateNo
		  ) as t1
		  group by riqi
  ) as t2
  where riqi >='2024-9-1'  and riqi <'2024-10-1'