Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
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
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, March 22, 2016 2:30 PM
Points: 90, Visits: 178
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 @ 1:31 PM
Points: 14,407, Visits: 37,678
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
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
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, March 22, 2016 2:30 PM
Points: 90, Visits: 178
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