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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:38:03+00:00 2026-06-14T14:38:03+00:00

I am working on a aggregate report and I have got so far with

  • 0

I am working on a aggregate report and I have got so far with the data but I am unsure how to proceed from here. I get by with SQL but my skills are limited

I have the follow in a temp table

ID   Count  Action
2   23      Installed
2   12      Uninstalled
2   36      Unchanged
3   12      Installed
3   25      Unchanged
4   35      Installed
4   25      Unchanged

I want to convert this into this format

ID  Installed   Uninstalled Unchanged
2   23          12          36
3   12          0           36
4   35          0           25

I have no idea where to go or even how to start to achieve this and cannot find anything to point me in the right direction (Im sure its there somewhere)

Any help would be appriciated

  • 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-14T14:38:05+00:00Added an answer on June 14, 2026 at 2:38 pm

    Try this:

    SELECT
      ID, 
      MAX(CASE WHEN "Action" = 'Installed' THEN Count END) AS 'Installed',
      MAX(CASE WHEN "Action" = 'Uninstalled ' THEN Count END) AS 'Uninstalled',
      MAX(CASE WHEN "Action" = 'Unchanged' THEN Count END) AS 'Unchanged'
    FROM Table
    GROUP BY ID;
    

    Or using the SQL Server PIVOT table operator like so:

    SELECT *
    FROM 
    (
       SELECT * FROM table
    )t
    PIVOT
    (
       MAX("Count") FOR "Action" IN([Installed], [Uninstalled], [Unchanged])
    ) p
    

    SQL Fiddle Demo for both

    However, for unknown number of Actions, you will have to select them dynamically like so:

    DECLARE @cols AS NVARCHAR(MAX);
    DECLARE @query AS NVARCHAR(MAX);
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Action) 
                        from Table1
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'');
    
    SET @query = 'SELECT ID,  ' + @cols + ' from 
                 (
                    SELECT * FROM Table1
                 ) x
                 PIVOT 
                 (
                    MAX(count)
                    FOR action IN (' + @cols + ')
                 ) p ';
    
    EXECUTE(@query);
    

    SQL Fiddle Demo(Dynamic Version)

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

Sidebar

Related Questions

I am working on optimizing one of the SQL Job. Here I have few
Working with Reporting Services 2008 r2. So here's my issue: We have 5 reports
We have a data provider class that returns repositories for each aggregate in our
I am working on a case study where I have to get comments, activities
I'm working on a sql selection where I need to get all the smallest
I'm working on a class responsible for aggregating data from several sites at once.
For a current project I am working I need to return an aggregate report
I've been attempting to aggregate (some what erratic) daily data. I'm actually working with
I'm working on tick data and want to aggregate my xts irregularly spaced series
Working with H2 I get this error when I try to write a row

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.