Need help on query performance

  • It goes like this:

    Declare @StartDate datetime, @EndDate datetime

    Set @StartDate = --beginning of the month

    Dateadd(mm, datediff(mm,0, @Date), 0)

    SET @EndDate = -- end of the month

    Dateadd(mm, 1, @StartDate)

    SELECT ....

    WHERE @StartDate <= start_time and start_time < @EndDate

    _____________
    Code for TallyGenerator

  • Thanks ! I wonder why I never thought of that !

  • Date (start_time)--report_item----EN (lang)---BM (lang)

    00:00 - 00:30-----Noisy----------------------3------------- 0

    00:00 - 00:30-----No Dial Tone-------------6--------------2

    00:30 - 01:00-----Noisy----------------------1--------------2

    00:30 - 01:00-----No Dial Tone-------------0------------- 7

    -----------------------------------------------------------------

    My query is able to output the result as above but it is extremely long and involve another separate table in order to sort the Date format as 00:00 - 00:30, 00:30 - 01:00 till 23:30 - 00:00.

    I was trying to use Sergiy's query to do it but unable to get the date in this format without using another table. Can anyone help? Full result is attached in excel sheet.

    Thanks.

  • I would strongly advise using the database layer to return the data, and the presentation layer to make it look pretty to the user. If you insist on formatting in the query, though, surely it's just a question of casting your the dates as time, then casting as char, then concatenating, then chopping off the seconds portion?

    John

  • If I use presentation layer (crystal report in my case) to organize it, would it take longer time?

    After chopping off the second portion, how could I compare the time? Does it look like this?

    WHERE @start_time >= '00:00' and @start_time <= '00:30'

    @start_time >= '00:30' and @start_time <= '01:00'

  • You asked about formatting the output. What you do in your WHERE clause doesn't make any difference to that. But the WHERE clause you posted doesn't contain any column names, so (assuming you don't get any errors) it will either return all rows or no rows. Please will you post the full query?

    John

  • jc85 (6/1/2016)


    Date (start_time)--report_item----EN (lang)---BM (lang)

    00:00 - 00:30-----Noisy----------------------3------------- 0

    00:00 - 00:30-----No Dial Tone-------------6--------------2

    00:30 - 01:00-----Noisy----------------------1--------------2

    00:30 - 01:00-----No Dial Tone-------------0------------- 7

    -----------------------------------------------------------------

    My query is able to output the result as above but it is extremely long and involve another separate table in order to sort the Date format as 00:00 - 00:30, 00:30 - 01:00 till 23:30 - 00:00.

    I was trying to use Sergiy's query to do it but unable to get the date in this format without using another table. Can anyone help? Full result is attached in excel sheet.

    Thanks.

    Now you're gonna hit your head again asking "why did not i think of it?".

    It's so easy. 🙂

    You group you'd events by day:

    dateadd(dd,0, datediff(dd,0,b.start_time)) as [date]

    But what you need is to group by 30 min time slots.

    So, if that's what you need - just do it!

    dateadd(n,0, datediff(n,0,b.start_time)/30*30) as [time slot]

    Then, when presenting the result you may split [time slot] into date and time parts - it's up to consumers of the report how do they wanna see it.

    No need to touch WHERE clause at all.

    _____________
    Code for TallyGenerator

  • John Mitchell-245523 (6/1/2016)


    You asked about formatting the output. What you do in your WHERE clause doesn't make any difference to that. But the WHERE clause you posted doesn't contain any column names, so (assuming you don't get any errors) it will either return all rows or no rows. Please will you post the full query?

    John

    rpt_GetMenuSelectionByInterval will be called, passing call flow name and date selected by user.

    Thanks in advance.

    ALTER PROCEDURE [dbo].[rpt_GetMenuSelectionByInterval]

    @call_flow_name nvarchar(50),

    @SelectedDate datetime

    AS

    --Create a temporary Table

    CREATE TABLE #tempTable(

    interval_id int,

    call_flow_name nvarchar(50),

    source nvarchar(50),

    lang nvarchar(5),

    hit int,

    report_item nvarchar(100)

    )

    IF @call_flow_name = 'Technical'

    INSERT INTO #tempTable exec [rpt_GetMenuSelectionTechnicalIntervalCountTable] @call_flow_name, @SelectedDate

    SELECT tb.interval as date, (tb.source + '-' + tb.report_item) as report_item, sum(isnull(tb1.EN,0))as 'EN',sum(isnull(tb1.BM,0))as 'BM'

    FROM (

    SELECT b.id, b.interval, a.report_item, a.call_flow_name, a.source

    FROM ann_reportitem a, ann_interval b

    WHERE a.report_id =8 AND a.call_flow_name =@call_flow_name

    ) as tb

    LEFT JOIN

    (

    SELECT interval_id, call_flow_name, source, hit as EN, 0 as BM, 0 as MD, report_item FROM #tempTable WHERE lang ='EN'

    UNION ALL

    SELECT interval_id, call_flow_name, source, 0 as EN, hit as BM, 0 as MD, report_item FROM #tempTable WHERE lang ='BM'

    UNION ALL

    SELECT interval_id, call_flow_name, source, 0 as EN, 0 as BM, hit as MD, report_item FROM #tempTable WHERE lang ='MD'

    ) as tb1 ON tb.id=tb1.interval_id AND tb.call_flow_name=tb1.call_flow_name

    AND tb.source = tb1.source and tb.report_item =tb1.report_item

    GROUP BY tb.interval, tb.report_item, tb.source

    ORDER BY tb.interval

    --Drop Temp Table

    DROP TABLE #tempTable

    ALTER PROCEDURE [dbo].[rpt_GetMenuSelectionTechnicalIntervalCountTable]

    @call_flow_name nvarchar(50),

    @SelectedDate datetime

    AS

    SELECT

    (CASE when b.start_time >= @SelectedDate + ' 00:00:00' and b.start_time < @SelectedDate + ' 00:30:00' then '1'

    when b.start_time >= @SelectedDate + ' 00:30:00' and b.start_time < @SelectedDate + ' 01:00:00' then '2'

    when b.start_time >= @SelectedDate + ' 01:00:00' and b.start_time < @SelectedDate + ' 01:30:00' then '3'

    when b.start_time >= @SelectedDate + ' 01:30:00' and b.start_time < @SelectedDate + ' 02:00:00' then '4'

    when b.start_time >= @SelectedDate + ' 02:00:00' and b.start_time < @SelectedDate + ' 02:30:00' then '5'

    when b.start_time >= @SelectedDate + ' 02:30:00' and b.start_time < @SelectedDate + ' 03:00:00' then '6'

    when b.start_time >= @SelectedDate + ' 03:00:00' and b.start_time < @SelectedDate + ' 03:30:00' then '7'

    when b.start_time >= @SelectedDate + ' 03:30:00' and b.start_time < @SelectedDate + ' 04:00:00' then '8'

    when b.start_time >= @SelectedDate + ' 04:00:00' and b.start_time < @SelectedDate + ' 04:30:00' then '9'

    when b.start_time >= @SelectedDate + ' 04:30:00' and b.start_time < @SelectedDate + ' 05:00:00' then '10'

    when b.start_time >= @SelectedDate + ' 05:00:00' and b.start_time < @SelectedDate + ' 05:30:00' then '11'

    when b.start_time >= @SelectedDate + ' 05:30:00' and b.start_time < @SelectedDate + ' 06:00:00' then '12'

    when b.start_time >= @SelectedDate + ' 06:00:00' and b.start_time < @SelectedDate + ' 06:30:00' then '13'

    when b.start_time >= @SelectedDate + ' 06:30:00' and b.start_time < @SelectedDate + ' 07:00:00' then '14'

    when b.start_time >= @SelectedDate + ' 07:00:00' and b.start_time < @SelectedDate + ' 07:30:00' then '15'

    when b.start_time >= @SelectedDate + ' 07:30:00' and b.start_time < @SelectedDate + ' 08:00:00' then '16'

    when b.start_time >= @SelectedDate + ' 08:00:00' and b.start_time < @SelectedDate + ' 08:30:00' then '17'

    when b.start_time >= @SelectedDate + ' 08:30:00' and b.start_time < @SelectedDate + ' 09:00:00' then '18'

    when b.start_time >= @SelectedDate + ' 09:00:00' and b.start_time < @SelectedDate + ' 09:30:00' then '19'

    when b.start_time >= @SelectedDate + ' 09:30:00' and b.start_time < @SelectedDate + ' 10:00:00' then '20'

    when b.start_time >= @SelectedDate + ' 10:00:00' and b.start_time < @SelectedDate + ' 10:30:00' then '21'

    when b.start_time >= @SelectedDate + ' 10:30:00' and b.start_time < @SelectedDate + ' 11:00:00' then '22'

    when b.start_time >= @SelectedDate + ' 11:00:00' and b.start_time < @SelectedDate + ' 11:30:00' then '23'

    when b.start_time >= @SelectedDate + ' 11:30:00' and b.start_time < @SelectedDate + ' 12:00:00' then '24'

    when b.start_time >= @SelectedDate + ' 12:00:00' and b.start_time < @SelectedDate + ' 12:30:00' then '25'

    when b.start_time >= @SelectedDate + ' 12:30:00' and b.start_time < @SelectedDate + ' 13:00:00' then '26'

    when b.start_time >= @SelectedDate + ' 13:00:00' and b.start_time < @SelectedDate + ' 13:30:00' then '27'

    when b.start_time >= @SelectedDate + ' 13:30:00' and b.start_time < @SelectedDate + ' 14:00:00' then '28'

    when b.start_time >= @SelectedDate + ' 14:00:00' and b.start_time < @SelectedDate + ' 14:30:00' then '29'

    when b.start_time >= @SelectedDate + ' 14:30:00' and b.start_time < @SelectedDate + ' 15:00:00' then '30'

    when b.start_time >= @SelectedDate + ' 15:00:00' and b.start_time < @SelectedDate + ' 15:30:00' then '31'

    when b.start_time >= @SelectedDate + ' 15:30:00' and b.start_time < @SelectedDate + ' 16:00:00' then '32'

    when b.start_time >= @SelectedDate + ' 16:00:00' and b.start_time < @SelectedDate + ' 16:30:00' then '33'

    when b.start_time >= @SelectedDate + ' 16:30:00' and b.start_time < @SelectedDate + ' 17:00:00' then '34'

    when b.start_time >= @SelectedDate + ' 17:00:00' and b.start_time < @SelectedDate + ' 17:30:00' then '35'

    when b.start_time >= @SelectedDate + ' 17:30:00' and b.start_time < @SelectedDate + ' 18:00:00' then '36'

    when b.start_time >= @SelectedDate + ' 18:00:00' and b.start_time < @SelectedDate + ' 18:30:00' then '37'

    when b.start_time >= @SelectedDate + ' 18:30:00' and b.start_time < @SelectedDate + ' 19:00:00' then '38'

    when b.start_time >= @SelectedDate + ' 19:00:00' and b.start_time < @SelectedDate + ' 19:30:00' then '39'

    when b.start_time >= @SelectedDate + ' 19:30:00' and b.start_time < @SelectedDate + ' 20:00:00' then '40'

    when b.start_time >= @SelectedDate + ' 20:00:00' and b.start_time < @SelectedDate + ' 20:30:00' then '41'

    when b.start_time >= @SelectedDate + ' 20:30:00' and b.start_time < @SelectedDate + ' 21:00:00' then '42'

    when b.start_time >= @SelectedDate + ' 21:00:00' and b.start_time < @SelectedDate + ' 21:30:00' then '43'

    when b.start_time >= @SelectedDate + ' 21:30:00' and b.start_time < @SelectedDate + ' 22:00:00' then '44'

    when b.start_time >= @SelectedDate + ' 22:00:00' and b.start_time < @SelectedDate + ' 22:30:00' then '45'

    when b.start_time >= @SelectedDate + ' 22:30:00' and b.start_time < @SelectedDate + ' 23:00:00' then '46'

    when b.start_time >= @SelectedDate + ' 23:00:00' and b.start_time < @SelectedDate + ' 23:30:00' then '47'

    when b.start_time >= @SelectedDate + ' 23:30:00' and b.start_time <= @SelectedDate + ' 23:59:59' then '48'

    ELSE 'Other' END )as interval_id,

    b.call_flow_name, b.source, b.lang, isnull(count(b.call_flow_name),0) as hit, 'No Dial Tone' as report_item

    FROM ann_events_Tech_Details b

    WHERE(dateadd(dd,0, datediff(dd,0,b.start_time)))= @SelectedDate AND b.call_flow_name = @call_flow_name and b.sel_tel_nodialtone =1

    GROUP BY

    (CASE when b.start_time >= @SelectedDate + ' 00:00:00' and b.start_time < @SelectedDate + ' 00:30:00' then '1'

    when b.start_time >= @SelectedDate + ' 00:30:00' and b.start_time < @SelectedDate + ' 01:00:00' then '2'

    when b.start_time >= @SelectedDate + ' 01:00:00' and b.start_time < @SelectedDate + ' 01:30:00' then '3'

    when b.start_time >= @SelectedDate + ' 01:30:00' and b.start_time < @SelectedDate + ' 02:00:00' then '4'

    when b.start_time >= @SelectedDate + ' 02:00:00' and b.start_time < @SelectedDate + ' 02:30:00' then '5'

    when b.start_time >= @SelectedDate + ' 02:30:00' and b.start_time < @SelectedDate + ' 03:00:00' then '6'

    when b.start_time >= @SelectedDate + ' 03:00:00' and b.start_time < @SelectedDate + ' 03:30:00' then '7'

    when b.start_time >= @SelectedDate + ' 03:30:00' and b.start_time < @SelectedDate + ' 04:00:00' then '8'

    when b.start_time >= @SelectedDate + ' 04:00:00' and b.start_time < @SelectedDate + ' 04:30:00' then '9'

    when b.start_time >= @SelectedDate + ' 04:30:00' and b.start_time < @SelectedDate + ' 05:00:00' then '10'

    when b.start_time >= @SelectedDate + ' 05:00:00' and b.start_time < @SelectedDate + ' 05:30:00' then '11'

    when b.start_time >= @SelectedDate + ' 05:30:00' and b.start_time < @SelectedDate + ' 06:00:00' then '12'

    when b.start_time >= @SelectedDate + ' 06:00:00' and b.start_time < @SelectedDate + ' 06:30:00' then '13'

    when b.start_time >= @SelectedDate + ' 06:30:00' and b.start_time < @SelectedDate + ' 07:00:00' then '14'

    when b.start_time >= @SelectedDate + ' 07:00:00' and b.start_time < @SelectedDate + ' 07:30:00' then '15'

    when b.start_time >= @SelectedDate + ' 07:30:00' and b.start_time < @SelectedDate + ' 08:00:00' then '16'

    when b.start_time >= @SelectedDate + ' 08:00:00' and b.start_time < @SelectedDate + ' 08:30:00' then '17'

    when b.start_time >= @SelectedDate + ' 08:30:00' and b.start_time < @SelectedDate + ' 09:00:00' then '18'

    when b.start_time >= @SelectedDate + ' 09:00:00' and b.start_time < @SelectedDate + ' 09:30:00' then '19'

    when b.start_time >= @SelectedDate + ' 09:30:00' and b.start_time < @SelectedDate + ' 10:00:00' then '20'

    when b.start_time >= @SelectedDate + ' 10:00:00' and b.start_time < @SelectedDate + ' 10:30:00' then '21'

    when b.start_time >= @SelectedDate + ' 10:30:00' and b.start_time < @SelectedDate + ' 11:00:00' then '22'

    when b.start_time >= @SelectedDate + ' 11:00:00' and b.start_time < @SelectedDate + ' 11:30:00' then '23'

    when b.start_time >= @SelectedDate + ' 11:30:00' and b.start_time < @SelectedDate + ' 12:00:00' then '24'

    when b.start_time >= @SelectedDate + ' 12:00:00' and b.start_time < @SelectedDate + ' 12:30:00' then '25'

    when b.start_time >= @SelectedDate + ' 12:30:00' and b.start_time < @SelectedDate + ' 13:00:00' then '26'

    when b.start_time >= @SelectedDate + ' 13:00:00' and b.start_time < @SelectedDate + ' 13:30:00' then '27'

    when b.start_time >= @SelectedDate + ' 13:30:00' and b.start_time < @SelectedDate + ' 14:00:00' then '28'

    when b.start_time >= @SelectedDate + ' 14:00:00' and b.start_time < @SelectedDate + ' 14:30:00' then '29'

    when b.start_time >= @SelectedDate + ' 14:30:00' and b.start_time < @SelectedDate + ' 15:00:00' then '30'

    when b.start_time >= @SelectedDate + ' 15:00:00' and b.start_time < @SelectedDate + ' 15:30:00' then '31'

    when b.start_time >= @SelectedDate + ' 15:30:00' and b.start_time < @SelectedDate + ' 16:00:00' then '32'

    when b.start_time >= @SelectedDate + ' 16:00:00' and b.start_time < @SelectedDate + ' 16:30:00' then '33'

    when b.start_time >= @SelectedDate + ' 16:30:00' and b.start_time < @SelectedDate + ' 17:00:00' then '34'

    when b.start_time >= @SelectedDate + ' 17:00:00' and b.start_time < @SelectedDate + ' 17:30:00' then '35'

    when b.start_time >= @SelectedDate + ' 17:30:00' and b.start_time < @SelectedDate + ' 18:00:00' then '36'

    when b.start_time >= @SelectedDate + ' 18:00:00' and b.start_time < @SelectedDate + ' 18:30:00' then '37'

    when b.start_time >= @SelectedDate + ' 18:30:00' and b.start_time < @SelectedDate + ' 19:00:00' then '38'

    when b.start_time >= @SelectedDate + ' 19:00:00' and b.start_time < @SelectedDate + ' 19:30:00' then '39'

    when b.start_time >= @SelectedDate + ' 19:30:00' and b.start_time < @SelectedDate + ' 20:00:00' then '40'

    when b.start_time >= @SelectedDate + ' 20:00:00' and b.start_time < @SelectedDate + ' 20:30:00' then '41'

    when b.start_time >= @SelectedDate + ' 20:30:00' and b.start_time < @SelectedDate + ' 21:00:00' then '42'

    when b.start_time >= @SelectedDate + ' 21:00:00' and b.start_time < @SelectedDate + ' 21:30:00' then '43'

    when b.start_time >= @SelectedDate + ' 21:30:00' and b.start_time < @SelectedDate + ' 22:00:00' then '44'

    when b.start_time >= @SelectedDate + ' 22:00:00' and b.start_time < @SelectedDate + ' 22:30:00' then '45'

    when b.start_time >= @SelectedDate + ' 22:30:00' and b.start_time < @SelectedDate + ' 23:00:00' then '46'

    when b.start_time >= @SelectedDate + ' 23:00:00' and b.start_time < @SelectedDate + ' 23:30:00' then '47'

    when b.start_time >= @SelectedDate + ' 23:30:00' and b.start_time <= @SelectedDate + ' 23:59:59' then '48'

    ELSE 'Other' END), b.call_flow_name, b.source, b.lang

    UNION

    SELECT

    (CASE when b.start_time >= @SelectedDate + ' 00:00:00' and b.start_time < @SelectedDate + ' 00:30:00' then '1'

    when b.start_time >= @SelectedDate + ' 00:30:00' and b.start_time < @SelectedDate + ' 01:00:00' then '2'

    when b.start_time >= @SelectedDate + ' 01:00:00' and b.start_time < @SelectedDate + ' 01:30:00' then '3'

    when b.start_time >= @SelectedDate + ' 01:30:00' and b.start_time < @SelectedDate + ' 02:00:00' then '4'

    when b.start_time >= @SelectedDate + ' 02:00:00' and b.start_time < @SelectedDate + ' 02:30:00' then '5'

    when b.start_time >= @SelectedDate + ' 02:30:00' and b.start_time < @SelectedDate + ' 03:00:00' then '6'

    when b.start_time >= @SelectedDate + ' 03:00:00' and b.start_time < @SelectedDate + ' 03:30:00' then '7'

    when b.start_time >= @SelectedDate + ' 03:30:00' and b.start_time < @SelectedDate + ' 04:00:00' then '8'

    when b.start_time >= @SelectedDate + ' 04:00:00' and b.start_time < @SelectedDate + ' 04:30:00' then '9'

    when b.start_time >= @SelectedDate + ' 04:30:00' and b.start_time < @SelectedDate + ' 05:00:00' then '10'

    when b.start_time >= @SelectedDate + ' 05:00:00' and b.start_time < @SelectedDate + ' 05:30:00' then '11'

    when b.start_time >= @SelectedDate + ' 05:30:00' and b.start_time < @SelectedDate + ' 06:00:00' then '12'

    when b.start_time >= @SelectedDate + ' 06:00:00' and b.start_time < @SelectedDate + ' 06:30:00' then '13'

    when b.start_time >= @SelectedDate + ' 06:30:00' and b.start_time < @SelectedDate + ' 07:00:00' then '14'

    when b.start_time >= @SelectedDate + ' 07:00:00' and b.start_time < @SelectedDate + ' 07:30:00' then '15'

    when b.start_time >= @SelectedDate + ' 07:30:00' and b.start_time < @SelectedDate + ' 08:00:00' then '16'

    when b.start_time >= @SelectedDate + ' 08:00:00' and b.start_time < @SelectedDate + ' 08:30:00' then '17'

    when b.start_time >= @SelectedDate + ' 08:30:00' and b.start_time < @SelectedDate + ' 09:00:00' then '18'

    when b.start_time >= @SelectedDate + ' 09:00:00' and b.start_time < @SelectedDate + ' 09:30:00' then '19'

    when b.start_time >= @SelectedDate + ' 09:30:00' and b.start_time < @SelectedDate + ' 10:00:00' then '20'

    when b.start_time >= @SelectedDate + ' 10:00:00' and b.start_time < @SelectedDate + ' 10:30:00' then '21'

    when b.start_time >= @SelectedDate + ' 10:30:00' and b.start_time < @SelectedDate + ' 11:00:00' then '22'

    when b.start_time >= @SelectedDate + ' 11:00:00' and b.start_time < @SelectedDate + ' 11:30:00' then '23'

    when b.start_time >= @SelectedDate + ' 11:30:00' and b.start_time < @SelectedDate + ' 12:00:00' then '24'

    when b.start_time >= @SelectedDate + ' 12:00:00' and b.start_time < @SelectedDate + ' 12:30:00' then '25'

    when b.start_time >= @SelectedDate + ' 12:30:00' and b.start_time < @SelectedDate + ' 13:00:00' then '26'

    when b.start_time >= @SelectedDate + ' 13:00:00' and b.start_time < @SelectedDate + ' 13:30:00' then '27'

    when b.start_time >= @SelectedDate + ' 13:30:00' and b.start_time < @SelectedDate + ' 14:00:00' then '28'

    when b.start_time >= @SelectedDate + ' 14:00:00' and b.start_time < @SelectedDate + ' 14:30:00' then '29'

    when b.start_time >= @SelectedDate + ' 14:30:00' and b.start_time < @SelectedDate + ' 15:00:00' then '30'

    when b.start_time >= @SelectedDate + ' 15:00:00' and b.start_time < @SelectedDate + ' 15:30:00' then '31'

    when b.start_time >= @SelectedDate + ' 15:30:00' and b.start_time < @SelectedDate + ' 16:00:00' then '32'

    when b.start_time >= @SelectedDate + ' 16:00:00' and b.start_time < @SelectedDate + ' 16:30:00' then '33'

    when b.start_time >= @SelectedDate + ' 16:30:00' and b.start_time < @SelectedDate + ' 17:00:00' then '34'

    when b.start_time >= @SelectedDate + ' 17:00:00' and b.start_time < @SelectedDate + ' 17:30:00' then '35'

    when b.start_time >= @SelectedDate + ' 17:30:00' and b.start_time < @SelectedDate + ' 18:00:00' then '36'

    when b.start_time >= @SelectedDate + ' 18:00:00' and b.start_time < @SelectedDate + ' 18:30:00' then '37'

    when b.start_time >= @SelectedDate + ' 18:30:00' and b.start_time < @SelectedDate + ' 19:00:00' then '38'

    when b.start_time >= @SelectedDate + ' 19:00:00' and b.start_time < @SelectedDate + ' 19:30:00' then '39'

    when b.start_time >= @SelectedDate + ' 19:30:00' and b.start_time < @SelectedDate + ' 20:00:00' then '40'

    when b.start_time >= @SelectedDate + ' 20:00:00' and b.start_time < @SelectedDate + ' 20:30:00' then '41'

    when b.start_time >= @SelectedDate + ' 20:30:00' and b.start_time < @SelectedDate + ' 21:00:00' then '42'

    when b.start_time >= @SelectedDate + ' 21:00:00' and b.start_time < @SelectedDate + ' 21:30:00' then '43'

    when b.start_time >= @SelectedDate + ' 21:30:00' and b.start_time < @SelectedDate + ' 22:00:00' then '44'

    when b.start_time >= @SelectedDate + ' 22:00:00' and b.start_time < @SelectedDate + ' 22:30:00' then '45'

    when b.start_time >= @SelectedDate + ' 22:30:00' and b.start_time < @SelectedDate + ' 23:00:00' then '46'

    when b.start_time >= @SelectedDate + ' 23:00:00' and b.start_time < @SelectedDate + ' 23:30:00' then '47'

    when b.start_time >= @SelectedDate + ' 23:30:00' and b.start_time <= @SelectedDate + ' 23:59:59' then '48'

    ELSE 'Other' END )as interval_id,

    b.call_flow_name, b.source, b.lang, isnull(count(b.call_flow_name),0) as hit, 'Noisy' as report_item

    FROM ann_events_Tech_Details b

    WHERE(dateadd(dd,0, datediff(dd,0,b.start_time)))= @SelectedDate AND b.call_flow_name = @call_flow_name and b.sel_tel_noisy =1

    GROUP BY

    (CASE when b.start_time >= @SelectedDate + ' 00:00:00' and b.start_time < @SelectedDate + ' 00:30:00' then '1'

    when b.start_time >= @SelectedDate + ' 00:30:00' and b.start_time < @SelectedDate + ' 01:00:00' then '2'

    when b.start_time >= @SelectedDate + ' 01:00:00' and b.start_time < @SelectedDate + ' 01:30:00' then '3'

    when b.start_time >= @SelectedDate + ' 01:30:00' and b.start_time < @SelectedDate + ' 02:00:00' then '4'

    when b.start_time >= @SelectedDate + ' 02:00:00' and b.start_time < @SelectedDate + ' 02:30:00' then '5'

    when b.start_time >= @SelectedDate + ' 02:30:00' and b.start_time < @SelectedDate + ' 03:00:00' then '6'

    when b.start_time >= @SelectedDate + ' 03:00:00' and b.start_time < @SelectedDate + ' 03:30:00' then '7'

    when b.start_time >= @SelectedDate + ' 03:30:00' and b.start_time < @SelectedDate + ' 04:00:00' then '8'

    when b.start_time >= @SelectedDate + ' 04:00:00' and b.start_time < @SelectedDate + ' 04:30:00' then '9'

    when b.start_time >= @SelectedDate + ' 04:30:00' and b.start_time < @SelectedDate + ' 05:00:00' then '10'

    when b.start_time >= @SelectedDate + ' 05:00:00' and b.start_time < @SelectedDate + ' 05:30:00' then '11'

    when b.start_time >= @SelectedDate + ' 05:30:00' and b.start_time < @SelectedDate + ' 06:00:00' then '12'

    when b.start_time >= @SelectedDate + ' 06:00:00' and b.start_time < @SelectedDate + ' 06:30:00' then '13'

    when b.start_time >= @SelectedDate + ' 06:30:00' and b.start_time < @SelectedDate + ' 07:00:00' then '14'

    when b.start_time >= @SelectedDate + ' 07:00:00' and b.start_time < @SelectedDate + ' 07:30:00' then '15'

    when b.start_time >= @SelectedDate + ' 07:30:00' and b.start_time < @SelectedDate + ' 08:00:00' then '16'

    when b.start_time >= @SelectedDate + ' 08:00:00' and b.start_time < @SelectedDate + ' 08:30:00' then '17'

    when b.start_time >= @SelectedDate + ' 08:30:00' and b.start_time < @SelectedDate + ' 09:00:00' then '18'

    when b.start_time >= @SelectedDate + ' 09:00:00' and b.start_time < @SelectedDate + ' 09:30:00' then '19'

    when b.start_time >= @SelectedDate + ' 09:30:00' and b.start_time < @SelectedDate + ' 10:00:00' then '20'

    when b.start_time >= @SelectedDate + ' 10:00:00' and b.start_time < @SelectedDate + ' 10:30:00' then '21'

    when b.start_time >= @SelectedDate + ' 10:30:00' and b.start_time < @SelectedDate + ' 11:00:00' then '22'

    when b.start_time >= @SelectedDate + ' 11:00:00' and b.start_time < @SelectedDate + ' 11:30:00' then '23'

    when b.start_time >= @SelectedDate + ' 11:30:00' and b.start_time < @SelectedDate + ' 12:00:00' then '24'

    when b.start_time >= @SelectedDate + ' 12:00:00' and b.start_time < @SelectedDate + ' 12:30:00' then '25'

    when b.start_time >= @SelectedDate + ' 12:30:00' and b.start_time < @SelectedDate + ' 13:00:00' then '26'

    when b.start_time >= @SelectedDate + ' 13:00:00' and b.start_time < @SelectedDate + ' 13:30:00' then '27'

    when b.start_time >= @SelectedDate + ' 13:30:00' and b.start_time < @SelectedDate + ' 14:00:00' then '28'

    when b.start_time >= @SelectedDate + ' 14:00:00' and b.start_time < @SelectedDate + ' 14:30:00' then '29'

    when b.start_time >= @SelectedDate + ' 14:30:00' and b.start_time < @SelectedDate + ' 15:00:00' then '30'

    when b.start_time >= @SelectedDate + ' 15:00:00' and b.start_time < @SelectedDate + ' 15:30:00' then '31'

    when b.start_time >= @SelectedDate + ' 15:30:00' and b.start_time < @SelectedDate + ' 16:00:00' then '32'

    when b.start_time >= @SelectedDate + ' 16:00:00' and b.start_time < @SelectedDate + ' 16:30:00' then '33'

    when b.start_time >= @SelectedDate + ' 16:30:00' and b.start_time < @SelectedDate + ' 17:00:00' then '34'

    when b.start_time >= @SelectedDate + ' 17:00:00' and b.start_time < @SelectedDate + ' 17:30:00' then '35'

    when b.start_time >= @SelectedDate + ' 17:30:00' and b.start_time < @SelectedDate + ' 18:00:00' then '36'

    when b.start_time >= @SelectedDate + ' 18:00:00' and b.start_time < @SelectedDate + ' 18:30:00' then '37'

    when b.start_time >= @SelectedDate + ' 18:30:00' and b.start_time < @SelectedDate + ' 19:00:00' then '38'

    when b.start_time >= @SelectedDate + ' 19:00:00' and b.start_time < @SelectedDate + ' 19:30:00' then '39'

    when b.start_time >= @SelectedDate + ' 19:30:00' and b.start_time < @SelectedDate + ' 20:00:00' then '40'

    when b.start_time >= @SelectedDate + ' 20:00:00' and b.start_time < @SelectedDate + ' 20:30:00' then '41'

    when b.start_time >= @SelectedDate + ' 20:30:00' and b.start_time < @SelectedDate + ' 21:00:00' then '42'

    when b.start_time >= @SelectedDate + ' 21:00:00' and b.start_time < @SelectedDate + ' 21:30:00' then '43'

    when b.start_time >= @SelectedDate + ' 21:30:00' and b.start_time < @SelectedDate + ' 22:00:00' then '44'

    when b.start_time >= @SelectedDate + ' 22:00:00' and b.start_time < @SelectedDate + ' 22:30:00' then '45'

    when b.start_time >= @SelectedDate + ' 22:30:00' and b.start_time < @SelectedDate + ' 23:00:00' then '46'

    when b.start_time >= @SelectedDate + ' 23:00:00' and b.start_time < @SelectedDate + ' 23:30:00' then '47'

    when b.start_time >= @SelectedDate + ' 23:30:00' and b.start_time <= @SelectedDate + ' 23:59:59' then '48'

    ELSE 'Other' END), b.call_flow_name, b.source, b.lang

    UNION

    SELECT

    (CASE when b.start_time >= @SelectedDate + ' 00:00:00' and b.start_time < @SelectedDate + ' 00:30:00' then '1'

    when b.start_time >= @SelectedDate + ' 00:30:00' and b.start_time < @SelectedDate + ' 01:00:00' then '2'

    when b.start_time >= @SelectedDate + ' 01:00:00' and b.start_time < @SelectedDate + ' 01:30:00' then '3'

    when b.start_time >= @SelectedDate + ' 01:30:00' and b.start_time < @SelectedDate + ' 02:00:00' then '4'

    when b.start_time >= @SelectedDate + ' 02:00:00' and b.start_time < @SelectedDate + ' 02:30:00' then '5'

    when b.start_time >= @SelectedDate + ' 02:30:00' and b.start_time < @SelectedDate + ' 03:00:00' then '6'

    when b.start_time >= @SelectedDate + ' 03:00:00' and b.start_time < @SelectedDate + ' 03:30:00' then '7'

    when b.start_time >= @SelectedDate + ' 03:30:00' and b.start_time < @SelectedDate + ' 04:00:00' then '8'

    when b.start_time >= @SelectedDate + ' 04:00:00' and b.start_time < @SelectedDate + ' 04:30:00' then '9'

    when b.start_time >= @SelectedDate + ' 04:30:00' and b.start_time < @SelectedDate + ' 05:00:00' then '10'

    when b.start_time >= @SelectedDate + ' 05:00:00' and b.start_time < @SelectedDate + ' 05:30:00' then '11'

    when b.start_time >= @SelectedDate + ' 05:30:00' and b.start_time < @SelectedDate + ' 06:00:00' then '12'

    when b.start_time >= @SelectedDate + ' 06:00:00' and b.start_time < @SelectedDate + ' 06:30:00' then '13'

    when b.start_time >= @SelectedDate + ' 06:30:00' and b.start_time < @SelectedDate + ' 07:00:00' then '14'

    when b.start_time >= @SelectedDate + ' 07:00:00' and b.start_time < @SelectedDate + ' 07:30:00' then '15'

    when b.start_time >= @SelectedDate + ' 07:30:00' and b.start_time < @SelectedDate + ' 08:00:00' then '16'

    when b.start_time >= @SelectedDate + ' 08:00:00' and b.start_time < @SelectedDate + ' 08:30:00' then '17'

    when b.start_time >= @SelectedDate + ' 08:30:00' and b.start_time < @SelectedDate + ' 09:00:00' then '18'

    when b.start_time >= @SelectedDate + ' 09:00:00' and b.start_time < @SelectedDate + ' 09:30:00' then '19'

    when b.start_time >= @SelectedDate + ' 09:30:00' and b.start_time < @SelectedDate + ' 10:00:00' then '20'

    when b.start_time >= @SelectedDate + ' 10:00:00' and b.start_time < @SelectedDate + ' 10:30:00' then '21'

    when b.start_time >= @SelectedDate + ' 10:30:00' and b.start_time < @SelectedDate + ' 11:00:00' then '22'

    when b.start_time >= @SelectedDate + ' 11:00:00' and b.start_time < @SelectedDate + ' 11:30:00' then '23'

    when b.start_time >= @SelectedDate + ' 11:30:00' and b.start_time < @SelectedDate + ' 12:00:00' then '24'

    when b.start_time >= @SelectedDate + ' 12:00:00' and b.start_time < @SelectedDate + ' 12:30:00' then '25'

    when b.start_time >= @SelectedDate + ' 12:30:00' and b.start_time < @SelectedDate + ' 13:00:00' then '26'

    when b.start_time >= @SelectedDate + ' 13:00:00' and b.start_time < @SelectedDate + ' 13:30:00' then '27'

    when b.start_time >= @SelectedDate + ' 13:30:00' and b.start_time < @SelectedDate + ' 14:00:00' then '28'

    when b.start_time >= @SelectedDate + ' 14:00:00' and b.start_time < @SelectedDate + ' 14:30:00' then '29'

    when b.start_time >= @SelectedDate + ' 14:30:00' and b.start_time < @SelectedDate + ' 15:00:00' then '30'

    when b.start_time >= @SelectedDate + ' 15:00:00' and b.start_time < @SelectedDate + ' 15:30:00' then '31'

    when b.start_time >= @SelectedDate + ' 15:30:00' and b.start_time < @SelectedDate + ' 16:00:00' then '32'

    when b.start_time >= @SelectedDate + ' 16:00:00' and b.start_time < @SelectedDate + ' 16:30:00' then '33'

    when b.start_time >= @SelectedDate + ' 16:30:00' and b.start_time < @SelectedDate + ' 17:00:00' then '34'

    when b.start_time >= @SelectedDate + ' 17:00:00' and b.start_time < @SelectedDate + ' 17:30:00' then '35'

    when b.start_time >= @SelectedDate + ' 17:30:00' and b.start_time < @SelectedDate + ' 18:00:00' then '36'

    when b.start_time >= @SelectedDate + ' 18:00:00' and b.start_time < @SelectedDate + ' 18:30:00' then '37'

    when b.start_time >= @SelectedDate + ' 18:30:00' and b.start_time < @SelectedDate + ' 19:00:00' then '38'

    when b.start_time >= @SelectedDate + ' 19:00:00' and b.start_time < @SelectedDate + ' 19:30:00' then '39'

    when b.start_time >= @SelectedDate + ' 19:30:00' and b.start_time < @SelectedDate + ' 20:00:00' then '40'

    when b.start_time >= @SelectedDate + ' 20:00:00' and b.start_time < @SelectedDate + ' 20:30:00' then '41'

    when b.start_time >= @SelectedDate + ' 20:30:00' and b.start_time < @SelectedDate + ' 21:00:00' then '42'

    when b.start_time >= @SelectedDate + ' 21:00:00' and b.start_time < @SelectedDate + ' 21:30:00' then '43'

    when b.start_time >= @SelectedDate + ' 21:30:00' and b.start_time < @SelectedDate + ' 22:00:00' then '44'

    when b.start_time >= @SelectedDate + ' 22:00:00' and b.start_time < @SelectedDate + ' 22:30:00' then '45'

    when b.start_time >= @SelectedDate + ' 22:30:00' and b.start_time < @SelectedDate + ' 23:00:00' then '46'

    when b.start_time >= @SelectedDate + ' 23:00:00' and b.start_time < @SelectedDate + ' 23:30:00' then '47'

    when b.start_time >= @SelectedDate + ' 23:30:00' and b.start_time <= @SelectedDate + ' 23:59:59' then '48'

    ELSE 'Other' END )as interval_id,

    b.call_flow_name, b.source, b.lang, isnull(count(b.call_flow_name),0) as hit, 'Tel - Speak to CSR' as report_item

    FROM ann_events_Tech_Details b

    WHERE(dateadd(dd,0, datediff(dd,0,b.start_time)))= @SelectedDate AND b.call_flow_name = @call_flow_name and b.sel_tel_csr =1

    GROUP BY

    (CASE when b.start_time >= @SelectedDate + ' 00:00:00' and b.start_time < @SelectedDate + ' 00:30:00' then '1'

    when b.start_time >= @SelectedDate + ' 00:30:00' and b.start_time < @SelectedDate + ' 01:00:00' then '2'

    when b.start_time >= @SelectedDate + ' 01:00:00' and b.start_time < @SelectedDate + ' 01:30:00' then '3'

    when b.start_time >= @SelectedDate + ' 01:30:00' and b.start_time < @SelectedDate + ' 02:00:00' then '4'

    when b.start_time >= @SelectedDate + ' 02:00:00' and b.start_time < @SelectedDate + ' 02:30:00' then '5'

    when b.start_time >= @SelectedDate + ' 02:30:00' and b.start_time < @SelectedDate + ' 03:00:00' then '6'

    when b.start_time >= @SelectedDate + ' 03:00:00' and b.start_time < @SelectedDate + ' 03:30:00' then '7'

    when b.start_time >= @SelectedDate + ' 03:30:00' and b.start_time < @SelectedDate + ' 04:00:00' then '8'

    when b.start_time >= @SelectedDate + ' 04:00:00' and b.start_time < @SelectedDate + ' 04:30:00' then '9'

    when b.start_time >= @SelectedDate + ' 04:30:00' and b.start_time < @SelectedDate + ' 05:00:00' then '10'

    when b.start_time >= @SelectedDate + ' 05:00:00' and b.start_time < @SelectedDate + ' 05:30:00' then '11'

    when b.start_time >= @SelectedDate + ' 05:30:00' and b.start_time < @SelectedDate + ' 06:00:00' then '12'

    when b.start_time >= @SelectedDate + ' 06:00:00' and b.start_time < @SelectedDate + ' 06:30:00' then '13'

    when b.start_time >= @SelectedDate + ' 06:30:00' and b.start_time < @SelectedDate + ' 07:00:00' then '14'

    when b.start_time >= @SelectedDate + ' 07:00:00' and b.start_time < @SelectedDate + ' 07:30:00' then '15'

    when b.start_time >= @SelectedDate + ' 07:30:00' and b.start_time < @SelectedDate + ' 08:00:00' then '16'

    when b.start_time >= @SelectedDate + ' 08:00:00' and b.start_time < @SelectedDate + ' 08:30:00' then '17'

    when b.start_time >= @SelectedDate + ' 08:30:00' and b.start_time < @SelectedDate + ' 09:00:00' then '18'

    when b.start_time >= @SelectedDate + ' 09:00:00' and b.start_time < @SelectedDate + ' 09:30:00' then '19'

    when b.start_time >= @SelectedDate + ' 09:30:00' and b.start_time < @SelectedDate + ' 10:00:00' then '20'

    when b.start_time >= @SelectedDate + ' 10:00:00' and b.start_time < @SelectedDate + ' 10:30:00' then '21'

    when b.start_time >= @SelectedDate + ' 10:30:00' and b.start_time < @SelectedDate + ' 11:00:00' then '22'

    when b.start_time >= @SelectedDate + ' 11:00:00' and b.start_time < @SelectedDate + ' 11:30:00' then '23'

    when b.start_time >= @SelectedDate + ' 11:30:00' and b.start_time < @SelectedDate + ' 12:00:00' then '24'

    when b.start_time >= @SelectedDate + ' 12:00:00' and b.start_time < @SelectedDate + ' 12:30:00' then '25'

    when b.start_time >= @SelectedDate + ' 12:30:00' and b.start_time < @SelectedDate + ' 13:00:00' then '26'

    when b.start_time >= @SelectedDate + ' 13:00:00' and b.start_time < @SelectedDate + ' 13:30:00' then '27'

    when b.start_time >= @SelectedDate + ' 13:30:00' and b.start_time < @SelectedDate + ' 14:00:00' then '28'

    when b.start_time >= @SelectedDate + ' 14:00:00' and b.start_time < @SelectedDate + ' 14:30:00' then '29'

    when b.start_time >= @SelectedDate + ' 14:30:00' and b.start_time < @SelectedDate + ' 15:00:00' then '30'

    when b.start_time >= @SelectedDate + ' 15:00:00' and b.start_time < @SelectedDate + ' 15:30:00' then '31'

    when b.start_time >= @SelectedDate + ' 15:30:00' and b.start_time < @SelectedDate + ' 16:00:00' then '32'

    when b.start_time >= @SelectedDate + ' 16:00:00' and b.start_time < @SelectedDate + ' 16:30:00' then '33'

    when b.start_time >= @SelectedDate + ' 16:30:00' and b.start_time < @SelectedDate + ' 17:00:00' then '34'

    when b.start_time >= @SelectedDate + ' 17:00:00' and b.start_time < @SelectedDate + ' 17:30:00' then '35'

    when b.start_time >= @SelectedDate + ' 17:30:00' and b.start_time < @SelectedDate + ' 18:00:00' then '36'

    when b.start_time >= @SelectedDate + ' 18:00:00' and b.start_time < @SelectedDate + ' 18:30:00' then '37'

    when b.start_time >= @SelectedDate + ' 18:30:00' and b.start_time < @SelectedDate + ' 19:00:00' then '38'

    when b.start_time >= @SelectedDate + ' 19:00:00' and b.start_time < @SelectedDate + ' 19:30:00' then '39'

    when b.start_time >= @SelectedDate + ' 19:30:00' and b.start_time < @SelectedDate + ' 20:00:00' then '40'

    when b.start_time >= @SelectedDate + ' 20:00:00' and b.start_time < @SelectedDate + ' 20:30:00' then '41'

    when b.start_time >= @SelectedDate + ' 20:30:00' and b.start_time < @SelectedDate + ' 21:00:00' then '42'

    when b.start_time >= @SelectedDate + ' 21:00:00' and b.start_time < @SelectedDate + ' 21:30:00' then '43'

    when b.start_time >= @SelectedDate + ' 21:30:00' and b.start_time < @SelectedDate + ' 22:00:00' then '44'

    when b.start_time >= @SelectedDate + ' 22:00:00' and b.start_time < @SelectedDate + ' 22:30:00' then '45'

    when b.start_time >= @SelectedDate + ' 22:30:00' and b.start_time < @SelectedDate + ' 23:00:00' then '46'

    when b.start_time >= @SelectedDate + ' 23:00:00' and b.start_time < @SelectedDate + ' 23:30:00' then '47'

    when b.start_time >= @SelectedDate + ' 23:30:00' and b.start_time <= @SelectedDate + ' 23:59:59' then '48'

    ELSE 'Other' END), b.call_flow_name, b.source, b.lang

    UNION

    SELECT

    (CASE when b.start_time >= @SelectedDate + ' 00:00:00' and b.start_time < @SelectedDate + ' 00:30:00' then '1'

    when b.start_time >= @SelectedDate + ' 00:30:00' and b.start_time < @SelectedDate + ' 01:00:00' then '2'

    when b.start_time >= @SelectedDate + ' 01:00:00' and b.start_time < @SelectedDate + ' 01:30:00' then '3'

    when b.start_time >= @SelectedDate + ' 01:30:00' and b.start_time < @SelectedDate + ' 02:00:00' then '4'

    when b.start_time >= @SelectedDate + ' 02:00:00' and b.start_time < @SelectedDate + ' 02:30:00' then '5'

    when b.start_time >= @SelectedDate + ' 02:30:00' and b.start_time < @SelectedDate + ' 03:00:00' then '6'

    when b.start_time >= @SelectedDate + ' 03:00:00' and b.start_time < @SelectedDate + ' 03:30:00' then '7'

    when b.start_time >= @SelectedDate + ' 03:30:00' and b.start_time < @SelectedDate + ' 04:00:00' then '8'

    when b.start_time >= @SelectedDate + ' 04:00:00' and b.start_time < @SelectedDate + ' 04:30:00' then '9'

    when b.start_time >= @SelectedDate + ' 04:30:00' and b.start_time < @SelectedDate + ' 05:00:00' then '10'

    when b.start_time >= @SelectedDate + ' 05:00:00' and b.start_time < @SelectedDate + ' 05:30:00' then '11'

    when b.start_time >= @SelectedDate + ' 05:30:00' and b.start_time < @SelectedDate + ' 06:00:00' then '12'

    when b.start_time >= @SelectedDate + ' 06:00:00' and b.start_time < @SelectedDate + ' 06:30:00' then '13'

    when b.start_time >= @SelectedDate + ' 06:30:00' and b.start_time < @SelectedDate + ' 07:00:00' then '14'

    when b.start_time >= @SelectedDate + ' 07:00:00' and b.start_time < @SelectedDate + ' 07:30:00' then '15'

    when b.start_time >= @SelectedDate + ' 07:30:00' and b.start_time < @SelectedDate + ' 08:00:00' then '16'

    when b.start_time >= @SelectedDate + ' 08:00:00' and b.start_time < @SelectedDate + ' 08:30:00' then '17'

    when b.start_time >= @SelectedDate + ' 08:30:00' and b.start_time < @SelectedDate + ' 09:00:00' then '18'

    when b.start_time >= @SelectedDate + ' 09:00:00' and b.start_time < @SelectedDate + ' 09:30:00' then '19'

    when b.start_time >= @SelectedDate + ' 09:30:00' and b.start_time < @SelectedDate + ' 10:00:00' then '20'

    when b.start_time >= @SelectedDate + ' 10:00:00' and b.start_time < @SelectedDate + ' 10:30:00' then '21'

    when b.start_time >= @SelectedDate + ' 10:30:00' and b.start_time < @SelectedDate + ' 11:00:00' then '22'

    when b.start_time >= @SelectedDate + ' 11:00:00' and b.start_time < @SelectedDate + ' 11:30:00' then '23'

    when b.start_time >= @SelectedDate + ' 11:30:00' and b.start_time < @SelectedDate + ' 12:00:00' then '24'

    when b.start_time >= @SelectedDate + ' 12:00:00' and b.start_time < @SelectedDate + ' 12:30:00' then '25'

    when b.start_time >= @SelectedDate + ' 12:30:00' and b.start_time < @SelectedDate + ' 13:00:00' then '26'

    when b.start_time >= @SelectedDate + ' 13:00:00' and b.start_time < @SelectedDate + ' 13:30:00' then '27'

    when b.start_time >= @SelectedDate + ' 13:30:00' and b.start_time < @SelectedDate + ' 14:00:00' then '28'

    when b.start_time >= @SelectedDate + ' 14:00:00' and b.start_time < @SelectedDate + ' 14:30:00' then '29'

    when b.start_time >= @SelectedDate + ' 14:30:00' and b.start_time < @SelectedDate + ' 15:00:00' then '30'

    when b.start_time >= @SelectedDate + ' 15:00:00' and b.start_time < @SelectedDate + ' 15:30:00' then '31'

    when b.start_time >= @SelectedDate + ' 15:30:00' and b.start_time < @SelectedDate + ' 16:00:00' then '32'

    when b.start_time >= @SelectedDate + ' 16:00:00' and b.start_time < @SelectedDate + ' 16:30:00' then '33'

    when b.start_time >= @SelectedDate + ' 16:30:00' and b.start_time < @SelectedDate + ' 17:00:00' then '34'

    when b.start_time >= @SelectedDate + ' 17:00:00' and b.start_time < @SelectedDate + ' 17:30:00' then '35'

    when b.start_time >= @SelectedDate + ' 17:30:00' and b.start_time < @SelectedDate + ' 18:00:00' then '36'

    when b.start_time >= @SelectedDate + ' 18:00:00' and b.start_time < @SelectedDate + ' 18:30:00' then '37'

    when b.start_time >= @SelectedDate + ' 18:30:00' and b.start_time < @SelectedDate + ' 19:00:00' then '38'

    when b.start_time >= @SelectedDate + ' 19:00:00' and b.start_time < @SelectedDate + ' 19:30:00' then '39'

    when b.start_time >= @SelectedDate + ' 19:30:00' and b.start_time < @SelectedDate + ' 20:00:00' then '40'

    when b.start_time >= @SelectedDate + ' 20:00:00' and b.start_time < @SelectedDate + ' 20:30:00' then '41'

    when b.start_time >= @SelectedDate + ' 20:30:00' and b.start_time < @SelectedDate + ' 21:00:00' then '42'

    when b.start_time >= @SelectedDate + ' 21:00:00' and b.start_time < @SelectedDate + ' 21:30:00' then '43'

    when b.start_time >= @SelectedDate + ' 21:30:00' and b.start_time < @SelectedDate + ' 22:00:00' then '44'

    when b.start_time >= @SelectedDate + ' 22:00:00' and b.start_time < @SelectedDate + ' 22:30:00' then '45'

    when b.start_time >= @SelectedDate + ' 22:30:00' and b.start_time < @SelectedDate + ' 23:00:00' then '46'

    when b.start_time >= @SelectedDate + ' 23:00:00' and b.start_time < @SelectedDate + ' 23:30:00' then '47'

    when b.start_time >= @SelectedDate + ' 23:30:00' and b.start_time <= @SelectedDate + ' 23:59:59' then '48'

    ELSE 'Other' END )as interval_id,

    b.call_flow_name, b.source, b.lang, isnull(count(b.call_flow_name),0) as hit, 'CKC Transfer CSR' as report_item

    FROM ann_events_Tech_Details b

    WHERE(dateadd(dd,0, datediff(dd,0,b.start_time)))= @SelectedDate AND b.call_flow_name = @call_flow_name and b.sel_tech_ckc_transfer =1

    GROUP BY

    (CASE when b.start_time >= @SelectedDate + ' 00:00:00' and b.start_time < @SelectedDate + ' 00:30:00' then '1'

    when b.start_time >= @SelectedDate + ' 00:30:00' and b.start_time < @SelectedDate + ' 01:00:00' then '2'

    when b.start_time >= @SelectedDate + ' 01:00:00' and b.start_time < @SelectedDate + ' 01:30:00' then '3'

    when b.start_time >= @SelectedDate + ' 01:30:00' and b.start_time < @SelectedDate + ' 02:00:00' then '4'

    when b.start_time >= @SelectedDate + ' 02:00:00' and b.start_time < @SelectedDate + ' 02:30:00' then '5'

    when b.start_time >= @SelectedDate + ' 02:30:00' and b.start_time < @SelectedDate + ' 03:00:00' then '6'

    when b.start_time >= @SelectedDate + ' 03:00:00' and b.start_time < @SelectedDate + ' 03:30:00' then '7'

    when b.start_time >= @SelectedDate + ' 03:30:00' and b.start_time < @SelectedDate + ' 04:00:00' then '8'

    when b.start_time >= @SelectedDate + ' 04:00:00' and b.start_time < @SelectedDate + ' 04:30:00' then '9'

    when b.start_time >= @SelectedDate + ' 04:30:00' and b.start_time < @SelectedDate + ' 05:00:00' then '10'

    when b.start_time >= @SelectedDate + ' 05:00:00' and b.start_time < @SelectedDate + ' 05:30:00' then '11'

    when b.start_time >= @SelectedDate + ' 05:30:00' and b.start_time < @SelectedDate + ' 06:00:00' then '12'

    when b.start_time >= @SelectedDate + ' 06:00:00' and b.start_time < @SelectedDate + ' 06:30:00' then '13'

    when b.start_time >= @SelectedDate + ' 06:30:00' and b.start_time < @SelectedDate + ' 07:00:00' then '14'

    when b.start_time >= @SelectedDate + ' 07:00:00' and b.start_time < @SelectedDate + ' 07:30:00' then '15'

    when b.start_time >= @SelectedDate + ' 07:30:00' and b.start_time < @SelectedDate + ' 08:00:00' then '16'

    when b.start_time >= @SelectedDate + ' 08:00:00' and b.start_time < @SelectedDate + ' 08:30:00' then '17'

    when b.start_time >= @SelectedDate + ' 08:30:00' and b.start_time < @SelectedDate + ' 09:00:00' then '18'

    when b.start_time >= @SelectedDate + ' 09:00:00' and b.start_time < @SelectedDate + ' 09:30:00' then '19'

    when b.start_time >= @SelectedDate + ' 09:30:00' and b.start_time < @SelectedDate + ' 10:00:00' then '20'

    when b.start_time >= @SelectedDate + ' 10:00:00' and b.start_time < @SelectedDate + ' 10:30:00' then '21'

    when b.start_time >= @SelectedDate + ' 10:30:00' and b.start_time < @SelectedDate + ' 11:00:00' then '22'

    when b.start_time >= @SelectedDate + ' 11:00:00' and b.start_time < @SelectedDate + ' 11:30:00' then '23'

    when b.start_time >= @SelectedDate + ' 11:30:00' and b.start_time < @SelectedDate + ' 12:00:00' then '24'

    when b.start_time >= @SelectedDate + ' 12:00:00' and b.start_time < @SelectedDate + ' 12:30:00' then '25'

    when b.start_time >= @SelectedDate + ' 12:30:00' and b.start_time < @SelectedDate + ' 13:00:00' then '26'

    when b.start_time >= @SelectedDate + ' 13:00:00' and b.start_time < @SelectedDate + ' 13:30:00' then '27'

    when b.start_time >= @SelectedDate + ' 13:30:00' and b.start_time < @SelectedDate + ' 14:00:00' then '28'

    when b.start_time >= @SelectedDate + ' 14:00:00' and b.start_time < @SelectedDate + ' 14:30:00' then '29'

    when b.start_time >= @SelectedDate + ' 14:30:00' and b.start_time < @SelectedDate + ' 15:00:00' then '30'

    when b.start_time >= @SelectedDate + ' 15:00:00' and b.start_time < @SelectedDate + ' 15:30:00' then '31'

    when b.start_time >= @SelectedDate + ' 15:30:00' and b.start_time < @SelectedDate + ' 16:00:00' then '32'

    when b.start_time >= @SelectedDate + ' 16:00:00' and b.start_time < @SelectedDate + ' 16:30:00' then '33'

    when b.start_time >= @SelectedDate + ' 16:30:00' and b.start_time < @SelectedDate + ' 17:00:00' then '34'

    when b.start_time >= @SelectedDate + ' 17:00:00' and b.start_time < @SelectedDate + ' 17:30:00' then '35'

    when b.start_time >= @SelectedDate + ' 17:30:00' and b.start_time < @SelectedDate + ' 18:00:00' then '36'

    when b.start_time >= @SelectedDate + ' 18:00:00' and b.start_time < @SelectedDate + ' 18:30:00' then '37'

    when b.start_time >= @SelectedDate + ' 18:30:00' and b.start_time < @SelectedDate + ' 19:00:00' then '38'

    when b.start_time >= @SelectedDate + ' 19:00:00' and b.start_time < @SelectedDate + ' 19:30:00' then '39'

    when b.start_time >= @SelectedDate + ' 19:30:00' and b.start_time < @SelectedDate + ' 20:00:00' then '40'

    when b.start_time >= @SelectedDate + ' 20:00:00' and b.start_time < @SelectedDate + ' 20:30:00' then '41'

    when b.start_time >= @SelectedDate + ' 20:30:00' and b.start_time < @SelectedDate + ' 21:00:00' then '42'

    when b.start_time >= @SelectedDate + ' 21:00:00' and b.start_time < @SelectedDate + ' 21:30:00' then '43'

    when b.start_time >= @SelectedDate + ' 21:30:00' and b.start_time < @SelectedDate + ' 22:00:00' then '44'

    when b.start_time >= @SelectedDate + ' 22:00:00' and b.start_time < @SelectedDate + ' 22:30:00' then '45'

    when b.start_time >= @SelectedDate + ' 22:30:00' and b.start_time < @SelectedDate + ' 23:00:00' then '46'

    when b.start_time >= @SelectedDate + ' 23:00:00' and b.start_time < @SelectedDate + ' 23:30:00' then '47'

    when b.start_time >= @SelectedDate + ' 23:30:00' and b.start_time <= @SelectedDate + ' 23:59:59' then '48'

    ELSE 'Other' END), b.call_flow_name, b.source, b.lang

    UNION

    SELECT

    (CASE when b.start_time >= @SelectedDate + ' 00:00:00' and b.start_time < @SelectedDate + ' 00:30:00' then '1'

    when b.start_time >= @SelectedDate + ' 00:30:00' and b.start_time < @SelectedDate + ' 01:00:00' then '2'

    when b.start_time >= @SelectedDate + ' 01:00:00' and b.start_time < @SelectedDate + ' 01:30:00' then '3'

    when b.start_time >= @SelectedDate + ' 01:30:00' and b.start_time < @SelectedDate + ' 02:00:00' then '4'

    when b.start_time >= @SelectedDate + ' 02:00:00' and b.start_time < @SelectedDate + ' 02:30:00' then '5'

    when b.start_time >= @SelectedDate + ' 02:30:00' and b.start_time < @SelectedDate + ' 03:00:00' then '6'

    when b.start_time >= @SelectedDate + ' 03:00:00' and b.start_time < @SelectedDate + ' 03:30:00' then '7'

    when b.start_time >= @SelectedDate + ' 03:30:00' and b.start_time < @SelectedDate + ' 04:00:00' then '8'

    when b.start_time >= @SelectedDate + ' 04:00:00' and b.start_time < @SelectedDate + ' 04:30:00' then '9'

    when b.start_time >= @SelectedDate + ' 04:30:00' and b.start_time < @SelectedDate + ' 05:00:00' then '10'

    when b.start_time >= @SelectedDate + ' 05:00:00' and b.start_time < @SelectedDate + ' 05:30:00' then '11'

    when b.start_time >= @SelectedDate + ' 05:30:00' and b.start_time < @SelectedDate + ' 06:00:00' then '12'

    when b.start_time >= @SelectedDate + ' 06:00:00' and b.start_time < @SelectedDate + ' 06:30:00' then '13'

    when b.start_time >= @SelectedDate + ' 06:30:00' and b.start_time < @SelectedDate + ' 07:00:00' then '14'

    when b.start_time >= @SelectedDate + ' 07:00:00' and b.start_time < @SelectedDate + ' 07:30:00' then '15'

    when b.start_time >= @SelectedDate + ' 07:30:00' and b.start_time < @SelectedDate + ' 08:00:00' then '16'

    when b.start_time >= @SelectedDate + ' 08:00:00' and b.start_time < @SelectedDate + ' 08:30:00' then '17'

    when b.start_time >= @SelectedDate + ' 08:30:00' and b.start_time < @SelectedDate + ' 09:00:00' then '18'

    when b.start_time >= @SelectedDate + ' 09:00:00' and b.start_time < @SelectedDate + ' 09:30:00' then '19'

    when b.start_time >= @SelectedDate + ' 09:30:00' and b.start_time < @SelectedDate + ' 10:00:00' then '20'

    when b.start_time >= @SelectedDate + ' 10:00:00' and b.start_time < @SelectedDate + ' 10:30:00' then '21'

    when b.start_time >= @SelectedDate + ' 10:30:00' and b.start_time < @SelectedDate + ' 11:00:00' then '22'

    when b.start_time >= @SelectedDate + ' 11:00:00' and b.start_time < @SelectedDate + ' 11:30:00' then '23'

    when b.start_time >= @SelectedDate + ' 11:30:00' and b.start_time < @SelectedDate + ' 12:00:00' then '24'

    when b.start_time >= @SelectedDate + ' 12:00:00' and b.start_time < @SelectedDate + ' 12:30:00' then '25'

    when b.start_time >= @SelectedDate + ' 12:30:00' and b.start_time < @SelectedDate + ' 13:00:00' then '26'

    when b.start_time >= @SelectedDate + ' 13:00:00' and b.start_time < @SelectedDate + ' 13:30:00' then '27'

    when b.start_time >= @SelectedDate + ' 13:30:00' and b.start_time < @SelectedDate + ' 14:00:00' then '28'

    when b.start_time >= @SelectedDate + ' 14:00:00' and b.start_time < @SelectedDate + ' 14:30:00' then '29'

    when b.start_time >= @SelectedDate + ' 14:30:00' and b.start_time < @SelectedDate + ' 15:00:00' then '30'

    when b.start_time >= @SelectedDate + ' 15:00:00' and b.start_time < @SelectedDate + ' 15:30:00' then '31'

    when b.start_time >= @SelectedDate + ' 15:30:00' and b.start_time < @SelectedDate + ' 16:00:00' then '32'

    when b.start_time >= @SelectedDate + ' 16:00:00' and b.start_time < @SelectedDate + ' 16:30:00' then '33'

    when b.start_time >= @SelectedDate + ' 16:30:00' and b.start_time < @SelectedDate + ' 17:00:00' then '34'

    when b.start_time >= @SelectedDate + ' 17:00:00' and b.start_time < @SelectedDate + ' 17:30:00' then '35'

    when b.start_time >= @SelectedDate + ' 17:30:00' and b.start_time < @SelectedDate + ' 18:00:00' then '36'

    when b.start_time >= @SelectedDate + ' 18:00:00' and b.start_time < @SelectedDate + ' 18:30:00' then '37'

    when b.start_time >= @SelectedDate + ' 18:30:00' and b.start_time < @SelectedDate + ' 19:00:00' then '38'

    when b.start_time >= @SelectedDate + ' 19:00:00' and b.start_time < @SelectedDate + ' 19:30:00' then '39'

    when b.start_time >= @SelectedDate + ' 19:30:00' and b.start_time < @SelectedDate + ' 20:00:00' then '40'

    when b.start_time >= @SelectedDate + ' 20:00:00' and b.start_time < @SelectedDate + ' 20:30:00' then '41'

    when b.start_time >= @SelectedDate + ' 20:30:00' and b.start_time < @SelectedDate + ' 21:00:00' then '42'

    when b.start_time >= @SelectedDate + ' 21:00:00' and b.start_time < @SelectedDate + ' 21:30:00' then '43'

    when b.start_time >= @SelectedDate + ' 21:30:00' and b.start_time < @SelectedDate + ' 22:00:00' then '44'

    when b.start_time >= @SelectedDate + ' 22:00:00' and b.start_time < @SelectedDate + ' 22:30:00' then '45'

    when b.start_time >= @SelectedDate + ' 22:30:00' and b.start_time < @SelectedDate + ' 23:00:00' then '46'

    when b.start_time >= @SelectedDate + ' 23:00:00' and b.start_time < @SelectedDate + ' 23:30:00' then '47'

    when b.start_time >= @SelectedDate + ' 23:30:00' and b.start_time <= @SelectedDate + ' 23:59:59' then '48'

    ELSE 'Other' END )as interval_id,

    b.call_flow_name, b.source, b.lang, isnull(count(b.call_flow_name),0) as hit, 'CKC End Call' as report_item

    FROM ann_events_Tech_Details b

    WHERE(dateadd(dd,0, datediff(dd,0,b.start_time)))= @SelectedDate AND b.call_flow_name = @call_flow_name and b.sel_tech_ckc_disconnect =1

    GROUP BY

    (CASE when b.start_time >= @SelectedDate + ' 00:00:00' and b.start_time < @SelectedDate + ' 00:30:00' then '1'

    when b.start_time >= @SelectedDate + ' 00:30:00' and b.start_time < @SelectedDate + ' 01:00:00' then '2'

    when b.start_time >= @SelectedDate + ' 01:00:00' and b.start_time < @SelectedDate + ' 01:30:00' then '3'

    when b.start_time >= @SelectedDate + ' 01:30:00' and b.start_time < @SelectedDate + ' 02:00:00' then '4'

    when b.start_time >= @SelectedDate + ' 02:00:00' and b.start_time < @SelectedDate + ' 02:30:00' then '5'

    when b.start_time >= @SelectedDate + ' 02:30:00' and b.start_time < @SelectedDate + ' 03:00:00' then '6'

    when b.start_time >= @SelectedDate + ' 03:00:00' and b.start_time < @SelectedDate + ' 03:30:00' then '7'

    when b.start_time >= @SelectedDate + ' 03:30:00' and b.start_time < @SelectedDate + ' 04:00:00' then '8'

    when b.start_time >= @SelectedDate + ' 04:00:00' and b.start_time < @SelectedDate + ' 04:30:00' then '9'

    when b.start_time >= @SelectedDate + ' 04:30:00' and b.start_time < @SelectedDate + ' 05:00:00' then '10'

    when b.start_time >= @SelectedDate + ' 05:00:00' and b.start_time < @SelectedDate + ' 05:30:00' then '11'

    when b.start_time >= @SelectedDate + ' 05:30:00' and b.start_time < @SelectedDate + ' 06:00:00' then '12'

    when b.start_time >= @SelectedDate + ' 06:00:00' and b.start_time < @SelectedDate + ' 06:30:00' then '13'

    when b.start_time >= @SelectedDate + ' 06:30:00' and b.start_time < @SelectedDate + ' 07:00:00' then '14'

    when b.start_time >= @SelectedDate + ' 07:00:00' and b.start_time < @SelectedDate + ' 07:30:00' then '15'

    when b.start_time >= @SelectedDate + ' 07:30:00' and b.start_time < @SelectedDate + ' 08:00:00' then '16'

    when b.start_time >= @SelectedDate + ' 08:00:00' and b.start_time < @SelectedDate + ' 08:30:00' then '17'

    when b.start_time >= @SelectedDate + ' 08:30:00' and b.start_time < @SelectedDate + ' 09:00:00' then '18'

    when b.start_time >= @SelectedDate + ' 09:00:00' and b.start_time < @SelectedDate + ' 09:30:00' then '19'

    when b.start_time >= @SelectedDate + ' 09:30:00' and b.start_time < @SelectedDate + ' 10:00:00' then '20'

    when b.start_time >= @SelectedDate + ' 10:00:00' and b.start_time < @SelectedDate + ' 10:30:00' then '21'

    when b.start_time >= @SelectedDate + ' 10:30:00' and b.start_time < @SelectedDate + ' 11:00:00' then '22'

    when b.start_time >= @SelectedDate + ' 11:00:00' and b.start_time < @SelectedDate + ' 11:30:00' then '23'

    when b.start_time >= @SelectedDate + ' 11:30:00' and b.start_time < @SelectedDate + ' 12:00:00' then '24'

    when b.start_time >= @SelectedDate + ' 12:00:00' and b.start_time < @SelectedDate + ' 12:30:00' then '25'

    when b.start_time >= @SelectedDate + ' 12:30:00' and b.start_time < @SelectedDate + ' 13:00:00' then '26'

    when b.start_time >= @SelectedDate + ' 13:00:00' and b.start_time < @SelectedDate + ' 13:30:00' then '27'

    when b.start_time >= @SelectedDate + ' 13:30:00' and b.start_time < @SelectedDate + ' 14:00:00' then '28'

    when b.start_time >= @SelectedDate + ' 14:00:00' and b.start_time < @SelectedDate + ' 14:30:00' then '29'

    when b.start_time >= @SelectedDate + ' 14:30:00' and b.start_time < @SelectedDate + ' 15:00:00' then '30'

    when b.start_time >= @SelectedDate + ' 15:00:00' and b.start_time < @SelectedDate + ' 15:30:00' then '31'

    when b.start_time >= @SelectedDate + ' 15:30:00' and b.start_time < @SelectedDate + ' 16:00:00' then '32'

    when b.start_time >= @SelectedDate + ' 16:00:00' and b.start_time < @SelectedDate + ' 16:30:00' then '33'

    when b.start_time >= @SelectedDate + ' 16:30:00' and b.start_time < @SelectedDate + ' 17:00:00' then '34'

    when b.start_time >= @SelectedDate + ' 17:00:00' and b.start_time < @SelectedDate + ' 17:30:00' then '35'

    when b.start_time >= @SelectedDate + ' 17:30:00' and b.start_time < @SelectedDate + ' 18:00:00' then '36'

    when b.start_time >= @SelectedDate + ' 18:00:00' and b.start_time < @SelectedDate + ' 18:30:00' then '37'

    when b.start_time >= @SelectedDate + ' 18:30:00' and b.start_time < @SelectedDate + ' 19:00:00' then '38'

    when b.start_time >= @SelectedDate + ' 19:00:00' and b.start_time < @SelectedDate + ' 19:30:00' then '39'

    when b.start_time >= @SelectedDate + ' 19:30:00' and b.start_time < @SelectedDate + ' 20:00:00' then '40'

    when b.start_time >= @SelectedDate + ' 20:00:00' and b.start_time < @SelectedDate + ' 20:30:00' then '41'

    when b.start_time >= @SelectedDate + ' 20:30:00' and b.start_time < @SelectedDate + ' 21:00:00' then '42'

    when b.start_time >= @SelectedDate + ' 21:00:00' and b.start_time < @SelectedDate + ' 21:30:00' then '43'

    when b.start_time >= @SelectedDate + ' 21:30:00' and b.start_time < @SelectedDate + ' 22:00:00' then '44'

    when b.start_time >= @SelectedDate + ' 22:00:00' and b.start_time < @SelectedDate + ' 22:30:00' then '45'

    when b.start_time >= @SelectedDate + ' 22:30:00' and b.start_time < @SelectedDate + ' 23:00:00' then '46'

    when b.start_time >= @SelectedDate + ' 23:00:00' and b.start_time < @SelectedDate + ' 23:30:00' then '47'

    when b.start_time >= @SelectedDate + ' 23:30:00' and b.start_time <= @SelectedDate + ' 23:59:59' then '48'

    ELSE 'Other' END), b.call_flow_name, b.source, b.lang

    UNION

    SELECT

    (CASE when b.start_time >= @SelectedDate + ' 00:00:00' and b.start_time < @SelectedDate + ' 00:30:00' then '1'

    when b.start_time >= @SelectedDate + ' 00:30:00' and b.start_time < @SelectedDate + ' 01:00:00' then '2'

    when b.start_time >= @SelectedDate + ' 01:00:00' and b.start_time < @SelectedDate + ' 01:30:00' then '3'

    when b.start_time >= @SelectedDate + ' 01:30:00' and b.start_time < @SelectedDate + ' 02:00:00' then '4'

    when b.start_time >= @SelectedDate + ' 02:00:00' and b.start_time < @SelectedDate + ' 02:30:00' then '5'

    when b.start_time >= @SelectedDate + ' 02:30:00' and b.start_time < @SelectedDate + ' 03:00:00' then '6'

    when b.start_time >= @SelectedDate + ' 03:00:00' and b.start_time < @SelectedDate + ' 03:30:00' then '7'

    when b.start_time >= @SelectedDate + ' 03:30:00' and b.start_time < @SelectedDate + ' 04:00:00' then '8'

    when b.start_time >= @SelectedDate + ' 04:00:00' and b.start_time < @SelectedDate + ' 04:30:00' then '9'

    when b.start_time >= @SelectedDate + ' 04:30:00' and b.start_time < @SelectedDate + ' 05:00:00' then '10'

    when b.start_time >= @SelectedDate + ' 05:00:00' and b.start_time < @SelectedDate + ' 05:30:00' then '11'

    when b.start_time >= @SelectedDate + ' 05:30:00' and b.start_time < @SelectedDate + ' 06:00:00' then '12'

    when b.start_time >= @SelectedDate + ' 06:00:00' and b.start_time < @SelectedDate + ' 06:30:00' then '13'

    when b.start_time >= @SelectedDate + ' 06:30:00' and b.start_time < @SelectedDate + ' 07:00:00' then '14'

    when b.start_time >= @SelectedDate + ' 07:00:00' and b.start_time < @SelectedDate + ' 07:30:00' then '15'

    when b.start_time >= @SelectedDate + ' 07:30:00' and b.start_time < @SelectedDate + ' 08:00:00' then '16'

    when b.start_time >= @SelectedDate + ' 08:00:00' and b.start_time < @SelectedDate + ' 08:30:00' then '17'

    when b.start_time >= @SelectedDate + ' 08:30:00' and b.start_time < @SelectedDate + ' 09:00:00' then '18'

    when b.start_time >= @SelectedDate + ' 09:00:00' and b.start_time < @SelectedDate + ' 09:30:00' then '19'

    when b.start_time >= @SelectedDate + ' 09:30:00' and b.start_time < @SelectedDate + ' 10:00:00' then '20'

    when b.start_time >= @SelectedDate + ' 10:00:00' and b.start_time < @SelectedDate + ' 10:30:00' then '21'

    when b.start_time >= @SelectedDate + ' 10:30:00' and b.start_time < @SelectedDate + ' 11:00:00' then '22'

    when b.start_time >= @SelectedDate + ' 11:00:00' and b.start_time < @SelectedDate + ' 11:30:00' then '23'

    when b.start_time >= @SelectedDate + ' 11:30:00' and b.start_time < @SelectedDate + ' 12:00:00' then '24'

    when b.start_time >= @SelectedDate + ' 12:00:00' and b.start_time < @SelectedDate + ' 12:30:00' then '25'

    when b.start_time >= @SelectedDate + ' 12:30:00' and b.start_time < @SelectedDate + ' 13:00:00' then '26'

    when b.start_time >= @SelectedDate + ' 13:00:00' and b.start_time < @SelectedDate + ' 13:30:00' then '27'

    when b.start_time >= @SelectedDate + ' 13:30:00' and b.start_time < @SelectedDate + ' 14:00:00' then '28'

    when b.start_time >= @SelectedDate + ' 14:00:00' and b.start_time < @SelectedDate + ' 14:30:00' then '29'

    when b.start_time >= @SelectedDate + ' 14:30:00' and b.start_time < @SelectedDate + ' 15:00:00' then '30'

    when b.start_time >= @SelectedDate + ' 15:00:00' and b.start_time < @SelectedDate + ' 15:30:00' then '31'

    when b.start_time >= @SelectedDate + ' 15:30:00' and b.start_time < @SelectedDate + ' 16:00:00' then '32'

    when b.start_time >= @SelectedDate + ' 16:00:00' and b.start_time < @SelectedDate + ' 16:30:00' then '33'

    when b.start_time >= @SelectedDate + ' 16:30:00' and b.start_time < @SelectedDate + ' 17:00:00' then '34'

    when b.start_time >= @SelectedDate + ' 17:00:00' and b.start_time < @SelectedDate + ' 17:30:00' then '35'

    when b.start_time >= @SelectedDate + ' 17:30:00' and b.start_time < @SelectedDate + ' 18:00:00' then '36'

    when b.start_time >= @SelectedDate + ' 18:00:00' and b.start_time < @SelectedDate + ' 18:30:00' then '37'

    when b.start_time >= @SelectedDate + ' 18:30:00' and b.start_time < @SelectedDate + ' 19:00:00' then '38'

    when b.start_time >= @SelectedDate + ' 19:00:00' and b.start_time < @SelectedDate + ' 19:30:00' then '39'

    when b.start_time >= @SelectedDate + ' 19:30:00' and b.start_time < @SelectedDate + ' 20:00:00' then '40'

    when b.start_time >= @SelectedDate + ' 20:00:00' and b.start_time < @SelectedDate + ' 20:30:00' then '41'

    when b.start_time >= @SelectedDate + ' 20:30:00' and b.start_time < @SelectedDate + ' 21:00:00' then '42'

    when b.start_time >= @SelectedDate + ' 21:00:00' and b.start_time < @SelectedDate + ' 21:30:00' then '43'

    when b.start_time >= @SelectedDate + ' 21:30:00' and b.start_time < @SelectedDate + ' 22:00:00' then '44'

    when b.start_time >= @SelectedDate + ' 22:00:00' and b.start_time < @SelectedDate + ' 22:30:00' then '45'

    when b.start_time >= @SelectedDate + ' 22:30:00' and b.start_time < @SelectedDate + ' 23:00:00' then '46'

    when b.start_time >= @SelectedDate + ' 23:00:00' and b.start_time < @SelectedDate + ' 23:30:00' then '47'

    when b.start_time >= @SelectedDate + ' 23:30:00' and b.start_time <= @SelectedDate + ' 23:59:59' then '48'

    ELSE 'Other' END )as interval_id,

    b.call_flow_name, b.source, b.lang, isnull(count(b.call_flow_name),0) as hit, 'Inv Num - DEL' as report_item

    FROM ann_events_Tech_Details b

    WHERE(dateadd(dd,0, datediff(dd,0,b.start_time)))= @SelectedDate AND b.call_flow_name = @call_flow_name and b.sel_inv_del =1

    GROUP BY

    (CASE when b.start_time >= @SelectedDate + ' 00:00:00' and b.start_time < @SelectedDate + ' 00:30:00' then '1'

    when b.start_time >= @SelectedDate + ' 00:30:00' and b.start_time < @SelectedDate + ' 01:00:00' then '2'

    when b.start_time >= @SelectedDate + ' 01:00:00' and b.start_time < @SelectedDate + ' 01:30:00' then '3'

    when b.start_time >= @SelectedDate + ' 01:30:00' and b.start_time < @SelectedDate + ' 02:00:00' then '4'

    when b.start_time >= @SelectedDate + ' 02:00:00' and b.start_time < @SelectedDate + ' 02:30:00' then '5'

    when b.start_time >= @SelectedDate + ' 02:30:00' and b.start_time < @SelectedDate + ' 03:00:00' then '6'

    when b.start_time >= @SelectedDate + ' 03:00:00' and b.start_time < @SelectedDate + ' 03:30:00' then '7'

    when b.start_time >= @SelectedDate + ' 03:30:00' and b.start_time < @SelectedDate + ' 04:00:00' then '8'

    when b.start_time >= @SelectedDate + ' 04:00:00' and b.start_time < @SelectedDate + ' 04:30:00' then '9'

    when b.start_time >= @SelectedDate + ' 04:30:00' and b.start_time < @SelectedDate + ' 05:00:00' then '10'

    when b.start_time >= @SelectedDate + ' 05:00:00' and b.start_time < @SelectedDate + ' 05:30:00' then '11'

    when b.start_time >= @SelectedDate + ' 05:30:00' and b.start_time < @SelectedDate + ' 06:00:00' then '12'

    when b.start_time >= @SelectedDate + ' 06:00:00' and b.start_time < @SelectedDate + ' 06:30:00' then '13'

    when b.start_time >= @SelectedDate + ' 06:30:00' and b.start_time < @SelectedDate + ' 07:00:00' then '14'

    when b.start_time >= @SelectedDate + ' 07:00:00' and b.start_time < @SelectedDate + ' 07:30:00' then '15'

    when b.start_time >= @SelectedDate + ' 07:30:00' and b.start_time < @SelectedDate + ' 08:00:00' then '16'

    when b.start_time >= @SelectedDate + ' 08:00:00' and b.start_time < @SelectedDate + ' 08:30:00' then '17'

    when b.start_time >= @SelectedDate + ' 08:30:00' and b.start_time < @SelectedDate + ' 09:00:00' then '18'

    when b.start_time >= @SelectedDate + ' 09:00:00' and b.start_time < @SelectedDate + ' 09:30:00' then '19'

    when b.start_time >= @SelectedDate + ' 09:30:00' and b.start_time < @SelectedDate + ' 10:00:00' then '20'

    when b.start_time >= @SelectedDate + ' 10:00:00' and b.start_time < @SelectedDate + ' 10:30:00' then '21'

    when b.start_time >= @SelectedDate + ' 10:30:00' and b.start_time < @SelectedDate + ' 11:00:00' then '22'

    when b.start_time >= @SelectedDate + ' 11:00:00' and b.start_time < @SelectedDate + ' 11:30:00' then '23'

    when b.start_time >= @SelectedDate + ' 11:30:00' and b.start_time < @SelectedDate + ' 12:00:00' then '24'

    when b.start_time >= @SelectedDate + ' 12:00:00' and b.start_time < @SelectedDate + ' 12:30:00' then '25'

    when b.start_time >= @SelectedDate + ' 12:30:00' and b.start_time < @SelectedDate + ' 13:00:00' then '26'

    when b.start_time >= @SelectedDate + ' 13:00:00' and b.start_time < @SelectedDate + ' 13:30:00' then '27'

    when b.start_time >= @SelectedDate + ' 13:30:00' and b.start_time < @SelectedDate + ' 14:00:00' then '28'

    when b.start_time >= @SelectedDate + ' 14:00:00' and b.start_time < @SelectedDate + ' 14:30:00' then '29'

    when b.start_time >= @SelectedDate + ' 14:30:00' and b.start_time < @SelectedDate + ' 15:00:00' then '30'

    when b.start_time >= @SelectedDate + ' 15:00:00' and b.start_time < @SelectedDate + ' 15:30:00' then '31'

    when b.start_time >= @SelectedDate + ' 15:30:00' and b.start_time < @SelectedDate + ' 16:00:00' then '32'

    when b.start_time >= @SelectedDate + ' 16:00:00' and b.start_time < @SelectedDate + ' 16:30:00' then '33'

    when b.start_time >= @SelectedDate + ' 16:30:00' and b.start_time < @SelectedDate + ' 17:00:00' then '34'

    when b.start_time >= @SelectedDate + ' 17:00:00' and b.start_time < @SelectedDate + ' 17:30:00' then '35'

    when b.start_time >= @SelectedDate + ' 17:30:00' and b.start_time < @SelectedDate + ' 18:00:00' then '36'

    when b.start_time >= @SelectedDate + ' 18:00:00' and b.start_time < @SelectedDate + ' 18:30:00' then '37'

    when b.start_time >= @SelectedDate + ' 18:30:00' and b.start_time < @SelectedDate + ' 19:00:00' then '38'

    when b.start_time >= @SelectedDate + ' 19:00:00' and b.start_time < @SelectedDate + ' 19:30:00' then '39'

    when b.start_time >= @SelectedDate + ' 19:30:00' and b.start_time < @SelectedDate + ' 20:00:00' then '40'

    when b.start_time >= @SelectedDate + ' 20:00:00' and b.start_time < @SelectedDate + ' 20:30:00' then '41'

    when b.start_time >= @SelectedDate + ' 20:30:00' and b.start_time < @SelectedDate + ' 21:00:00' then '42'

    when b.start_time >= @SelectedDate + ' 21:00:00' and b.start_time < @SelectedDate + ' 21:30:00' then '43'

    when b.start_time >= @SelectedDate + ' 21:30:00' and b.start_time < @SelectedDate + ' 22:00:00' then '44'

    when b.start_time >= @SelectedDate + ' 22:00:00' and b.start_time < @SelectedDate + ' 22:30:00' then '45'

    when b.start_time >= @SelectedDate + ' 22:30:00' and b.start_time < @SelectedDate + ' 23:00:00' then '46'

    when b.start_time >= @SelectedDate + ' 23:00:00' and b.start_time < @SelectedDate + ' 23:30:00' then '47'

    when b.start_time >= @SelectedDate + ' 23:30:00' and b.start_time <= @SelectedDate + ' 23:59:59' then '48'

    ELSE 'Other' END), b.call_flow_name, b.source, b.lang

    UNION

    SELECT

    (CASE when b.start_time >= @SelectedDate + ' 00:00:00' and b.start_time < @SelectedDate + ' 00:30:00' then '1'

    when b.start_time >= @SelectedDate + ' 00:30:00' and b.start_time < @SelectedDate + ' 01:00:00' then '2'

    when b.start_time >= @SelectedDate + ' 01:00:00' and b.start_time < @SelectedDate + ' 01:30:00' then '3'

    when b.start_time >= @SelectedDate + ' 01:30:00' and b.start_time < @SelectedDate + ' 02:00:00' then '4'

    when b.start_time >= @SelectedDate + ' 02:00:00' and b.start_time < @SelectedDate + ' 02:30:00' then '5'

    when b.start_time >= @SelectedDate + ' 02:30:00' and b.start_time < @SelectedDate + ' 03:00:00' then '6'

    when b.start_time >= @SelectedDate + ' 03:00:00' and b.start_time < @SelectedDate + ' 03:30:00' then '7'

    when b.start_time >= @SelectedDate + ' 03:30:00' and b.start_time < @SelectedDate + ' 04:00:00' then '8'

    when b.start_time >= @SelectedDate + ' 04:00:00' and b.start_time < @SelectedDate + ' 04:30:00' then '9'

    when b.start_time >= @SelectedDate + ' 04:30:00' and b.start_time < @SelectedDate + ' 05:00:00' then '10'

    when b.start_time >= @SelectedDate + ' 05:00:00' and b.start_time < @SelectedDate + ' 05:30:00' then '11'

    when b.start_time >= @SelectedDate + ' 05:30:00' and b.start_time < @SelectedDate + ' 06:00:00' then '12'

    when b.start_time >= @SelectedDate + ' 06:00:00' and b.start_time < @SelectedDate + ' 06:30:00' then '13'

    when b.start_time >= @SelectedDate + ' 06:30:00' and b.start_time < @SelectedDate + ' 07:00:00' then '14'

    when b.start_time >= @SelectedDate + ' 07:00:00' and b.start_time < @SelectedDate + ' 07:30:00' then '15'

    when b.start_time >= @SelectedDate + ' 07:30:00' and b.start_time < @SelectedDate + ' 08:00:00' then '16'

    when b.start_time >= @SelectedDate + ' 08:00:00' and b.start_time < @SelectedDate + ' 08:30:00' then '17'

    when b.start_time >= @SelectedDate + ' 08:30:00' and b.start_time < @SelectedDate + ' 09:00:00' then '18'

    when b.start_time >= @SelectedDate + ' 09:00:00' and b.start_time < @SelectedDate + ' 09:30:00' then '19'

    when b.start_time >= @SelectedDate + ' 09:30:00' and b.start_time < @SelectedDate + ' 10:00:00' then '20'

    when b.start_time >= @SelectedDate + ' 10:00:00' and b.start_time < @SelectedDate + ' 10:30:00' then '21'

    when b.start_time >= @SelectedDate + ' 10:30:00' and b.start_time < @SelectedDate + ' 11:00:00' then '22'

    when b.start_time >= @SelectedDate + ' 11:00:00' and b.start_time < @SelectedDate + ' 11:30:00' then '23'

    when b.start_time >= @SelectedDate + ' 11:30:00' and b.start_time < @SelectedDate + ' 12:00:00' then '24'

    when b.start_time >= @SelectedDate + ' 12:00:00' and b.start_time < @SelectedDate + ' 12:30:00' then '25'

    when b.start_time >= @SelectedDate + ' 12:30:00' and b.start_time < @SelectedDate + ' 13:00:00' then '26'

    when b.start_time >= @SelectedDate + ' 13:00:00' and b.start_time < @SelectedDate + ' 13:30:00' then '27'

    when b.start_time >= @SelectedDate + ' 13:30:00' and b.start_time < @SelectedDate + ' 14:00:00' then '28'

    when b.start_time >= @SelectedDate + ' 14:00:00' and b.start_time < @SelectedDate + ' 14:30:00' then '29'

    when b.start_time >= @SelectedDate + ' 14:30:00' and b.start_time < @SelectedDate + ' 15:00:00' then '30'

    when b.start_time >= @SelectedDate + ' 15:00:00' and b.start_time < @SelectedDate + ' 15:30:00' then '31'

    when b.start_time >= @SelectedDate + ' 15:30:00' and b.start_time < @SelectedDate + ' 16:00:00' then '32'

    when b.start_time >= @SelectedDate + ' 16:00:00' and b.start_time < @SelectedDate + ' 16:30:00' then '33'

    when b.start_time >= @SelectedDate + ' 16:30:00' and b.start_time < @SelectedDate + ' 17:00:00' then '34'

    when b.start_time >= @SelectedDate + ' 17:00:00' and b.start_time < @SelectedDate + ' 17:30:00' then '35'

    when b.start_time >= @SelectedDate + ' 17:30:00' and b.start_time < @SelectedDate + ' 18:00:00' then '36'

    when b.start_time >= @SelectedDate + ' 18:00:00' and b.start_time < @SelectedDate + ' 18:30:00' then '37'

    when b.start_time >= @SelectedDate + ' 18:30:00' and b.start_time < @SelectedDate + ' 19:00:00' then '38'

    when b.start_time >= @SelectedDate + ' 19:00:00' and b.start_time < @SelectedDate + ' 19:30:00' then '39'

    when b.start_time >= @SelectedDate + ' 19:30:00' and b.start_time < @SelectedDate + ' 20:00:00' then '40'

    when b.start_time >= @SelectedDate + ' 20:00:00' and b.start_time < @SelectedDate + ' 20:30:00' then '41'

    when b.start_time >= @SelectedDate + ' 20:30:00' and b.start_time < @SelectedDate + ' 21:00:00' then '42'

    when b.start_time >= @SelectedDate + ' 21:00:00' and b.start_time < @SelectedDate + ' 21:30:00' then '43'

    when b.start_time >= @SelectedDate + ' 21:30:00' and b.start_time < @SelectedDate + ' 22:00:00' then '44'

    when b.start_time >= @SelectedDate + ' 22:00:00' and b.start_time < @SelectedDate + ' 22:30:00' then '45'

    when b.start_time >= @SelectedDate + ' 22:30:00' and b.start_time < @SelectedDate + ' 23:00:00' then '46'

    when b.start_time >= @SelectedDate + ' 23:00:00' and b.start_time < @SelectedDate + ' 23:30:00' then '47'

    when b.start_time >= @SelectedDate + ' 23:30:00' and b.start_time <= @SelectedDate + ' 23:59:59' then '48'

    ELSE 'Other' END )as interval_id,

    b.call_flow_name, b.source, b.lang, isnull(count(b.call_flow_name),0) as hit, 'Inv Num - Unifi' as report_item

    FROM ann_events_Tech_Details b

    WHERE(dateadd(dd,0, datediff(dd,0,b.start_time)))= @SelectedDate AND b.call_flow_name = @call_flow_name and b.sel_inv_unifi =1

    GROUP BY

    (CASE when b.start_time >= @SelectedDate + ' 00:00:00' and b.start_time < @SelectedDate + ' 00:30:00' then '1'

    when b.start_time >= @SelectedDate + ' 00:30:00' and b.start_time < @SelectedDate + ' 01:00:00' then '2'

    when b.start_time >= @SelectedDate + ' 01:00:00' and b.start_time < @SelectedDate + ' 01:30:00' then '3'

    when b.start_time >= @SelectedDate + ' 01:30:00' and b.start_time < @SelectedDate + ' 02:00:00' then '4'

    when b.start_time >= @SelectedDate + ' 02:00:00' and b.start_time < @SelectedDate + ' 02:30:00' then '5'

    when b.start_time >= @SelectedDate + ' 02:30:00' and b.start_time < @SelectedDate + ' 03:00:00' then '6'

    when b.start_time >= @SelectedDate + ' 03:00:00' and b.start_time < @SelectedDate + ' 03:30:00' then '7'

    when b.start_time >= @SelectedDate + ' 03:30:00' and b.start_time < @SelectedDate + ' 04:00:00' then '8'

    when b.start_time >= @SelectedDate + ' 04:00:00' and b.start_time < @SelectedDate + ' 04:30:00' then '9'

    when b.start_time >= @SelectedDate + ' 04:30:00' and b.start_time < @SelectedDate + ' 05:00:00' then '10'

    when b.start_time >= @SelectedDate + ' 05:00:00' and b.start_time < @SelectedDate + ' 05:30:00' then '11'

    when b.start_time >= @SelectedDate + ' 05:30:00' and b.start_time < @SelectedDate + ' 06:00:00' then '12'

    when b.start_time >= @SelectedDate + ' 06:00:00' and b.start_time < @SelectedDate + ' 06:30:00' then '13'

    when b.start_time >= @SelectedDate + ' 06:30:00' and b.start_time < @SelectedDate + ' 07:00:00' then '14'

    when b.start_time >= @SelectedDate + ' 07:00:00' and b.start_time < @SelectedDate + ' 07:30:00' then '15'

    when b.start_time >= @SelectedDate + ' 07:30:00' and b.start_time < @SelectedDate + ' 08:00:00' then '16'

    when b.start_time >= @SelectedDate + ' 08:00:00' and b.start_time < @SelectedDate + ' 08:30:00' then '17'

    when b.start_time >= @SelectedDate + ' 08:30:00' and b.start_time < @SelectedDate + ' 09:00:00' then '18'

    when b.start_time >= @SelectedDate + ' 09:00:00' and b.start_time < @SelectedDate + ' 09:30:00' then '19'

    when b.start_time >= @SelectedDate + ' 09:30:00' and b.start_time < @SelectedDate + ' 10:00:00' then '20'

    when b.start_time >= @SelectedDate + ' 10:00:00' and b.start_time < @SelectedDate + ' 10:30:00' then '21'

    when b.start_time >= @SelectedDate + ' 10:30:00' and b.start_time < @SelectedDate + ' 11:00:00' then '22'

    when b.start_time >= @SelectedDate + ' 11:00:00' and b.start_time < @SelectedDate + ' 11:30:00' then '23'

    when b.start_time >= @SelectedDate + ' 11:30:00' and b.start_time < @SelectedDate + ' 12:00:00' then '24'

    when b.start_time >= @SelectedDate + ' 12:00:00' and b.start_time < @SelectedDate + ' 12:30:00' then '25'

    when b.start_time >= @SelectedDate + ' 12:30:00' and b.start_time < @SelectedDate + ' 13:00:00' then '26'

    when b.start_time >= @SelectedDate + ' 13:00:00' and b.start_time < @SelectedDate + ' 13:30:00' then '27'

    when b.start_time >= @SelectedDate + ' 13:30:00' and b.start_time < @SelectedDate + ' 14:00:00' then '28'

    when b.start_time >= @SelectedDate + ' 14:00:00' and b.start_time < @SelectedDate + ' 14:30:00' then '29'

    when b.start_time >= @SelectedDate + ' 14:30:00' and b.start_time < @SelectedDate + ' 15:00:00' then '30'

    when b.start_time >= @SelectedDate + ' 15:00:00' and b.start_time < @SelectedDate + ' 15:30:00' then '31'

    when b.start_time >= @SelectedDate + ' 15:30:00' and b.start_time < @SelectedDate + ' 16:00:00' then '32'

    when b.start_time >= @SelectedDate + ' 16:00:00' and b.start_time < @SelectedDate + ' 16:30:00' then '33'

    when b.start_time >= @SelectedDate + ' 16:30:00' and b.start_time < @SelectedDate + ' 17:00:00' then '34'

    when b.start_time >= @SelectedDate + ' 17:00:00' and b.start_time < @SelectedDate + ' 17:30:00' then '35'

    when b.start_time >= @SelectedDate + ' 17:30:00' and b.start_time < @SelectedDate + ' 18:00:00' then '36'

    when b.start_time >= @SelectedDate + ' 18:00:00' and b.start_time < @SelectedDate + ' 18:30:00' then '37'

    when b.start_time >= @SelectedDate + ' 18:30:00' and b.start_time < @SelectedDate + ' 19:00:00' then '38'

    when b.start_time >= @SelectedDate + ' 19:00:00' and b.start_time < @SelectedDate + ' 19:30:00' then '39'

    when b.start_time >= @SelectedDate + ' 19:30:00' and b.start_time < @SelectedDate + ' 20:00:00' then '40'

    when b.start_time >= @SelectedDate + ' 20:00:00' and b.start_time < @SelectedDate + ' 20:30:00' then '41'

    when b.start_time >= @SelectedDate + ' 20:30:00' and b.start_time < @SelectedDate + ' 21:00:00' then '42'

    when b.start_time >= @SelectedDate + ' 21:00:00' and b.start_time < @SelectedDate + ' 21:30:00' then '43'

    when b.start_time >= @SelectedDate + ' 21:30:00' and b.start_time < @SelectedDate + ' 22:00:00' then '44'

    when b.start_time >= @SelectedDate + ' 22:00:00' and b.start_time < @SelectedDate + ' 22:30:00' then '45'

    when b.start_time >= @SelectedDate + ' 22:30:00' and b.start_time < @SelectedDate + ' 23:00:00' then '46'

    when b.start_time >= @SelectedDate + ' 23:00:00' and b.start_time < @SelectedDate + ' 23:30:00' then '47'

    when b.start_time >= @SelectedDate + ' 23:30:00' and b.start_time <= @SelectedDate + ' 23:59:59' then '48'

    ELSE 'Other' END), b.call_flow_name, b.source, b.lang

  • Sergiy (6/1/2016)


    Now you're gonna hit your head again asking "why did not i think of it?".

    It's so easy. 🙂

    You group you'd events by day:

    dateadd(dd,0, datediff(dd,0,b.start_time)) as [date]

    But what you need is to group by 30 min time slots.

    So, if that's what you need - just do it!

    dateadd(n,0, datediff(n,0,b.start_time)/30*30) as [time slot]

    Then, when presenting the result you may split [time slot] into date and time parts - it's up to consumers of the report how do they wanna see it.

    No need to touch WHERE clause at all.

    I'm afraid I still don't understand the concept. 🙁

    dateadd(n,0, datediff(n,0,b.start_time)/30*30) as [time slot]

    Will return error - Arithmetic overflow error converting expression to data type datetime.

    Assuming [start_time] is '2016-01-01 00:00:00.000', then it will be dateadd(n,0, 61041600/30*30) and exceeding year 9999 thus causing Arithmetic overflow error.

    Or am I getting the whole idea wrong?

  • jc85 (6/1/2016)


    Sergiy (6/1/2016)


    Now you're gonna hit your head again asking "why did not i think of it?".

    It's so easy. 🙂

    You group you'd events by day:

    dateadd(dd,0, datediff(dd,0,b.start_time)) as [date]

    But what you need is to group by 30 min time slots.

    So, if that's what you need - just do it!

    dateadd(n,0, datediff(n,0,b.start_time)/30*30) as [time slot]

    Then, when presenting the result you may split [time slot] into date and time parts - it's up to consumers of the report how do they wanna see it.

    No need to touch WHERE clause at all.

    I'm afraid I still don't understand the concept. 🙁

    dateadd(n,0, datediff(n,0,b.start_time)/30*30) as [time slot]

    Will return error - Arithmetic overflow error converting expression to data type datetime.

    Assuming [start_time] is '2016-01-01 00:00:00.000', then it will be dateadd(n,0, 61041600/30*30) and exceeding year 9999 thus causing Arithmetic overflow error.

    Or am I getting the whole idea wrong?

    That's because Sergiy switched the order of the second and third parameters for the DATEADD function in both of his examples. They should be

    dateadd(dd, datediff(dd,0,b.start_time), 0) as [date]

    dateadd(n, datediff(n,0,b.start_time)/30*30, 0) as [time slot]

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • drew.allen (6/1/2016)


    That's because Sergiy switched the order of the second and third parameters for the DATEADD function in both of his examples. They should be

    dateadd(dd, datediff(dd,0,b.start_time), 0) as [date]

    dateadd(n, datediff(n,0,b.start_time)/30*30, 0) as [time slot]

    Drew

    Thanks.

  • SELECT CONVERT(DATE, DT.[time slot]) date,

    CONVERT(VARCHAR(20),DT.[time slot], 108) + '-' + CONVERT(VARCHAR(20),DATEADD(n, 30, DT.[time slot]), 108)

    FROM (SELECT dateadd(n, datediff(n,0, GETDATE())/30*30, 0) as [time slot]

    ) DT

    Is it what you're after?

    _____________
    Code for TallyGenerator

  • Sergiy (6/1/2016)


    SELECT CONVERT(DATE, DT.[time slot]) date,

    CONVERT(VARCHAR(20),DT.[time slot], 108) + '-' + CONVERT(VARCHAR(20),DATEADD(n, 30, DT.[time slot]), 108)

    FROM (SELECT dateadd(n, datediff(n,0, GETDATE())/30*30, 0) as [time slot]

    ) DT

    Is it what you're after?

    Exactly, thanks !

    I noticed if data from specific [source] is empty, then it will not appear in the result rather than showing 0. PFCC data on 2016-06-02 is missing from the table below, is there a way to force it to show as 0?

    date--------------report_item---------------ENBMMD

    2016-06-01 CYGAL-CKC End Call---------050

    2016-06-01 PFCC-CKC End Call----------000

    2016-06-01 CYGAL-CKC Transfer CSR----1612015

    2016-06-01 PFCC-CKC Transfer CSR------000

    2016-06-01 CYGAL-Inv Num - DEL-------3761638424

    2016-06-01 PFCC-Inv Num - DEL---------000

    2016-06-01 CYGAL-Inv Num - Unifi-------558931270

    2016-06-01 PFCC-Inv Num - Unifi--------100

    2016-06-01 CYGAL-No Dial Tone---------2141020326

    2016-06-01 PFCC-No Dial Tone----------000

    2016-06-01 CYGAL-Noisy---------------2318168

    2016-06-01 PFCC-Noisy-----------------000

    2016-06-01 CYGAL-Tel - Speak to CSR---5042944391

    2016-06-01 PFCC-Tel - Speak to CSR----000

    2016-06-02 CYGAL-CKC End Call---------030

    2016-06-02 CYGAL-CKC Transfer CSR----248713

    2016-06-02 CYGAL-Inv Num - DEL-------182994237

    2016-06-02 CYGAL-Inv Num - Unifi-------239404117

    2016-06-02 CYGAL-No Dial Tone---------130767210

    2016-06-02 CYGAL-Noisy----------------1314455

    2016-06-02 CYGAL-Tel - Speak to CSR---3601977230

    SELECT DT.date, (a.source + '-' + a.report_item) as report_item,

    SUM(CASE WHEN DT.lang = 'EN' THEN CASE a.report_item

    WHEN 'No Dial Tone' THEN NoDialTone

    WHEN 'Noisy' THEN Noisy

    WHEN 'CKC End Call' THEN CKCEndCall

    WHEN 'CKC Transfer CSR' THEN CKCTransferCSR

    WHEN 'Tel - Speak to CSR' THEN TelSpeakToCSR

    WHEN 'Inv Num - Del' THEN InvNumDel

    WHEN 'Inv Num - Unifi' THEN InvNumUnifi

    ELSE 0 END ELSE 0 END) EN,

    SUM(CASE WHEN DT.lang = 'BM' THEN CASE a.report_item

    WHEN 'No Dial Tone' THEN NoDialTone

    WHEN 'Noisy' THEN Noisy

    WHEN 'CKC End Call' THEN CKCEndCall

    WHEN 'CKC Transfer CSR' THEN CKCTransferCSR

    WHEN 'Tel - Speak to CSR' THEN TelSpeakToCSR

    WHEN 'Inv Num - Del' THEN InvNumDel

    WHEN 'Inv Num - Unifi' THEN InvNumUnifi

    ELSE 0 END ELSE 0 END) BM,

    SUM(CASE WHEN DT.lang = 'MD' THEN CASE a.report_item

    WHEN 'No Dial Tone' THEN NoDialTone

    WHEN 'Noisy' THEN Noisy

    WHEN 'CKC End Call' THEN CKCEndCall

    WHEN 'CKC Transfer CSR' THEN CKCTransferCSR

    WHEN 'Tel - Speak to CSR' THEN TelSpeakToCSR

    WHEN 'Inv Num - Del' THEN InvNumDel

    WHEN 'Inv Num - Unifi' THEN InvNumUnifi

    ELSE 0 END ELSE 0 END) MD

    FROM ann_ReportItem a

    INNER JOIN (

    SELECT dateadd(dd,0, datediff(dd,0,b.start_time)) as [date], b.source, b.lang,

    SUM(CONVERT(INT, b.sel_tel_nodialtone)) NoDialTone,

    SUM(CONVERT(INT, b.sel_tel_noisy)) Noisy,

    SUM(CONVERT(INT, b.sel_tech_ckc_disconnect)) CKCEndCall,

    SUM(CONVERT(INT, b.sel_tech_ckc_transfer)) CKCTransferCSR,

    SUM(CONVERT(INT, b.sel_tel_csr)) TelSpeakToCSR,

    SUM(CONVERT(INT, b.sel_inv_del)) InvNumDel,

    SUM(CONVERT(INT, b.sel_inv_unifi)) InvNumUnifi

    FROM ann_events_Tech_Details b

    WHERE b.start_time >= @StartDate

    AND b.start_time <= @EndDate

    AND b.lang IN ('EN', 'BM', 'MD')

    GROUP BY dateadd (dd, 0, datediff(dd, 0, b.start_time)), b.source, b.lang

    ) DT ON a.source = DT.source

    WHERE a.report_id =8 AND a.call_flow_name = @Loc_call_flow_name

    AND a.report_item IN ('No Dial Tone','Noisy', 'CKC End Call', 'CKC Transfer CSR', 'Tel - Speak to CSR', 'Inv Num - Del', 'Inv Num - Unifi')

    GROUP BY DT.DATE, A.SOURCE, a.report_item

    ORDER BY DT.date, a.report_item

    Update: I figured out how to do it and the query speed is slightly slower. Appreciate feedback on my query, thanks!

    SELECT tb.date, tb.report_item as report_item, sum(isnull(tb1.EN,0)) as EN , sum(isnull(tb1.BM ,0)) as BM, sum(isnull(tb1.MD ,0)) as MD

    FROM (

    SELECT (dateadd(dd,0, datediff(dd,0,b.start_time))) as date, (a.source + '-' + a.report_item) AS report_item

    , a.call_flow_name, a.source

    FROM ann_ReportItem a, ann_events_Tech_Details b

    WHERE (dateadd(dd,0, datediff(dd,0,b.start_time))) >= @StartDate

    AND (dateadd(dd,0, datediff(dd,0,b.start_time))) <= @EndDate

    AND a.report_id =8 AND a.call_flow_name = @call_flow_name

    GROUP BY (dateadd(dd,0, datediff(dd,0,b.start_time))), a.report_item, a.call_flow_name, a.source

    ) as tb LEFT JOIN

    (

    SELECT DT. date, a.source, (a.source + '-' + a.report_item ) as report_item,

    SUM(CASE WHEN DT .lang = 'EN' THEN CASE a.report_item WHEN 'No Dial Tone' THEN NoDialTone

    WHEN 'Noisy' THEN Noisy

    WHEN 'CKC End Call' THEN CKCEndCall

    WHEN 'CKC Transfer CSR' THEN CKCTransferCSR

    WHEN 'Tel - Speak to CSR' THEN TelSpeakToCSR

    WHEN 'Inv Num - Del' THEN InvNumDel

    WHEN 'Inv Num - Unifi' THEN InvNumUnifi

    ELSE 0 END ELSE 0 END) EN ,

    SUM(CASE WHEN DT .lang = 'BM' THEN CASE a.report_item WHEN 'No Dial Tone' THEN NoDialTone

    WHEN 'Noisy' THEN Noisy

    WHEN 'CKC End Call' THEN CKCEndCall

    WHEN 'CKC Transfer CSR' THEN CKCTransferCSR

    WHEN 'Tel - Speak to CSR' THEN TelSpeakToCSR

    WHEN 'Inv Num - Del' THEN InvNumDel

    WHEN 'Inv Num - Unifi' THEN InvNumUnifi

    ELSE 0 END ELSE 0 END) BM ,

    SUM(CASE WHEN DT .lang = 'MD' THEN CASE a.report_item WHEN 'No Dial Tone' THEN NoDialTone

    WHEN 'Noisy' THEN Noisy

    WHEN 'CKC End Call' THEN CKCEndCall

    WHEN 'CKC Transfer CSR' THEN CKCTransferCSR

    WHEN 'Tel - Speak to CSR' THEN TelSpeakToCSR

    WHEN 'Inv Num - Del' THEN InvNumDel

    WHEN 'Inv Num - Unifi' THEN InvNumUnifi

    ELSE 0 END ELSE 0 END) MD

    FROM ann_ReportItem a

    INNER JOIN (

    SELECT dateadd (dd, 0, datediff(dd ,0, b.start_time )) as [date], b.source, b. lang,

    SUM(CONVERT (INT, b.sel_tel_nodialtone )) NoDialTone,

    SUM(CONVERT (INT, b.sel_tel_noisy )) Noisy,

    SUM(CONVERT (INT, b.sel_tech_ckc_disconnect )) CKCEndCall,

    SUM(CONVERT (INT, b.sel_tech_ckc_transfer )) CKCTransferCSR,

    SUM(CONVERT (INT, b.sel_tel_csr )) TelSpeakToCSR,

    SUM(CONVERT (INT, b.sel_inv_del )) InvNumDel,

    SUM(CONVERT (INT, b.sel_inv_unifi )) InvNumUnifi

    FROM ann_events_Tech_Details b

    WHERE b. start_time >= @StartDate

    AND b. start_time < dateadd(dd ,0, datediff( dd,0 ,@EndDate)+ 1)

    AND b. lang IN ('EN', 'BM', 'MD')

    GROUP BY dateadd (dd, 0, datediff(dd , 0, b.start_time )), b.source, b.lang

    ) DT ON a.source = DT.source

    WHERE a. report_id =8 AND a. call_flow_name = @call_flow_name

    AND a. report_item IN ('No Dial Tone', 'Noisy', 'CKC End Call', 'CKC Transfer CSR', 'Tel - Speak to CSR' , 'Inv Num - Del' , 'Inv Num - Unifi' )

    GROUP BY DT.DATE , A.SOURCE, a.report_item

    )

    AS tb1 ON tb.source = tb1.source and tb.date = tb1.date and tb.report_item =tb1.report_item

    GROUP BY tb.date , tb1.en, tb.report_item, tb.source

    ORDER BY tb.date, tb.report_item

  • I have read through a few articles on stored procedure performance with parameters, variables and literals but still unable to understand why Parameters run faster than variables in the query below.

    Can someone please enlighten me?

    SELECT a.service_number, rc .dupeCount, a.ani , a. dnis, a .start_time, a.end_time

    FROM ann_events_Tech_Details a

    inner join (

    SELECT service_number , COUNT (*) AS dupeCount

    FROM ann_events_Tech_Details

    WHERE call_flow_name = @call_flow_name AND start_time >= @StartDate AND start_time <= @EndDate AND service_number <> '' AND service_number IS NOT NULL

    GROUP BY service_number

    HAVING COUNT(*) > 1

    ) rc on a.service_number = rc. service_number

    WHERE a. call_flow_name = @call_flow_name AND a.start_time >= @StartDate AND a.start_time <= @EndDate AND a. service_number <> '' AND a.service_number IS NOT NULL

    DECLARE @Loc_call_flow_name nvarchar(50) = @call_flow_name

    DECLARE @Loc_StartDate datetime = @StartDate

    DECLARE @Loc_EndDate datetime = @EndDate

    SELECT a.service_number, rc .dupeCount, a.ani , a. dnis, a .start_time, a.end_time

    FROM ann_events_Tech_Details a

    inner join (

    SELECT service_number , COUNT (*) AS dupeCount

    FROM ann_events_Tech_Details

    WHERE call_flow_name = @Loc_call_flow_name AND start_time >= @Loc_StartDate AND start_time <= @Loc_EndDate AND service_number <> '' AND service_number IS NOT NULL

    GROUP BY service_number

    HAVING COUNT(*) > 1

    ) rc on a.service_number = rc. service_number

    WHERE a. call_flow_name = @Loc_call_flow_name AND a.start_time >= @Loc_StartDate AND a.start_time <= @Loc_EndDate AND a. service_number <> '' AND a.service_number IS NOT NULL

  • jc85 (6/2/2016)


    I have read through a few articles on stored procedure performance with parameters, variables and literals but still unable to understand why Parameters run faster than variables in the query below.

    Can someone please enlighten me?

    SELECT a.service_number, rc .dupeCount, a.ani , a. dnis, a .start_time, a.end_time

    FROM ann_events_Tech_Details a

    inner join (

    SELECT service_number , COUNT (*) AS dupeCount

    FROM ann_events_Tech_Details

    WHERE call_flow_name = @call_flow_name AND start_time >= @StartDate AND start_time <= @EndDate AND service_number <> '' AND service_number IS NOT NULL

    GROUP BY service_number

    HAVING COUNT(*) > 1

    ) rc on a.service_number = rc. service_number

    WHERE a. call_flow_name = @call_flow_name AND a.start_time >= @StartDate AND a.start_time <= @EndDate AND a. service_number <> '' AND a.service_number IS NOT NULL

    DECLARE @Loc_call_flow_name nvarchar(50) = @call_flow_name

    DECLARE @Loc_StartDate datetime = @StartDate

    DECLARE @Loc_EndDate datetime = @EndDate

    SELECT a.service_number, rc .dupeCount, a.ani , a. dnis, a .start_time, a.end_time

    FROM ann_events_Tech_Details a

    inner join (

    SELECT service_number , COUNT (*) AS dupeCount

    FROM ann_events_Tech_Details

    WHERE call_flow_name = @Loc_call_flow_name AND start_time >= @Loc_StartDate AND start_time <= @Loc_EndDate AND service_number <> '' AND service_number IS NOT NULL

    GROUP BY service_number

    HAVING COUNT(*) > 1

    ) rc on a.service_number = rc. service_number

    WHERE a. call_flow_name = @Loc_call_flow_name AND a.start_time >= @Loc_StartDate AND a.start_time <= @Loc_EndDate AND a. service_number <> '' AND a.service_number IS NOT NULL

    The queries seem to be identical.

    The only difference is in the variables used as parameters, and the difference must be caused by different data types.

    Ideally, parameters must be the same data type as the column they are applied to.

    Without it the optimiser may go some sideways when figuring out the best execution plan.

    Another thing which may confuse the optimiser is unnecessary conditions.

    Fulfilling the condition a. service_number <> '' means no NULLs left in the set, which makes a.service_number IS NOT NULL not needed at all.

    If you join 2 tables by a.service_number = rc. service_number then you do not need to check both of them for service_number <> '' AND service_number IS NOT NULL.

    DECLARE @Loc_call_flow_name nvarchar(50) = @call_flow_name

    DECLARE @Loc_StartDate datetime = @StartDate

    DECLARE @Loc_EndDate datetime = @EndDate

    SELECT a.service_number, rc.dupeCount, a.ani, a.dnis, a.start_time, a.end_time

    FROM (

    SELECT call_flow_name, service_number ,

    MIN(start_time) start_time_from, MAX(start_time) start_time_to,

    COUNT (*) AS dupeCount

    FROM dbo.ann_events_Tech_Details

    WHERE call_flow_name = @Loc_call_flow_name

    AND start_time >= @Loc_StartDate AND start_time <= @Loc_EndDate

    AND service_number <> ''

    GROUP BY service_number

    HAVING COUNT(*) > 1

    ) rc

    inner join dbo.ann_events_Tech_Details a on a.call_flow_name = rc.call_flow_name AND a.service_number = rc.service_number

    AND a.start_time BETWEEN rc.start_time_from and rc.start_time_to

    _____________
    Code for TallyGenerator

Viewing 15 posts - 61 through 75 (of 76 total)

You must be logged in to reply to this topic. Login to reply