Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sql script - grant total

Posted on 2011-03-02
5
Medium Priority
?
1,020 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

610 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