Database function SELECT with optional argument
select d.field1, d.field2, COUNT(*) as record_count

WHERE d.fieldX = argument OR argument IS NULL

from my_table d
group by d.field1, d.field2;

Be aware that this may not always produce an efficient access plan. If you experience performance issues, you'll likely be better off executing one of the two query variants conditionally, something like
IF argument IS NULL THEN
   SELECT ... -- without conditions
ELSE 
   SELECT ... WHERE fieldX = argument
END IF