苏飞论坛

标题: 跟我一起学SQL 第二章2.1计算任意两个时间之间的星期几的次数(横向显示) [打印本页]

作者: session    时间: 2013-5-15 11:22
标题: 跟我一起学SQL 第二章2.1计算任意两个时间之间的星期几的次数(横向显示)
本帖最后由 session 于 2013-5-15 11:24 编辑


  1. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_weekdaycount]') and xtype in (N'FN', N'IF', N'TF'))
  2. drop function [dbo].[f_weekdaycount]
  3. GO
  4. /*--计算任意两个时间之间的星期几的次数(横向显示)
  5. 本方法直接判断 @@datefirst 做对应处理
  6. 不受 sp_language 及 set datefirst 的影响  

  7. /*--调用示例

  8. select * from f_weekdaycount('2004-9-01','2004-9-02')
  9. --*/
  10. create function f_weekdaycount(
  11. @dt_begin datetime,
  12. @dt_end datetime
  13. )returns table
  14. as
  15. return(
  16. select 跨周数
  17.   ,周一=case a
  18.    when -1 then case when 1 between b and c then 1 else 0 end
  19.    when  0 then case when b<=1 then 1 else 0 end
  20.      +case when c>=1 then 1 else 0 end
  21.    else a+case when b<=1 then 1 else 0 end
  22.     +case when c>=1 then 1 else 0 end
  23.    end
  24.   ,周二=case a
  25.    when -1 then case when 2 between b and c then 1 else 0 end
  26.    when  0 then case when b<=2 then 1 else 0 end
  27.      +case when c>=2 then 1 else 0 end
  28.    else a+case when b<=2 then 1 else 0 end
  29.     +case when c>=2 then 1 else 0 end
  30.    end
  31.   ,周三=case a
  32.    when -1 then case when 3 between b and c then 1 else 0 end
  33.    when  0 then case when b<=3 then 1 else 0 end
  34.      +case when c>=3 then 1 else 0 end
  35.    else a+case when b<=3 then 1 else 0 end
  36.     +case when c>=3 then 1 else 0 end
  37.    end
  38.   ,周四=case a
  39.    when -1 then case when 4 between b and c then 1 else 0 end
  40.    when  0 then case when b<=4 then 1 else 0 end
  41.      +case when c>=4 then 1 else 0 end
  42.    else a+case when b<=4 then 1 else 0 end
  43.     +case when c>=4 then 1 else 0 end
  44.    end
  45.   ,周五=case a
  46.    when -1 then case when 5 between b and c then 1 else 0 end
  47.    when  0 then case when b<=5 then 1 else 0 end
  48.      +case when c>=5 then 1 else 0 end
  49.    else a+case when b<=5 then 1 else 0 end
  50.     +case when c>=5 then 1 else 0 end
  51.    end
  52.   ,周六=case a
  53.    when -1 then case when 6 between b and c then 1 else 0 end
  54.    when  0 then case when b<=6 then 1 else 0 end
  55.      +case when c>=6 then 1 else 0 end
  56.    else a+case when b<=6 then 1 else 0 end
  57.     +case when c>=6 then 1 else 0 end
  58.    end
  59.   ,周日=case a
  60.    when -1 then case when 0 between b and c then 1 else 0 end
  61.    when  0 then case when b<=0 then 1 else 0 end
  62.      +case when c>=0 then 1 else 0 end
  63.    else a+case when b<=0 then 1 else 0 end
  64.     +case when c>=0 then 1 else 0 end
  65.    end
  66. from(
  67.   select 跨周数=case when @dt_begin<@dt_end
  68.     then (datediff(day,@dt_begin,@dt_end)+7)/7
  69.     else (datediff(day,@dt_end,@dt_begin)+7)/7 end
  70.    ,a=case when @dt_begin<@dt_end
  71.     then datediff(week,@dt_begin,@dt_end)-1
  72.     else datediff(week,@dt_end,@dt_begin)-1 end
  73.    ,b=case when @dt_begin<@dt_end
  74.     then (@@datefirst+datepart(weekday,@dt_begin)-1)%7
  75.     else (@@datefirst+datepart(weekday,@dt_end)-1)%7 end
  76.    ,c=case when @dt_begin<@dt_end
  77.     then (@@datefirst+datepart(weekday,@dt_end)-1)%7
  78.     else (@@datefirst+datepart(weekday,@dt_begin)-1)%7 end)a
  79. )
  80. go
复制代码

作者: 站长苏飞    时间: 2013-5-15 11:24
支持一下




欢迎光临 苏飞论坛 (http://www.sufeinet.com/) Powered by Discuz! X3.4