Solved

Adding ROW and COLUMN subtotals and percentages to TSQL output

Posted on 2010-09-21
10
720 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
 
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now