Page 1 of 1

Problem with calculated fields

Posted: 2020-04-13 18:47
by balfons
hi,

I'm using a calculated field in order to show the number of children a record has.

I have 2 tables:

- Parent: f_cont_improv_nonconformities
- Child: f_cont_improv_remedial_action

I'm using this query:

SELECT COUNT(*) FROM `f_cont_improv_nonconformities`
LEFT JOIN `f_cont_improv_remedial_action` ON `f_cont_improv_remedial_action`.`nonconformity_id`=`f_cont_improv_nonconformities`.`ID`
WHERE `f_cont_improv_nonconformities`.`ID`='%ID%'

the result is that when the parent has one or more children the query drops the correct result but... when there is no children... the query drops always the result: 1.

Any idea?

Re: Problem with calculated fields

Posted: 2020-04-15 10:18
by balfons
Hi,

I've been investigating a llitle bit.

My query always return a joined record where the part coming from the child table includes the ID with the null value.

I'm not an SQL expert but I think this join shouldn't include these cases.

So, I've modified my quey including the prevention for null values

SELECT COUNT(*) FROM `f_cont_improv_nonconformities`
LEFT JOIN `f_cont_improv_remedial_action` ON `f_cont_improv_remedial_action`.`nonconformity_id`=`f_cont_improv_nonconformities`.`ID`
WHERE `f_cont_improv_nonconformities`.`ID`='%ID%'
AND `f_cont_improv_remedial_action`.`ID` IS NOT NULL

Now it works as expected.

Thanks

Re: Problem with calculated fields

Posted: 2020-04-15 11:35
by jsetzer
Maybe you should use an INNER JOIN instead of a Left Join?

Re: Problem with calculated fields

Posted: 2020-04-15 18:01
by balfons
I'll try!!

Thanks

Re: Problem with calculated fields

Posted: 2020-04-16 17:56
by balfons
It works perfectly

Thanks!!