DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`wrongEqualizatedClient`()
BEGIN
	SELECT clientFk, c.name, c.isActive, c.isTaxDataChecked, count(ie) as num
	FROM vn.client c
		JOIN 
		(
		SELECT DISTINCT 
				`a`.`clientFk` ,
				 a.isEqualizated = 0 as ie
				
			FROM
				`vn`.`address` `a`
		) sub ON sub.clientFk = c.id
		WHERE c.hasToInvoiceByAddress = FALSE
        
		GROUP BY clientFk
		HAVING num > 1
	UNION ALL
    SELECT DISTINCT c.id, c.name, c.isActive, c.isTaxDataChecked,1
			FROM
				`vn`.`address` `a`
				JOIN `vn`.`client` `c` ON `c`.`id` = `a`.`clientFk`
			WHERE ABS(a.isEqualizated) <> ABS(c.isEqualizated)
            AND c.hasToInvoiceByAddress = FALSE;
END$$
DELIMITER ;
