Solved

Adding ROW and COLUMN subtotals and percentages to TSQL output

Posted on 2010-09-21
10
724 Views
Last Modified: 2012-05-10
See attached workbook for examples of:
- SOURCE data (temp table XROADS_RADIOLOGY)
- Existing OUTPUT
- Desired OUTPUT
...each on its own tab.

Starting with SOURCE DATA and using the following query:
SELECT Provider, Type, QTY,(POINTS1 + POINTS2) as Points
FROM XROADS_RADIOLOGY
ORDER BY Type,Provider

I get the OUTPUT EXISTING shown in the attachment.

How may I modify my query so as to include (a) subtotal of QTY and Points by Type, and (b) Percentage of points by provider for each Type.  See OUTPUT DESIRED in the attachment.



 DATA-SAMPLES.xls
0
Comment
Question by:bleonardi
  • 5
  • 4
10 Comments
 
LVL 6

Expert Comment

by:matthewrhoades
ID: 33730643
SELECT Provider, Type, QTY,(POINTS1 + POINTS2) as Points, (POINTS1+POINTS2)/SUM(POINTS1+POINTS2)
FROM XROADS_RADIOLOGY
GROUP BY Provider, Type, QTY
ORDER BY Type,Provider

If you get an error about one of the fields not being in the group by clause or an aggregate, you can just add it to the group by clause.
0
 

Author Comment

by:bleonardi
ID: 33730829
I did get an error as you thought, and changed the query to this and it ran:

SELECT Provider, Type, QTY,(POINTS1 + POINTS2) as Points, (POINTS1+POINTS2)/SUM(POINTS1+POINTS2)
FROM XROADS_RADIOLOGY
GROUP BY Provider, Type, QTY, points1, points2
ORDER BY Type,Provider

However the new column was all "1"'s (I suppose there needs to be some data conversion?).

Also, there is no subtotal by Type.
0
 
LVL 1

Expert Comment

by:Theand1978
ID: 33732849
It makes use of temp table and it is a 2 stage process that could probably be improved upon but this is my initial approach and solution.

Image represents what the results will look lik

IF Object_ID( 'tempdb..#Temp', 'U' ) IS NOT NULL DROP TABLE #Temp
GO

   --calculate the points
   SELECT Provider, [Type], QTY, (Points1+Points2) AS Points, 0.0 AS [Percent], 1 AS Ord
     INTO #Temp
     FROM SourceData
    UNION ALL
  -- Calculate the subtotals
  SELECT 'total', [Type], sum(QTY), sum( ( points1+points2) ), (sum( ( points1+points2) ) / sum( ( points1+points2) ) * 100), 2 
    FROM SourceData
GROUP BY [Type]
GO

--Calculate the percent 
UPDATE T 
   SET T.[Percent] = convert( float, T.Points ) / convert( float, A.Points ) * 100
  FROM #Temp T
  JOIN (SELECT [Type], Points FROM #Temp WHERE Provider = 'total' ) A ON A.[Type] = T.[Type] 
GO

  SELECT Provider, [Type], QTY, Points, [Percent] 
    FROM #Temp
ORDER BY [Type], Ord, Provider
GO

Open in new window

results.jpg
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 1

Expert Comment

by:Theand1978
ID: 33734310
Another version with out using a Temp Table and just selects / Derived table and a union
SELECT Provider, 
          S.[Type], 
          QTY, 
          (Points1+Points2) AS Points, 
          Convert( Decimal( 5,1), convert( Decimal(5,1) , (Points1+Points2)) / Convert( Decimal(5,1), T.Total) * 100.0 ) as [Percent]
     FROM SourceData S
     JOIN ( SELECT [Type], sum( ( points1+points2) ) AS Total FROM SourceData GROUP BY [Type] ) T ON T.[Type] = S.[Type]
    UNION ALL
  -- Calculate the subtotals
  SELECT 'total', [Type], sum(QTY), sum( ( points1+points2) ), (sum( ( points1+points2) ) / sum( ( points1+points2) ) * 100)
    FROM SourceData
GROUP BY [Type]
Order by [Type], Points
GO

Open in new window

0
 

Author Comment

by:bleonardi
ID: 33734519
Thank Thean1978.  I ran and got this message:

Server: Msg 209, Level 16, State 1, Line 1
Ambiguous column name 'TYPE'.
0
 
LVL 1

Expert Comment

by:Theand1978
ID: 33734558
I am assuming you ran the second version ( with out the temp table )
0
 

Author Comment

by:bleonardi
ID: 33734570
Correct
0
 
LVL 1

Accepted Solution

by:
Theand1978 earned 500 total points
ID: 33734616
Try this. I just aliased every column in the SQL, I don't see how it could be "ambiguous".
SELECT S.Provider, 
          S.[Type], 
          S.QTY, 
          (S.Points1+S.Points2) AS Points, 
          Convert( Decimal( 5,1), convert( Decimal(5,1) , (S.Points1+S.Points2)) / Convert( Decimal(5,1), T.Total) * 100.0 ) as [Percent]
     FROM SourceData S
     JOIN ( SELECT [Type], sum( ( points1+points2) ) AS Total FROM SourceData GROUP BY [Type] ) T ON T.[Type] = S.[Type]
    UNION ALL
  -- Calculate the subtotals
  SELECT 'total', ST.[Type], sum(ST.QTY), sum( ( ST.points1+ST.points2) ), (sum( ( ST.points1+ST.points2) ) / sum( ( ST.points1+ST.points2) ) * 100)
    FROM SourceData ST
GROUP BY ST.[Type]
Order by [Type], Points

Open in new window

0
 

Author Comment

by:bleonardi
ID: 33734685
I ran the new version and got the same error.  

After changing the last line to
   Order by S.[Type], Points
it worked great!

Points awarded.  Thanks
0
 
LVL 1

Expert Comment

by:Theand1978
ID: 33734727
It was a pleasure,

And Thank You !! that was a nice little challenge to help my day along.

Take Care
   Clint
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question