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


sum a column data based on another column data


sum a column data based on another column data

Author
Message
gissah
gissah
SSC-Enthusiastic
SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)

Group: General Forum Members
Points: 122 Visits: 329
I am trying to sum a column total bases on an invoiced number column in tsql or ssrs.
if I SUM(final) OVER(PARTITION BY InvoiceNmbr) it works fine on all the columns.
But once I start to drill down or filter it by office or a user, I only get the total by each user on the invoice and not the cumulative total by the invoice.
I want the cumulative total by invoice regardless of which filter I select.
any help will be appreciated.
Jack Corbett
  Jack Corbett
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18594 Visits: 14898
Can you share your complete query?

It sounds like you are doing something like this:


SELECT
I.*,
SUM(final) OVER(PARTITION BY InvoiceNmbr) AS invoicedTotal
FROM
dbo.invoices AS I
WHERE
I.USER = 'jsmith';



But there are rows for an InvoiceNmbr by multiple users and when you run the query, you expect to see the total for the invoice number, while only showing details for the specific user. I think you need something like this:


SELECT
I.*,
TI.invoicedTotal
FROM
dbo.invoices AS I CROSS APPLY
(
SELECT SUM(final) AS invoicedTotal FROM dbo.invoices AS TI WHERE I.InvoiceNmber = I2.InvoiceNmbr
) AS TI
WHERE
I.USER = 'jsmith';





Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
gissah
gissah
SSC-Enthusiastic
SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)

Group: General Forum Members
Points: 122 Visits: 329
Thanks Mr. Jack you are right on the money.
That solved my issue.
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