?
Solved

sql script - grant total

Posted on 2011-03-02
5
Medium Priority
?
1,018 Views
Last Modified: 2012-08-13
the supplied sql script works fine and produces the following.

how do I script -- adding an accumulative column (adding accumulative all the subtotals) - and then a final grand total cell.


Frequency  ChargeableAmount    SubTotals
     2          0                       0
     4      100000                  400000
     3      200000                  600000
    17      500000                 8500000
     5      1000000                 5000000
SELECT COUNT(RecordID) AS Frequency, ChargeableAmount AS ChargeableAmounts, SUM (ChargeableAmount) AS SubTotals

FROM dbo.abcData

WHERE (TransactionKey = 8)
  AND (TransactionTime BETWEEN '2011-03-01 00:00:00.000' AND '2011-03-01 23:59:59.999')
  
GROUP BY ChargeableAmount

Open in new window

0
Comment
Question by:amillyard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35016425
not sure what you exactly want

did you want one new column with 3 of this which is having total of all subtotals ...it is same for all...is this?


 SELECT COUNT(RecordID) AS Frequency, ChargeableAmount AS ChargeableAmounts, SUM (ChargeableAmount) AS SubTotals , (select  sum(ChargeableAmount) from dbo.abcData) as grandtotal

FROM dbo.abcData

WHERE (TransactionKey = 8)
  AND (TransactionTime BETWEEN '2011-03-01 00:00:00.000' AND '2011-03-01 23:59:59.999')
 
GROUP BY ChargeableAmount
0
 
LVL 25

Expert Comment

by:reb73
ID: 35016467
Something like this?
;WITH CTE (RowNo, Frequency, ChargeableAmounts, SubTotals)
AS
(
	SELECT	ROW_NUMBER() OVER (order by ChargeableAmount),
		COUNT(RecordID) AS Frequency, ChargeableAmount AS ChargeableAmounts, SUM (ChargeableAmount) AS SubTotals
	FROM	dbo.abcData
	WHERE (TransactionKey = 8)
	  AND (TransactionTime BETWEEN '2011-03-01 00:00:00.000' AND '2011-03-01 23:59:59.999')
	GROUP BY ChargeableAmount 
)
SELECT RowNo, Frequency, ChargeableAmounts, SubTotals, (SELECT SUM(b.SubTotals) FROM CTE b WHERE b.RowNo <= a.RowNo) as RunningTotals
FROM CTE a
COMPUTE SUM(SubTotals)

Open in new window

0
 

Author Comment

by:amillyard
ID: 35016504
pratima_mcs:  am getting the error as follows:

Arithmetic overflow error converting expression to data type int.
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35016565
SELECT COUNT(RecordID) AS Frequency, ChargeableAmount AS ChargeableAmounts, SUM (ChargeableAmount) AS SubTotals ,
CAST( (select  sum(ChargeableAmount) from dbo.abcData) as bigint) as grandtotal

FROM dbo.abcData

WHERE (TransactionKey = 8)
  AND (TransactionTime BETWEEN '2011-03-01 00:00:00.000' AND '2011-03-01 23:59:59.999')
 
GROUP BY ChargeableAmount
0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 2000 total points
ID: 35016583
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

752 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