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

Create a Column That holds the SUM of values in the same field where The Invoice Number is the same Expand / Collapse
Author
Message
Posted Friday, February 01, 2013 7:44 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Yesterday @ 9:57 AM
Points: 85, Visits: 407
First, sorry for the confusing title.

I am looking for a way to sum the values based on the customerid and invoiceid being the same for multiple rows Then adding a column with the sum of all.

SELECT        T1.IDCUST, T1.IDINVC, T2.AMTPAYM, 'SumofRows' AS TotalPayment
FROM MyTable1_ARIBH T1 INNER JOIN
MyTable2_ARTCP T2 ON T1.IDCUST = T2.IDCUST AND
T1.IDINVC = T2.IDINVC
WHERE (T1.IDCUST = 'TOR829') AND (T1.IDINVC = '72124')


Results: I would like the TotalPayment Column to be the SUM of AMTPAYM from the 3 rows. Thanks
IDCUST IDINVC AMTPAYM TotalPayment
TOR829 72124 23815.180
TOR829 72124 72813.500
TOR829 72124 19.070


Post #1414684
Posted Friday, February 01, 2013 7:51 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, April 12, 2013 3:51 AM
Points: 5,075, Visits: 4,831
Something like this?
SELECT        
T1.IDCUST,
T1.IDINVC,
T2.AMTPAYM,
Dev1.TotalPayment
FROM
MyTable1_ARIBH T1
INNER JOIN
MyTable2_ARTCP T2
ON
T1.IDCUST = T2.IDCUST
AND
T1.IDINVC = T2.IDINVC
INNER JOIN
(
SELECT
T1.IDCUST,
T1.IDINVC,
SUM(T2.AMTPAYM) AS TotalPayment
FROM
MyTable1_ARIBH T1
INNER JOIN
MyTable2_ARTCP T2
ON
T1.IDCUST = T2.IDCUST
AND
T1.IDINVC = T2.IDINVC
WHERE
T1.IDCUST = 'TOR829'
AND
T1.IDINVC = '72124'
GROUP BY
T1.IDCUST,
T1.IDINVC
) AS Dev1
ON
T1.IDCUST = Dev1.IDCUST
AND
T1.IDINVC = Dev1.IDINVC
WHERE
T1.IDCUST = 'TOR829'
AND
T1.IDINVC = '72124'





Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1414688
Posted Friday, February 01, 2013 8:41 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:27 AM
Points: 5,618, Visits: 10,990
Or this:

SELECT
T1.IDCUST,
T1.IDINVC,
T2.AMTPAYM,
TotalPayment = SUM(AMTPAYM) OVER (PARTITION BY IDCUST, IDINVC)
FROM MyTable1_ARIBH T1
INNER JOIN MyTable2_ARTCP T2
ON T2.IDCUST = T1.IDCUST
AND T2.IDINVC = T1.IDINVC
WHERE T1.IDCUST = 'TOR829'
AND T1.IDINVC = '72124'



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1414708
Posted Friday, February 01, 2013 8:50 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, April 12, 2013 3:51 AM
Points: 5,075, Visits: 4,831
ChrisM@Work (2/1/2013)
Or this:

SELECT
T1.IDCUST,
T1.IDINVC,
T2.AMTPAYM,
TotalPayment = SUM(AMTPAYM) OVER (PARTITION BY IDCUST, IDINVC)
FROM MyTable1_ARIBH T1
INNER JOIN MyTable2_ARTCP T2
ON T2.IDCUST = T1.IDCUST
AND T2.IDINVC = T1.IDINVC
WHERE T1.IDCUST = 'TOR829'
AND T1.IDINVC = '72124'



Doh, I keep forgetting you can OVER an aggregate function.




Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1414715
Posted Friday, February 01, 2013 6:51 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 4:51 PM
Points: 32,923, Visits: 26,811
anthony.green (2/1/2013)
ChrisM@Work (2/1/2013)
Or this:

SELECT
T1.IDCUST,
T1.IDINVC,
T2.AMTPAYM,
TotalPayment = SUM(AMTPAYM) OVER (PARTITION BY IDCUST, IDINVC)
FROM MyTable1_ARIBH T1
INNER JOIN MyTable2_ARTCP T2
ON T2.IDCUST = T1.IDCUST
AND T2.IDINVC = T1.IDINVC
WHERE T1.IDCUST = 'TOR829'
AND T1.IDINVC = '72124'



Doh, I keep forgetting you can OVER an aggregate function.


BWAA-HAAAA!!!! I used to OVER look it all the time!


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1414892
Posted Monday, February 04, 2013 12:57 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, April 12, 2013 3:51 AM
Points: 5,075, Visits: 4,831
Jeff Moden (2/1/2013)
anthony.green (2/1/2013)
ChrisM@Work (2/1/2013)
Or this:

SELECT
T1.IDCUST,
T1.IDINVC,
T2.AMTPAYM,
TotalPayment = SUM(AMTPAYM) OVER (PARTITION BY IDCUST, IDINVC)
FROM MyTable1_ARIBH T1
INNER JOIN MyTable2_ARTCP T2
ON T2.IDCUST = T1.IDCUST
AND T2.IDINVC = T1.IDINVC
WHERE T1.IDCUST = 'TOR829'
AND T1.IDINVC = '72124'



Doh, I keep forgetting you can OVER an aggregate function.


BWAA-HAAAA!!!! I used to OVER look it all the time!


Your just to cool for school Jeff




Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1415142
Posted Monday, February 04, 2013 7:06 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 4:51 PM
Points: 32,923, Visits: 26,811
anthony.green (2/4/2013)
Jeff Moden (2/1/2013)
anthony.green (2/1/2013)
ChrisM@Work (2/1/2013)
Or this:

SELECT
T1.IDCUST,
T1.IDINVC,
T2.AMTPAYM,
TotalPayment = SUM(AMTPAYM) OVER (PARTITION BY IDCUST, IDINVC)
FROM MyTable1_ARIBH T1
INNER JOIN MyTable2_ARTCP T2
ON T2.IDCUST = T1.IDCUST
AND T2.IDINVC = T1.IDINVC
WHERE T1.IDCUST = 'TOR829'
AND T1.IDINVC = '72124'



Doh, I keep forgetting you can OVER an aggregate function.


BWAA-HAAAA!!!! I used to OVER look it all the time!


Your just to cool for school Jeff


Not even close... I'm trying to quit smoking, again, and my jokes are really going to such for about the next 6 months because of it.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1415552
Posted Tuesday, February 05, 2013 4:05 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, April 12, 2013 3:51 AM
Points: 5,075, Visits: 4,831
Jeff Moden (2/4/2013)
anthony.green (2/4/2013)
Jeff Moden (2/1/2013)
anthony.green (2/1/2013)
ChrisM@Work (2/1/2013)
Or this:

SELECT
T1.IDCUST,
T1.IDINVC,
T2.AMTPAYM,
TotalPayment = SUM(AMTPAYM) OVER (PARTITION BY IDCUST, IDINVC)
FROM MyTable1_ARIBH T1
INNER JOIN MyTable2_ARTCP T2
ON T2.IDCUST = T1.IDCUST
AND T2.IDINVC = T1.IDINVC
WHERE T1.IDCUST = 'TOR829'
AND T1.IDINVC = '72124'



Doh, I keep forgetting you can OVER an aggregate function.


BWAA-HAAAA!!!! I used to OVER look it all the time!


Your just to cool for school Jeff


Not even close... I'm trying to quit smoking, again, and my jokes are really going to such for about the next 6 months because of it.


Good luck with the stopping, the other half is trying again as well, so I know what your going through.




Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1415710
Posted Tuesday, February 05, 2013 8:02 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 1:17 PM
Points: 8,641, Visits: 8,273
Jeff Moden (2/4/2013)

Not even close... I'm trying to quit smoking, again, and my jokes are really going to such for about the next 6 months because of it.


You can do it Jeff. It is damn hard!!! I have been smoke free for almost 9 years now and was a 2 pack a day smoker for about 25 years. The patch helped me, just take it off before bed.

Sorry to hijack this thread but I wanted to offer my support.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1415864
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse