Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1280260
  • 博文数量: 510
  • 博客积分: 20296
  • 博客等级: 上将
  • 技术积分: 4680
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-30 03:58
文章存档

2011年(13)

2010年(92)

2009年(242)

2008年(163)

我的朋友

分类: 数据库开发技术

2010-09-16 20:40:47


  1. --前一日
  2. select *
  3. from 表名
  4. where datediff(d,cast(日期 as datetime),getdate())=1

  5. --上周
  6. select *
  7. from 表名 where
  8. datediff(ww,cast(日期 as datetime),getdate())=1

  9. --上周
  10. select *
  11. from 表名
  12. where DATEPART(ww, 日期) = DATEPART(ww, GETDATE())-1 and
  13.       DATEPART(yy, 日期) = DATEPART(yy, GETDATE())

  14. --上月
  15. select *
  16. from 表名
  17. where datediff(month,cast(日期 as datetime),getdate())=1

  18. --上年
  19. select *
  20. from 表名
  21. where datediff(year,cast(日期 as datetime),getdate())=1
  22.  
  23. --按日统计
  24. select count(*),day([date])
  25. from 表名
  26. where year([date]) = '2006'
  27. group by day([date])
  28.  
  29. --按周
  30. select count(*),datename(week,[date])
  31. from 表名
  32. where year([date]) = '2006'
  33. group by datename(week,[date])
  34.  
  35. --按月
  36. select count(*),month([date])
  37. from 表名
  38. where year([date]) = '2006'
  39. group by month([date])
  40.  
  41. --按季
  42. select count(*),datename(quarter,[date])
  43. from 表名
  44. where year([date]) = '2006'
  45. group by datename(quarter,[date])
  46.  
  47. --按年
  48. select count(*),year([date])
  49. from 表名
  50. where year([date]) >= '2000' group by year([date])

参考:
     

阅读(1584) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~