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

  • Home
  • SEARCH
  • 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 54813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:16:46+00:00 2026-05-10T17:16:46+00:00

I have a database in the following format: ID TYPE SUBTYPE COUNT MONTH 1

  • 0

I have a database in the following format:

 ID    TYPE   SUBTYPE    COUNT   MONTH  1      A      Z          1       7/1/2008  1      A      Z          3       7/1/2008  2      B      C          2       7/2/2008  1      A      Z          3       7/2/2008 

Can I use SQL to convert it into this:

ID    A_Z   B_C   MONTH 1     4     0     7/1/2008 2     0     2     7/2/2008 1     0     3     7/2/2008 

So, the TYPE, SUBTYPE are concatenated into new columns and COUNT is summed where the ID and MONTH match.

Any tips would be appreciated. Is this possible in SQL or should I program it manually?

The database is SQL Server 2005.

Assume there are 100s of TYPES and SUBTYPES so and ‘A’ and ‘Z’ shouldn’t be hard coded but generated dynamically.

  • 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. 2026-05-10T17:16:46+00:00Added an answer on May 10, 2026 at 5:16 pm

    SQL Server 2005 offers a very useful PIVOT and UNPIVOT operator which allow you to make this code maintenance-free using PIVOT and some code generation/dynamic SQL

    /* CREATE TABLE [dbo].[stackoverflow_159456](     [ID] [int] NOT NULL,     [TYPE] [char](1) NOT NULL,     [SUBTYPE] [char](1) NOT NULL,     [COUNT] [int] NOT NULL,     [MONTH] [datetime] NOT NULL ) ON [PRIMARY] */  DECLARE @sql AS varchar(max) DECLARE @pivot_list AS varchar(max) -- Leave NULL for COALESCE technique DECLARE @select_list AS varchar(max) -- Leave NULL for COALESCE technique  SELECT @pivot_list = COALESCE(@pivot_list + ', ', '') + '[' + PIVOT_CODE + ']'         ,@select_list = COALESCE(@select_list + ', ', '') + 'ISNULL([' + PIVOT_CODE + '], 0) AS [' + PIVOT_CODE + ']' FROM (     SELECT DISTINCT [TYPE] + '_' + SUBTYPE AS PIVOT_CODE     FROM stackoverflow_159456 ) AS PIVOT_CODES  SET @sql = ' ;WITH p AS (     SELECT ID, [MONTH], [TYPE] + ''_'' + SUBTYPE AS PIVOT_CODE, SUM([COUNT]) AS [COUNT]     FROM stackoverflow_159456     GROUP BY ID, [MONTH], [TYPE] + ''_'' + SUBTYPE ) SELECT ID, [MONTH], ' + @select_list + ' FROM p PIVOT (     SUM([COUNT])     FOR PIVOT_CODE IN (         ' + @pivot_list + '     ) ) AS pvt '  EXEC (@sql) 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 61k
  • Answers 61k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer function dir_contents_recursive($dir) { // open handler for the directory $iter… May 11, 2026 at 9:45 am
  • added an answer You can't do that in linq2sql. Use a stored procedure,… May 11, 2026 at 9:45 am
  • added an answer If you want all your services to return the same… May 11, 2026 at 9:45 am

Related Questions

I have a database in the following format: ID TYPE SUBTYPE COUNT MONTH 1
I have the following tables in my database that have a many-to-many relationship, which
I have a database that hold's a user's optional profile. In the profile I
I have a table in the database that I'm retrieving using LINQ to SQL,
I have a field in a database that is nearly unique: 98% of the
Let's say that I have a record in the database and that both admin
I have a database which holds the residents of each house in a certain
I have data coming from the database in the form of a DataSet .
I have a database that is stuck in single-user mode. I kill the process
I have database with many tables. In the first table, I have a field

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.