SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to total on distinct?


How to total on distinct?

Author
Message
Frank Cazabon
Frank Cazabon
SSC Veteran
SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)

Group: General Forum Members
Points: 246 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?
Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28760 Visits: 39980
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!

Steven Willis
Steven Willis
SSC Eights!
SSC Eights! (855 reputation)SSC Eights! (855 reputation)SSC Eights! (855 reputation)SSC Eights! (855 reputation)SSC Eights! (855 reputation)SSC Eights! (855 reputation)SSC Eights! (855 reputation)SSC Eights! (855 reputation)

Group: General Forum Members
Points: 855 Visits: 1721
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



Frank Cazabon
Frank Cazabon
SSC Veteran
SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)

Group: General Forum Members
Points: 246 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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search