Solved

sql script - grant total

Posted on 2011-03-02
5
999 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
  • 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 500 total points
ID: 35016583
0

Featured Post

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.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

911 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

23 Experts available now in Live!

Get 1:1 Help Now