国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

SQL技巧:創(chuàng)建用來按小時(shí)報(bào)告的查詢

2024-07-21 02:10:45
字體:
供稿:網(wǎng)友
  • 本文來源于網(wǎng)頁設(shè)計(jì)愛好者web開發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問。
  • 要?jiǎng)?chuàng)建一個(gè)可以每個(gè)小時(shí)報(bào)告的查詢,首先要?jiǎng)?chuàng)建一個(gè)表格。該表格一列記錄日期,而沒有時(shí)間信息;另一列記錄鐘點(diǎn)。下面的表格有一列記錄了不同的處理類型。例如,我們可以按小時(shí)找出處理類型的總數(shù)。 

    create table test
    (starttime datetime not null
    default current_timestamp,
    startdate datetime not null
    default convert(datetime, convert(char(10),current_timestamp, 110)),
    starthour int not null
    default datepart(hh,current_timestamp),
    trantype int not null
    constraint ck_trantype check ( trantype in

    1, -- insert
    2, -- update
    3, -- delete
    )
    default 1
    )
    go 
    接下來,插入test的數(shù)據(jù)來模擬一個(gè)可能的樣本。

    insert test (starttime, trantype) values (current_timestamp, 3)
    insert test (starttime, trantype) values (current_timestamp, 2)
    insert test (starttime, trantype) values (current_timestamp, 3)
    go

    declare @hr int
    set @hr = datepart(hh, dateadd(hh,-1,current_timestamp) )

    insert test (starttime, trantype, starthour) _
      values (dateadd(hh,-1,current_timestamp), 3, @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), 2, @hr)
    go 

    然后用一個(gè)查詢來找出按日和小時(shí)的處理總數(shù)。

    select startdate tran_day,
    starthour tran_hour
    , case trantype when 1 then 'insert'
    when 2 then 'update'
    when 3 then 'delete'
    else 'unknown'
    end trantype,
    count(*) tran_total
    from
    test
    group by
    startdate,
    starthour
    ,trantype
    order by startdate, starthour
    compute sum(count(*)) by startdate, starthour
    go
     
    去掉test可以清空test表格。

    drop table test
    go 
    發(fā)表評(píng)論 共有條評(píng)論
    用戶名: 密碼:
    驗(yàn)證碼: 匿名發(fā)表
    主站蜘蛛池模板: 商南县| 克什克腾旗| 康定县| 辽阳市| 吉首市| 蒙城县| 兴和县| 独山县| 桦川县| 同仁县| 扶余县| 赤水市| 武邑县| 安溪县| 封开县| 壤塘县| 靖边县| 当涂县| 湄潭县| 望奎县| 佳木斯市| 祥云县| 松江区| 兴和县| 梓潼县| 新郑市| 建昌县| 雷州市| 福清市| 靖安县| 神农架林区| 高清| 万全县| 会宁县| 林口县| 诸城市| 塘沽区| 略阳县| 汝阳县| 古浪县| 绥滨县|