Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.54 KB | None | 0 0
  1. set @StartDate = '2018-03-15 00:00:00';
  2. set @EndDate = '2018-03-15 23:59:59';
  3.  
  4. DELIMITER //
  5. CREATE FUNCTION fn_GetStartDate ()
  6.     RETURNS DATETIME
  7.     DETERMINISTIC NO SQL
  8. BEGIN
  9.   RETURN @StartDate;
  10. END//
  11. DELIMITER ;
  12.  
  13. DELIMITER //
  14. CREATE FUNCTION fn_GetEndDate ()
  15.     RETURNS DATETIME
  16.     DETERMINISTIC NO SQL
  17. BEGIN
  18.   RETURN @EndDate;
  19. END//
  20. DELIMITER ;
  21.  
  22. CREATE OR REPLACE VIEW cdr_call AS
  23. select
  24.   i.id AS id,
  25.   i.answer AS answer,
  26.   i.end AS end,
  27.   i.src AS src,
  28.   i.dst AS dst,
  29.   i.channel AS channel,
  30.   i.dstchannel AS dstchannel,
  31.   i.dcontext AS dcontext,
  32.   i.lastapp AS lastapp,
  33.   i.lastdata AS lastdata,
  34.   (case
  35.      when ((i.lastapp = 'Playback') and (i.lastdata = 'invalid')) then 'NO ANSWER'
  36.      when (i.lastapp = 'Queue') then 'NO ANSWER'
  37.      when (i.dcontext = 'play-busy') then 'BUSY'
  38.      when ((i.hangupcause = 1) and (i.disposition = 'ANSWERED')) then 'BUSY'
  39.      when (i.lastapp = 'VoiceMail') then 'VOICEMAIL'
  40.      when (((i.dcontext like 'ivr%') and (i.disposition = 'NO ANSWER')) or (((i.lastapp like 'BackGround') or (i.lastapp = 'Dial')) and (i.dstchannel = ''))) then 'NO ANSWER'
  41.      when (i.lastapp = 'WaitExten') then 'NO ANSWER' else i.disposition end
  42.   ) AS disposition,
  43.   i.amaflags AS amaflags,
  44.   i.accountcode AS accountcode,
  45.   i.uniqueid AS uniqueid,
  46.   i.userfield AS userfield,
  47.   i.record AS record,
  48.   i.caller_num AS caller_num,
  49.   i.called_num AS called_num,
  50.   i.hops AS hops,
  51.   i.incoming AS incoming,
  52.   i.outgoing AS outgoing,
  53.   i.hangupcause AS hangupcause,
  54.   i.queue AS queue,
  55.   i.linkedid AS linkedid,
  56.   i.uploaded AS uploaded,
  57.   i.sent AS sent,
  58.   i.transfer AS transfer,
  59.   ifnull(i.incoming,i.outgoing) AS did,
  60.   ends.duration as duration,
  61.   ends.billsec as billsec,
  62.   (ends.duration - ends.billsec) AS wait,
  63.   ends.`start` as `start`
  64. from (select
  65.         max(a.id) as latest_id,
  66.         max(`start`) as `start`,
  67.         max(a.duration) as duration,
  68.         max(a.billsec) as billsec,
  69.         count(1) as count
  70.       from ast_cdr_adaptive a
  71.       where
  72.         ((a.hops between 0 and 1) and
  73.          ((a.answer is not null) or
  74.          (isnull(a.answer) and isnull(a.incoming) and (a.outgoing is not null))) and
  75.          a.start BETWEEN fn_GetStartDate() AND fn_GetEndDate())
  76.       group by a.linkedid order by a.`end` desc) as ends
  77.       inner join ast_cdr_adaptive i on (i.id = ends.latest_id);
  78.  
  79. -- пример как run-анать запросы
  80.  
  81. use virt352;
  82.  
  83. set @StartDate = '2018-03-12 00:00:00';
  84. set @EndDate = '2018-03-15 23:59:59';
  85. select count(*) from cdr_call;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement