Sum Total Amount per InvoiceNo in SQL Server 2008 R2 query

I want to get the Total Amount per InvoiceNo.

I tried to do the Sum query but the results is not wrong.

SELECT 
      [hdr]
      ,[VendorID]
      ,[InvoiceDate]
      ,[InvoiceNo]
      ,[Location]
      ,[ItemNo ]
      ,sum([AmtDue])
      ,[Taxes]
      ,sum([TTLAmt])
      ,[SeqNo]
  FROM [dbo].[Header]
  Group by 
  [hdr]
      ,[VendorID]
      ,[InvoiceDate]
      ,[InvoiceNo]
      ,[Location]
      ,[ItemNo ]
      ,[AmtDue]
      ,[Taxes]
      ,[TTLAmt]
      ,[SeqNo]

Open in new window


Example:
Table:
Hdr    VendorID     InvoiceDate                   InvoiceNo               Location    ItemNo     AmtDue       Taxes      TTLAmt        SeqNo

Hdr     7143               2015-05-26             457214      24166           28806      0.00                         0.00      0.00         1
Hdr     7143       2015-05-26            457214      24166           28806     18.36                         0.00      18.36         1
hdr      7143       2015-05-26            457214      24166           28806     45.90                         0.00      45.90         1
hdr      7143       2015-05-26            457712      24158           23802      0.00                         0.00      0.00         1
hdr      7143       2015-05-26            457712      24158           23802      9.18                         0.00      9.18         1
hdr      7143       2015-05-26            457712      24158           23802      27.54                         0.00      27.54         1
hdr      7143       2015-05-26            457712      24158           23802       110.16                          0.00      110.16         1
hdr      7143       2015-05-26            457726      24189           23804       0.00                          0.00      0.00         1
hdr      7143       2015-05-26            457726      24189           23804       15.67                          0.00      15.67         1
hdr      7143       2015-05-26            457726      24189           23804       36.72                           0.00      36.72         1
hdr      7143       2015-05-26            457726      24189           23804       91.80                           0.00      91.80         1

Results:
Hdr    VendorID     InvoiceDate      InvoiceNo      Location    ItemNo     AmtDue    Taxes      TTLAmt        SeqNo

Hdr    7143                2015-05-26       457214      24166           28806        64.26           0.00      64.26         1
Hdr     7143               2015-05-26       457712      24158           23802        146.88    0.00      146.88         1
Hdr     7143       2015-05-26      457726      24189           23804        144.19    0.00      144.19         1
Queennie LAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
"I tried to do the Sum query but the results is not wrong." If is not wrong what is the issue?
By your example I can't see any issue. Maybe if you add the expected result we can help you better.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Looks correct to me.  InvoiceNo=457214 has three rows where TTLAmt = 0.00, 18.36, and 45.90, and their sum is 64.26.

So, if 'not wrong' means 'right', then you're good.

Unless 'not wrong' really means 'wrong', in which you need to define for us what 'right' is.
0
dsackerContract ERP Admin/ConsultantCommented:
I assume you meant "now wrong" instead of "not wrong". What totals should be showing up in the results?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Queennie LAuthor Commented:
I cannot get the total amount per invoice no. It does not show the right results.

this is the results of my query:

Hdr    VendorID     InvoiceDate                   InvoiceNo               Location    ItemNo     AmtDue       Taxes      TTLAmt        SeqNo

 Hdr     7143               2015-05-26             457214      24166           28806      0.00                         0.00      0.00         1
 Hdr     7143       2015-05-26            457214      24166           28806     18.36                         0.00      18.36         1
 hdr      7143       2015-05-26            457214      24166           28806     45.90                         0.00      45.90         1
 hdr      7143       2015-05-26            457712      24158           23802      0.00                         0.00      0.00         1
 hdr      7143       2015-05-26            457712      24158           23802      9.18                         0.00      9.18         1
 hdr      7143       2015-05-26            457712      24158           23802      27.54                         0.00      27.54         1
 hdr      7143       2015-05-26            457712      24158           23802       110.16                          0.00      110.16         1
 hdr      7143       2015-05-26            457726      24189           23804       0.00                          0.00      0.00         1
 hdr      7143       2015-05-26            457726      24189           23804       15.67                          0.00      15.67         1
 hdr      7143       2015-05-26            457726      24189           23804       36.72                           0.00      36.72         1
 hdr      7143       2015-05-26            457726      24189           23804       91.80                           0.00      91.80         1
0
Queennie LAuthor Commented:
The results is wrong.
0
Queennie LAuthor Commented:
My query did not show the total amount per invoice no.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Queenie - Define 'right' and 'wrong'.  Mind readers we ain't.

If you're ONLY trying to get the sum and no other columns, try this.  What might be confusing you is that if you include all other columns, every time some columns are not unique (such as AmtDue) then that is going to cause multiple rows of InvoiceNo.
SELECT InvoiceNo, SUM(TTLAmount)  as InvoiceSum
FROM Header
GROUP BY InvoiceNo
ORDER BY InvoiceNo

Open in new window

0
dsackerContract ERP Admin/ConsultantCommented:
If you run just a bare query for only the invoice numbers, does this yield the correct results:

SELECT  InvoiceNo,
        SUM(TTLAmt) AS TotalAmount,
        SUM(AmtDue) AS TotalDue
FROM    Header
GROUP BY InvoiceNo

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
Ok, then try this query:
WITH CTE_Totals (InvoiceNo, TotalAmtDue, TotalTTLAmt
AS
	(SELECT [InvoiceNo], sum([AmtDue]), sum([TTLAmt])
	FROM [dbo].[Header]
	GROUP BY [InvoiceNo])
SELECT 
      h.[hdr]
      ,h.[VendorID]
      ,h.[InvoiceDate]
      ,h.[InvoiceNo]
      ,h.[Location]
      ,h.[ItemNo ]
      ,t.[TotalAmtDue]
      ,h.[Taxes]
      ,t.[TTLAmt]
      ,h.[SeqNo]
FROM [dbo].[Header] h
	INNER JOIN CTE_Totals t ON h.[InvoiceNo] = t.[InvoiceNo]

Open in new window

0
Queennie LAuthor Commented:
The results should be showing below:

 Hdr    VendorID     InvoiceDate      InvoiceNo   Location    ItemNo     AmtDue    Taxes      TTLAmt        SeqNo

 Hdr    7143             2015-05-26       457214        24166           28806        64.26      0.00         64.26               1
 Hdr     7143            2015-05-26       457712        24158           23802        146.88    0.00        146.88             1
 Hdr     7143            2015-05-26       457726        24189           23804        144.19    0.00        144.19             1
0
dsackerContract ERP Admin/ConsultantCommented:
Change ...

,[Taxes]
To ...

, SUM([Taxes]
And take it out of your GROUP BY.

You may want to take your SeqNo out of both your SELECT and GROUP BY as well, unless you know your data well and are certain that will not have different values for any one invoice number.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
The above experts' T-SQL looks correct, so there has to be something else going on here.
In SSMS please go to the Header table and do a right-click > Script Table As > Create To > New Query Editor Window, and copy paste the results into this question.

Please avoid making statements like 'The results should be showing below', 'right', 'not right', and 'wrong'. as those will no longer be helpful.
0
Queennie LAuthor Commented:
Thank you for all your help.

It is correct syntax now.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Just for kicks and giggles can you copy-paste into this question what 'correct syntax' resulted in the correct answer?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.