Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to total on distinct? Expand / Collapse
Author
Message
Posted Tuesday, March 12, 2013 12:21 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, June 17, 2014 7:07 AM
Points: 66, Visits: 124
Hi,

I have to create a report like this:

Deposit ID  |  Deposit Amount  |  Receipt #  |  Receipt Amount
1 | 1000 | 1 | 500
| | 2 | 500
2 | 2000 | 3 | 1500
| | 4 | 500
------------------------------------------------------------
Total | 3000 | 1 | 3000


I've pulled all the data into one dataset and have the layout and totalling of the Receipt Amount fine.

My problem is that the total Deposit Amount is way over because it occurs once in each row, even though I am only displaying it once for each row. Is there a way that I can get this totalled properly?
Post #1430044
Posted Tuesday, March 12, 2013 1:52 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:22 PM
Points: 12,889, Visits: 31,839
i might be reading it wrong, but i think you just want to use either a CTE or a subquery, so you can further group your data;
something like this, maybe?
SELECT
[Deposit ID],
SUM([Deposit Amount]) AS TotalDeposits,
SUM([Receipt Amount]) AS TotalReceipts
FROM (SELECT
DISTINCT
[Deposit ID],
[Deposit Amount],
[Receipt #],
[Receipt Amount]
FROM MyTable) MyAlias
GROUP BY [Deposit ID]




Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1430080
Posted Tuesday, March 12, 2013 1:52 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
Frank Cazabon (3/12/2013)
Hi,

I have to create a report like this:

Deposit ID  |  Deposit Amount  |  Receipt #  |  Receipt Amount
1 | 1000 | 1 | 500
| | 2 | 500
2 | 2000 | 3 | 1500
| | 4 | 500
------------------------------------------------------------
Total | 3000 | 1 | 3000


I've pulled all the data into one dataset and have the layout and totalling of the Receipt Amount fine.

My problem is that the total Deposit Amount is way over because it occurs once in each row, even though I am only displaying it once for each row. Is there a way that I can get this totalled properly?


Try getting your totals like this:


SELECT
SUM(DepositAmount) OVER (PARTITION BY DepositID) AS TotalDeposits
,SUM(ReceiptAmount) OVER (PARTITION BY ReceiptID) AS TotalReceipts


Post #1430081
Posted Tuesday, March 12, 2013 3:13 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, June 17, 2014 7:07 AM
Points: 66, Visits: 124
Thanks Steven & Lowell, that helped. I didn't quite use your queries but I adjusted my query (stored procedure actually) to do the totalling for me.
Post #1430121
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse