Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8764135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:57:14+00:00 2026-06-13T15:57:14+00:00

I have the below SQL statement. The column wrapupcode can be up to four

  • 0

I have the below SQL statement. The column wrapupcode can be up to four values:

Sales, Service, Meeting, Other

When the SQL is run, the column wrapupcode is showing as a single column. What I would like to work out, is to dynamically create a new wrapupcode based on the value. We may added or remove the codes, otherwise I would case for the value.

This is based on a MySQL database.

Data currently looks like:

user  wrapupcode  intialtime  endofwrapup  timediff
joe   Service     11:38       11:39        1
jenny Sales       11:35       11:36        1
joe   Service     11:41       11:42        1

But would like to see if its possible via SQL on a MySQL database:

user  Service  Sales     timediff
joe   2                  2
jenny          1         1

I can do the sum/avg for the times and totals, its just adding a new column on each different wrapupcode.

SELECT
     ( SELECT loginid FROM `axpuser` WHERE age.userid = axpuser.pkey ) as user,
     ( SELECT name FROM `breakcode` WHERE age.wrapupcode = pkey ) as wrapupcode,
     time(age.`instime`) as initialtime,
     age.`ENDOFWRAPUPTIME` AS endofwrapup,
     timediff(age.`ENDOFWRAPUPTIME`, time(age.`instime`)) as timediff
  FROM
     agentcallinformation age
  WHERE
     age.endofwrapuptime IS NOT null and ( SELECT name FROM `breakcode` WHERE age.wrapupcode =   pkey ) <> ''
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:57:15+00:00Added an answer on June 13, 2026 at 3:57 pm

    The basic syntax will be:

    select user,
        sum(case when wrapupcode = 'Service' then 1 else 0 end) Service,
        sum(case when wrapupcode = 'Sales' then 1 else 0 end) Sales,
        sum(case when wrapupcode = 'Meeting' then 1 else 0 end) Meeting,
        sum(case when wrapupcode = 'Other' then 1 else 0 end) Other,
        count(timediff) timediff
    from
    (       
        <yourquery>
    ) src
    group by user
    

    Hard-coded static version will be something similar to this:

    select user,
        sum(case when wrapupcode = 'Service' then 1 else 0 end) Service,
        sum(case when wrapupcode = 'Sales' then 1 else 0 end) Sales,
        sum(case when wrapupcode = 'Meeting' then 1 else 0 end) Meeting,
        sum(case when wrapupcode = 'Other' then 1 else 0 end) Other,
        count(timediff) timediff
    from
    (       
        select u.loginid as user,
            b.name wrapupcode,
            time(age.`instime`) as initialtime,
            age.`ENDOFWRAPUPTIME` AS endofwrapup,
            count(timediff(age.`ENDOFWRAPUPTIME`,   time(age.`instime`))) as timediff
        from agentcallinformation age
        left join `axpuser` u
            on age.userid = u.pkey
        left join `breakcode` b
            on age.wrapupcode = b.pkey
            and age.wrapupcode <> ''
        WHERE age.endofwrapuptime IS NOT null 
    ) src
    group by user
    

    I changed the query to use JOIN syntax instead of the correlated subqueries.

    If you need a dynamic version, then you can use prepared statements:

    SET @sql = NULL;
    SELECT
      GROUP_CONCAT(DISTINCT
        CONCAT(
          'sum(case when wrapupcode = ''',
          name,
          ''' then 1 else 0 end) AS ',
          name
        )
      ) INTO @sql
    FROM breakcode;
    
    SET @sql = CONCAT('SELECT user, ', @sql, ' 
                        , count(timediff) timediff
                      from
                      (     
                        select u.loginid as user,
                            b.name wrapupcode,
                            time(age.`instime`) as initialtime,
                            age.`ENDOFWRAPUPTIME` AS endofwrapup,
                            count(timediff(age.`ENDOFWRAPUPTIME`,   time(age.`instime`))) as timediff
                        from agentcallinformation age
                        left join `axpuser` u
                            on age.userid = u.pkey
                        left join `breakcode` b
                            on age.wrapupcode = b.pkey
                            and age.wrapupcode <> ''
                        WHERE age.endofwrapuptime IS NOT null 
                    ) src
                    GROUP BY user');
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with the SQL statement detailed below. The query returns the
i have four tables like below in sql server 2008 : TABLE 1 ->
I have strings like the ones below in a SQL column. I want to
I have a SQL select statement that doesnt want to format my $ values
Below is an sql statement and an error I'm receiving. I have included all
I have the below code that splits the sql statement and give its indices
I have the code below: string SQL = select * from + TableName; using
I have a table similar to below in sql server 2005 date_column | field1
I'm using Sql-Server, and I have the below query which returns all columns that
I have an SQL query (below) that essentially takes a student from tbStudents, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.