您现在的位置: 中国男护士网 >> 考试频道 >> 计算机等级 >> 三级辅导 >> 正文    
  SQL技巧:创建用来按小时报告的查询 【注册男护士专用博客】          

SQL技巧:创建用来按小时报告的查询

www.nanhushi.com     佚名   不详 

要创建一个可以每个小时报告的查询,首先要创建一个表格。该表格一列记录日期,而没有时间信息;另一列记录钟点。下面的表格有一列记录了不同的处理类型。例如,我们可以按小时找出处理类型的总数。

create table test(starttime datetime not nulldefault current_timestamp,startdate datetime not nulldefault convert(datetime, convert(char(10),current_timestamp, 110)),starthour int not nulldefault datepart(hh,current_timestamp),trantype int not nullconstraint ck_trantype check ( trantype in( 1, -- insert2, -- update3, -- delete)default 1)go 

接下来,插入test的数据来模拟一个可能的样本。

insert test (starttime, trantype) values (current_timestamp,)insert test (starttime, trantype) values (current_timestamp,)insert test (starttime, trantype) values (current_timestamp,)godeclare @hr intset @hr = datepart(hh, dateadd(hh,-1,current_timestamp) )insert test (starttime, trantype, starthour) _  values (dateadd(hh,-1,current_timestamp),, @hr)insert test (starttime, trantype, starthour) _  values (dateadd(hh,-1,current_timestamp), 1, @hr)insert test (starttime, trantype, starthour) _  values (dateadd(hh,-1,current_timestamp),, @hr)go 

然后用一个查询来找出按日和小时的处理总数。

select startdate tran_day,starthour tran_hour, case trantype when 1 then 'insert'when then 'update'when then 'delete'else 'unknown'end trantype,count(*) tran_totalfromtestgroup bystartdate,starthour,trantypeorder by startdate, starthourcompute sum(count(*)) by startdate, starthourgo

去掉test可以清空test表格。

drop table test
go

 

文章录入:杜斌    责任编辑:杜斌 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
     

    联 系 信 息
    QQ:88236621
    电话:15853773350
    E-Mail:malenurse@163.com
    免费发布招聘信息
    做中国最专业男护士门户网站
    最 新 热 门
     青海省2010年自考报名办…
     网友介绍护师资格考试过…
     [组图]2008年4月计算机等…
     [组图]2008年4月全国计算…
     [组图]2008年4月全国计算…
     [组图]2008年4月计算机等…
     护士执业考试:护理学考试…
     初级护师考试《内科护理…
     初级护师考试《内科护理…
     初级护师考试《内科护理…
    最 新 推 荐
     2008年西医综合大纲生理…
     2008年西医综合大纲-生物…
     2008年西医综合大纲病理…
     2008年西医综合大纲内科…
     2008年西医综合大纲外科…
    相 关 文 章
    2011年护士资格考试:考…
    2009年初级护士资格考试…
    2009年主管护师考试于12…
    2009年初级护士考试于12…
    2009年初级护师考试于12…
    石家庄市关于2009年度护…
    2009年护士专业技术资格…
    医护技能考试周末举行 4…
    护士“托福”今年7月开考
    护士“托福”考试开始报…
    专 题 栏 目