Solved

Adding ROW and COLUMN subtotals and percentages to TSQL output

Posted on 2010-09-21
10
721 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

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

Suggested Solutions

Title # Comments Views Activity
PHP installation issues 11 59
Problem with SQL Script - Cannot call methods on char 2 19
Live Storage Vmotion VMs with shared VMDK 10 51
sql server insert 12 28
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

816 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

9 Experts available now in Live!

Get 1:1 Help Now