一个时间段的算法?

sjrw96004 2013-04-28 09:15:27
2-8点计早餐,12-13点计中餐,17-次日2点计晚餐
任意两个时间间,早中晚餐数
例如:2013年1月1日10时10分至1月3日5时50分

1日 中1晚1
2日 早1中1晚1
3日 早1

合计早2中2晚2

用查询语句写得出来不?非得用存储过程??
...全文
221 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xmniemaosheng 2013-05-05
  • 打赏
  • 举报
回复
餐饮管理中经常要用到的营业日的概念,需要把日期先转换成营业日,然后再处理。思路搞清楚了,语法很容易的
sjrw96004 2013-05-03
  • 打赏
  • 举报
回复
谢谢,参考了下,的确要用存储过程
q465897859 2013-05-03
  • 打赏
  • 举报
回复
存储过程也只需 头尾处理下 中间的就都包含三次
sjrw96004 2013-05-03
  • 打赏
  • 举报
回复
上面的例子没能解决跨天、月、年的问题 表的结构 时间一,时间二,早,中,晚 是指时间一到时间二(任意两个时间,可能跨几天或者一年) 中间经过的早中晚次数
SQL77 2013-05-03
  • 打赏
  • 举报
回复
引用 4 楼 sjrw96004 的回复:
谢谢,参考了下,的确要用存储过程
你加一条WHERE DT BETWEEN @BEGINGTIME AND @ENDTIME 语句限定时间范围就成了.
SQL77 2013-05-03
  • 打赏
  • 举报
回复
引用 4 楼 sjrw96004 的回复:
谢谢,参考了下,的确要用存储过程
你看别人用存储过程了?..只是语句
叶子 2013-04-29
  • 打赏
  • 举报
回复
我添加了一条数据'2013-01-03 01:10:00' 这应该也是01-02的晚餐.

declare @T table (col datetime)
insert into @T
select '2013-01-01 12:45:00' union all
select '2013-01-01 20:00:00' union all
select '2013-01-02 05:00:00' union all
select '2013-01-02 12:30:00' union all
select '2013-01-02 18:00:00' union all
select '2013-01-03 01:10:00' union all
select '2013-01-03 04:00:00'

select 
convert(varchar(10),dateadd(hh,-2,col),120) as 日期,
sum(
	case when col 
	between convert(varchar(11),col,120)+'02:00:00'
	and convert(varchar(11),col,120)+'08:00:00' then 1 else 0 end
) as 早餐,
sum(
	case when col 
	between convert(varchar(11),col,120)+'12:00:00'
	and convert(varchar(11),col,120)+'13:00:00' then 1 else 0 end
) as 中餐,
sum(
	case when 
    col >=convert(varchar(11),col,120)+'17:00:00' 
    or col <=convert(varchar(11),col,120)+'02:00:00'
    then 1 else 0 end
) as 晚餐
from @T 
group by convert(varchar(10),dateadd(hh,-2,col),120)

/*
日期         早餐          中餐          晚餐
---------- ----------- ----------- -----------
2013-01-01 0           1           1
2013-01-02 1           1           2
2013-01-03 1           0           0
*/
叶子 2013-04-29
  • 打赏
  • 举报
回复

declare @T table (col datetime)
insert into @T
select '2013-01-01 12:45:00' union all
select '2013-01-01 20:00:00' union all
select '2013-01-02 05:00:00' union all
select '2013-01-02 12:30:00' union all
select '2013-01-02 18:00:00' union all
select '2013-01-03 04:00:00'

select convert(varchar(10),col,120) as 日期,
sum(
	case when col 
	between convert(varchar(11),col,120)+'02:00:00'
	and convert(varchar(11),col,120)+'08:00:00' then 1 else 0 end
) as 早餐,
sum(
	case when col 
	between convert(varchar(11),col,120)+'12:00:00'
	and convert(varchar(11),col,120)+'13:00:00' then 1 else 0 end
) as 中餐,
sum(
	case when col 
	between convert(varchar(11),col,120)+'17:00:00'
	and convert(varchar(11),col,120)+'23:59:00' then 1 else 0 end
) as 晚餐
from @T 
group by convert(varchar(10),col,120)

/*
日期         早餐          中餐          晚餐
---------- ----------- ----------- -----------
2013-01-01 0           1           1
2013-01-02 1           1           1
2013-01-03 1           0           0
*/
叶子 2013-04-29
  • 打赏
  • 举报
回复
存储过程也是SQL语句组成的. 如果时间段不跨天的话,group by 就可以了.

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧