Reputation: 330
Hello I am trying to get the right SUM from my query, and i know that this works:
SELECT SUM(AMOUNT) From IncomingInvoiceLine inner Join [File] ON IncomingInvoiceLine.FILENUMBER = [File].FILENUMBER WHERE [File].RELATIONCODE = '12TU01'
But this is the amount without the currency being calculated so i tried this:
SELECT fmsTotalAmountIncoming INTO TempIncomingAmounts FROM (
SELECT SUM(CASE WHEN fms1.currency != 'EUR'
THEN fms1.amount * fms1.rate
ELSE ISNULL(fms1.amount, 0) END ) fmsTotalAmountIncoming
FROM [fms].[dbo].[IncomingInvoiceLine] fms1
WHERE fms1.RELATIONCODE = '12TU01'
) a
SELECT fmsTotalAmountIncoming FROM [fms].[dbo].[TempIncomingAmounts]
DROP TABLE [fms].[dbo].[TempIncomingAmounts]
And this does not return the right result, it returns NULL while the first query returns:
8145.46
Yet I can't figure out why the query with the currency being converted returns NULL. It should return
8106.546
(I first made this in vb.net and then wanted to make it faster by writing a stored procedure).
Does anyone see why it does this?
Upvotes: 0
Views: 126
Reputation: 330
The problem in the end was my stupidity, i was trying to get it directly from the IncomingInvoiceLine, while i needed to inner join on file. Now it works, using this:
SELECT fmsTotalAmountIncoming INTO TempIncomingAmounts FROM (
SELECT SUM(CASE WHEN fms1.currency != 'EUR'
THEN fms1.amount * fms1.rate
ELSE ISNULL(fms1.amount, 0) END) fmsTotalAmountIncoming
FROM [fms].[dbo].[file] f
INNER JOIN [fms].[dbo].[incominginvoiceline] fms1 ON
fms1.filenumber = CONVERT(NVARCHAR, f.filenumber)
WHERE f.RELATIONCODE = @RelationCode
) a
Thank you for all the help!
Upvotes: 0
Reputation: 1
may be one of the null value in multiplication. You can figure out following way
ISNULL(fms1.amount * fms1.rate, 0)
Upvotes: 0
Reputation: 455
I Think your multiplication may causing the problem. Because i have tried the same problem with some dummy data and it is giving the the accurate result means it is not giving any null value
SELECT fmsTotalAmountIncoming INTO TempIncomingAmounts FROM (
SELECT SUM(CASE WHEN fms1.currency != 'EUR'
THEN fms1.amount * 0.5
ELSE ISNULL(fms1.amount, 0) END ) fmsTotalAmountIncoming
FROM [fms].[dbo].[IncomingInvoiceLine] fms1
WHERE fms1.RELATIONCODE = '12TU01'
) a
SELECT fmsTotalAmountIncoming FROM [fms].[dbo].[TempIncomingAmounts]
DROP TABLE [fms].[dbo].[TempIncomingAmounts]
If the above query results the correct result then its the multipilcation problem.
Upvotes: 1
Reputation: 611
It's because our fms1.amount * fms1.rate.
Rate is informat X.XX, cast it to the same format as amount
.
It can be conversely. Add more info (data type of amount and data type of rate) for more detailed answer.
Upvotes: 0