SELECT COUNT(DISTINCT "hosts"."id")
FROM "hosts"
LEFT OUTER JOIN "models" ON "models"."id" = "hosts"."model_id"
LEFT OUTER JOIN "hostgroups" ON "hostgroups"."id" = "hosts"."hostgroup_id"
LEFT OUTER JOIN "nics" ON "nics"."host_id" = "hosts"."id"
AND "nics"."primary" = 't'
LEFT OUTER JOIN "domains" ON "domains"."id" = "nics"."domain_id"
LEFT OUTER JOIN "realms" ON "realms"."id" = "hosts"."realm_id"
LEFT OUTER JOIN "environments" ON "environments"."id" = "hosts"."environment_id"
LEFT OUTER JOIN "architectures" ON "architectures"."id" = "hosts"."architecture_id"
LEFT OUTER JOIN "compute_resources" ON "compute_resources"."id" = "hosts"."compute_resource_id"
LEFT OUTER JOIN "images" ON "images"."id" = "hosts"."image_id"
LEFT OUTER JOIN "operatingsystems" ON "operatingsystems"."id" = "hosts"."operatingsystem_id"
LEFT OUTER JOIN "nics" "primary_interfaces_hosts" ON "primary_interfaces_hosts"."host_id" = "hosts"."id"
AND "primary_interfaces_hosts"."primary" = 't'
LEFT OUTER JOIN "nics" "interfaces_hosts" ON "interfaces_hosts"."host_id" = "hosts"."id"
LEFT OUTER JOIN "nics" "primary_interfaces_hosts_join" ON "primary_interfaces_hosts_join"."host_id" = "hosts"."id"
AND "primary_interfaces_hosts_join"."primary" = 't'
LEFT OUTER JOIN "subnets" ON "subnets"."id" = "primary_interfaces_hosts_join"."subnet_id"
LEFT OUTER JOIN "nics" "provision_interfaces_hosts" ON "provision_interfaces_hosts"."host_id" = "hosts"."id"
AND "provision_interfaces_hosts"."provision" = 't'
LEFT OUTER JOIN "discovery_rules" ON "discovery_rules"."id" = "hosts"."discovery_rule_id"
WHERE "hosts"."type" IN ('Host::Managed')
    AND (("hosts"."name" ILIKE '%asdf%'
          OR "hosts"."comment" ILIKE '%asdf%'
          OR "models"."name" ILIKE '%asdf%'
          OR "hostgroups"."name" ILIKE '%asdf%'
          OR "hostgroups"."title" ILIKE '%asdf%'
          OR "hostgroups"."title" ILIKE '%asdf%'
          OR "domains"."name" ILIKE '%asdf%'
          OR "realms"."name" ILIKE '%asdf%'
          OR "environments"."name" ILIKE '%asdf%'
          OR "architectures"."name" ILIKE '%asdf%'
          OR "compute_resources"."name" ILIKE '%asdf%'
          OR "images"."name" ILIKE '%asdf%'
          OR "operatingsystems"."name" ILIKE '%asdf%'
          OR "operatingsystems"."description" ILIKE '%asdf%'
          OR "operatingsystems"."title" ILIKE '%asdf%'
          OR "operatingsystems"."major" ILIKE '%asdf%'
          OR "operatingsystems"."minor" ILIKE '%asdf%'
          OR "nics"."ip" ILIKE '%asdf%'
          OR "nics"."ip" ILIKE '%asdf%'
          OR "nics"."mac" ILIKE '%asdf%'
          OR "subnets"."network" ILIKE '%asdf%'
          OR "subnets"."name" ILIKE '%asdf%'
          OR "hosts"."uuid" ILIKE '%asdf%'
          OR "nics"."mac" ILIKE '%asdf%'
          OR "operatingsystems"."name" ILIKE '%asdf%'
          OR "operatingsystems"."description" ILIKE '%asdf%'
          OR "operatingsystems"."title" ILIKE '%asdf%'
          OR "operatingsystems"."major" ILIKE '%asdf%'
          OR "operatingsystems"."minor" ILIKE '%asdf%'
          OR "discovery_rules"."name" ILIKE '%asdf%'))
    AND (last_report < '2016-07-21 16:55:26.923487'
         AND enabled != 'f');
