Avatar of tmajor99
tmajor99

asked on 

SQL Select to summarize data and total

I need a SQL Select that will summarize data by category and code.  Here is what my table rows look like:

Product     Code        Category    
1203           O              Online          
3900           H              Tools          
4999           X               Heaters      
5009           O              Heaters      
8990                            Tools          
3111           H              Tools          
5115           O              Tools            

I need a SQL select that will summarize the data by Category and Code like this:
                           O             X            H       Other         Total    
Online               1                                                          1
Tools                 1                             2          1               4
Heaters            1               1                                          2

Summary         3              1            2          1                7
Microsoft SQL Server

Avatar of undefined
Last Comment
Scott Pletcher
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Are the Code values preset or does the query need to be dynamic?
ASKER CERTIFIED SOLUTION
Avatar of Dung Dinh
Dung Dinh
Flag of Viet Nam image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of tmajor99
tmajor99

ASKER

Responding to Brain's question about the codes.  The codes are not known so the query does need to be dynamic.
Avatar of tmajor99
tmajor99

ASKER

Is there any other way of doing this by using group by's and cross apply?
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo