【问题标题】:Count of transactios by time slot按时隙的事务计数
【发布时间】:2015-07-22 12:17:21
【问题描述】:

我有一个带有时间戳字段的交易历史“TRANSACTION_HISTORY”表

TRANS_ID-----SIGNATURE_TIMESTAMP-------SIGNATURE_USR---INTERNAL_STATUS

2148091------22/07/2015 15:35:06 ---------- RS1------------00

2148091------22/07/2015 15:36:08----------  RS1------------01

2148091------22/07/2015 15:46:07----------  RS1------------B1

2139220------22/07/2015 15:36:07----------  RS1------------00

2148089------22/07/2015 15:31:42----------  GC1------------00

我正在尝试按用户状态在不同时间段按交易类型分组的每日交易计数。 所有具有time stamp <= 9 A.M.then 9 A.M. 等的事务。

Status--------- 9:00 A.M.----------11:00 A.M.------3:00 P.M.---Grand Total

Pending (00) -----7-----------------9--------------21----------37

User1 ------------2-----------------3--------------13----------18

User2 ------------4-----------------2---------------6----------12

User3 ------------1-----------------4---------------2-----------7

Booked(01)  ------3-----------------4--------------12----------19

User1 ------------0-----------------1---------------7-----------8

User2 ------------2-----------------1---------------3-----------6

User3 ------------1-----------------2---------------2-----------5

Completed(B1)-----3-----------------4--------------12----------19

User1 ------------0-----------------1--------------7------------8

User2 ------------2-----------------1--------------3------------6

User3 ------------1-----------------2--------------2------------5

这是我目前写的代码:


选择 t.description,

   case
     when ts.id in ('B1', 'B2', 'B3') then
      'Completed'
     else
      ts.description
   end as status_Desc,
   hist.signature_timestamp,
   usr.login_name as user_name,
   case
     when hist.signature_timestamp <=
          to_date('22/07/2015 09:00:00', 'dd/mm/yyyy hh24:mi:ss') then
      '9 AM'
     when hist.signature_timestamp <=
          to_date('22/07/2015 11:00:00', 'dd/mm/yyyy hh24:mi:ss') then
      '11 AM'
     when hist.signature_timestamp <=
          to_date('22/07/2015 15:00:00', 'dd/mm/yyyy hh24:mi:ss') then
      '3 PM'
     when hist.signature_timestamp <=
          to_date('22/07/2015 18:30:00', 'dd/mm/yyyy hh24:mi:ss') then
      '6:30 PM'
   end as time_Slot from k$transaction_header  h,
   k$transtypes          t,
   k$transaction_history hist,
   k$usr                 usr,
   k$TRANSACTION_STATUS  ts where h.trtype = t.trtype and hist.trans_id = h.id  and usr.id = hist.signature_usr and ts.id = hist.internal_status   and H.ACCOUNT_DTE >= sysdate - 1

谢谢,

优素福

【问题讨论】:

    标签: sql oracle10g timestamp timeslots


    【解决方案1】:

    棘手但试试这个:

        with myCte
    as
    (
    select Signature_Usr,
    Case
     when CONVERT(Time,TimeofTransaction) < CONVERT(TIME,'1900-01-01 9:00:00.000') then Before9
     when CONVERT(Time,TimeofTransaction) < CONVERT(TIME,'1900-01-01 10:00:00.000') and 
      CONVERT(Time,TimeofTransaction) >= CONVERT(TIME,'1900-01-01 9:00:00.000') then After9Before10
     /* And So On */
     End as TimePeriord
     from myTable
    
    )
    
    select Signature_USR,COUNT(Signature_USR) as CountOfTrans,TimePeriod
     from myCte
     Group by Rollup(Signature_USR,TimePeriod)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      • 1970-01-01
      • 1970-01-01
      • 2020-07-26
      • 2015-12-24
      相关资源
      最近更新 更多