September 27, 2012 at 7:13 am
Hi
I have
SELECT
[PolicyId]= MB.POLICY_ID
,[SwitchValue]= ISNULL(SUM(MB.TOTAL_AMOUNT), 0.00)
,[RequestedAmount]= ISNULL(SUM(BF.FEE_AMOUNT), 0.00)
,[AmountPaid]= ISNULL(SUM(BF.GLOBAL_FEE_TAKEN), 0.00)
FROM PR_MONEY_BFN MB
LEFT JOIN PR_BFN_FEES BF WITH (NOLOCK)
ON MB.POLICY_ID = BF.POLICY_ID
WHERE
-- ISNULL(BF.GLOBAL_FEE_TAKEN, 0.00) < ISNULL(BF.FEE_AMOUNT, 0.00)
--AND BF.GLOBAL_FEE_TAKEN < BF.FEE_AMOUNT
I don't get any data when I try to use: "BF.GLOBAL_FEE_TAKEN < BF.FEE_AMOUNT"
but when I use: "ISNULL(BF.GLOBAL_FEE_TAKEN, 0.00) < ISNULL(BF.FEE_AMOUNT, 0.00)" I get data back but with zero values for [AmountPaid]
September 27, 2012 at 7:19 am
What is the value in the two columns in the where clause, I am guessing that one or both are NULL due to you wrapping them in ISNULL in the SELECT and therefore you are running into the UNKNOWN clause.
The where predicate needs to return TRUE or FALSE and as you are comparing NULLS you get UNKNOWN as it might be TRUE it might be FALSE so SQL leaves them out.
But when you put the ISNULL clause around them as you are setting the values to a actual value SQL will always return TRUE or FALSE not UNKNOWN
September 27, 2012 at 7:24 am
[RequestedAmount] [AmountPaid]
32700 10200
17000 0
150 0
2425 1425
September 27, 2012 at 7:26 am
So 0 for a Ammount paid, so is that 0 due to the ISNULL or is that 0 due to the sum of the values?
September 27, 2012 at 7:33 am
I changed ny code to
,[SwitchValue]= SUM(MB.TOTAL_AMOUNT)
,[RequestedAmount]= SUM(BF.FEE_AMOUNT)
,[AmountPaid]= SUM(BF.GLOBAL_FEE_TAKEN)
to I get the NULL values where I was getting Zero values, so it was due to ISNULL
September 27, 2012 at 7:34 am
Well there you go then, you where running into the UNKNOWN section of the predicate and in turn it was filtering them out as if a value is NULL SQL cannot say if NULL < 100 = TRUE or FALSE, therefore SQL removes it from the data set.
Adding the ISNULL to the where clause made it say 0.00 < 100 = TRUE so the row is returned.
September 27, 2012 at 7:45 am
So then, I have a situation where I have 10200 < 32700, why don't I get both value?
September 27, 2012 at 7:48 am
Your where clause is not SUMming the data, so while 10200 < 32700 = TRUE, that is not what is actually being checked in the where.
September 27, 2012 at 7:54 am
How do I then filter with values where [AmountPaid] is less than [RequestedAmount]
September 27, 2012 at 7:56 am
WHERE
ISNULL(SUM(BF.GLOBAL_FEE_TAKEN), 0.00) < ISNULL(SUM(BF.FEE_AMOUNT), 0.00)
September 27, 2012 at 8:05 am
Thank you.
September 27, 2012 at 8:11 am
anthony.green (9/27/2012)
WHEREISNULL(SUM(BF.GLOBAL_FEE_TAKEN), 0.00) < ISNULL(SUM(BF.FEE_AMOUNT), 0.00)
Shouldn't that be on the HAVING clause (after the GROUP BY)?
September 27, 2012 at 8:12 am
You can't put a SUM into a where clause.
HAVING ISNULL(SUM(BF.GLOBAL_FEE_TAKEN), 0.00) < ISNULL(SUM(BF.FEE_AMOUNT), 0.00)
With the appropriate group by, of course.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
September 27, 2012 at 8:14 am
Yep thanks both, case of the afternoon slump, need more coffee to keep going.
September 27, 2012 at 8:18 am
I have already figured that one out, so I have
HAVING SUM(ISNULL(BF.GLOBAL_FEE_TAKEN, 0.00)) < SUM(ISNULL(BF.FEE_AMOUNT, 0.00))
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply