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 8482577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:51:38+00:00 2026-06-10T19:51:38+00:00

I have a database that contains phone call records (I am NOT a telemarketer,

  • 0

I have a database that contains phone call records (I am NOT a telemarketer, this is emergency call data). Each row is a call record. Some of the columns contain timing information. There is also a priority column for each call. It looks like this:

  PRIORITY    TIMING1     TIMING2     TIMING3
  -----0----------22----------3-----------43 
  -----1----------42----------10----------12
  -----0----------12----------13----------6
  -----2----------23----------12----------37
  -----1----------12----------16----------23

The output that I need is basically this one row per timing with the sum of all the time grouped by priority as columns. There are a fixed number of timings and priorities that I know of ahead of time. It would look like this:

------------Priority 0-------Priority 1-------Priority 2
  Timing1----123--------------332--------------233
  Timing2----265--------------241--------------302
  Timing3----387--------------192--------------201

Is it possible to do something like this in a single query? The query also has to be runnable on a mobile platform that is fairly resource constrained, although time to run isn’t a huge deal. Given the size of this table and the memory issue I would rather avoid temporary tables.

  • 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-10T19:51:39+00:00Added an answer on June 10, 2026 at 7:51 pm

    You can use UNPIVOT and PIVOT for this:

    Here is a Static Version of the query – a static version means that you hard-code the values.

    select *
    from
    (
      select *
      from
      (
        select priority, timing1, timing2, timing3
        from yourtable
      ) x
      unpivot
      (
        value
        for field in (timing1, timing2, timing3)
      ) u
    ) x1
    pivot
    (
      sum(value)
      for priority in ([0], [1], [2])
    ) p
    

    see SQL Fiddle with Demo

    You can also do this Dynamically:

    DECLARE @colsUnpivot AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX),
        @colsPivot as  NVARCHAR(MAX),
        @colsPivotName as  NVARCHAR(MAX)
    
    select @colsUnpivot = stuff((select ','+ quotename(C.name)
             from sys.columns as C
             where C.object_id = object_id('yourtable') and
                   C.name LIKE 'timing%'
             for xml path('')), 1, 1, '')
    
    select @colsPivot = 
        STUFF((SELECT distinct ',' + Quotename(priority)
                 from yourtable
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    select @colsPivotName = 
        STUFF((SELECT distinct ',' 
               + Quotename(priority) + ' as Priority' + cast(priority as varchar(1))
                 from yourtable
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query 
      = 'select col, '+ @colsPivotName +'
          from
          (
            select priority, val, col
            from 
            (
              select priority, ' + @colsUnpivot + '
              from yourtable
            ) x
            unpivot
            (
              val
              for col in ('+ @colsUnpivot +')
            ) u
          ) x1
          pivot
          (
            sum(val)
            for priority in ( '+ @colspivot + ')
          ) p'
    
    exec(@query)
    

    see SQL Fiddle with Demo

    Edit #1, you stated in the comments that you are using SQLite which does not have a PIVOT function. But you can still do this using a UNION ALL and then an aggregate function with a CASE:

    select col,
      sum(case when priority = 0 then value end) as Priority0,
      sum(case when priority = 1 then value end) as Priority1,
      sum(case when priority = 2 then value end) as Priority2
    from
    (
      select priority, timing1 as value, 'timing1' as col
      from yourtable
      union all
      select priority, timing2 as value, 'timing2' as col
      from yourtable
      union all
      select priority, timing3 as value, 'timing3' as col
      from yourtable
    ) x
    group by col
    

    see SQL Fiddle with Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Situation: I have a database that contains sales records, each record has a ID
I have a database that contains user details including sensitive data. They're not as
I am not experienced in this field. I have a database that contains a
I have a Database that contains data about articles , structures and manufacturers .
I have a database that contains data for many clients. Currently, we insert tens
I have a database that contains a table that looks a bit like this:
We have a database that contains xml fields. At this moment we perform queries
I have a database that contains rows with links to each episode in them.
I have a database that contains a history of product sales. For example the
I have an xml database that contains films, for example: <film id=5> <title>The Avengers</title>

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.