0

I need something like this

SELECT
foreach(service IN (SELECT tbl_services.service_name FROM tbl_services))
    COUNT(CASE WHEN service=ANY(tbl_providers.provided_services) THEN 1 ELSE NULL END)
FROM tbl_providers;

Is there any specific solution for implementing this type of for each in sql? thanks

2
  • 1
    When you start thinking in "loops" in SQL you are doing something wrong. Commented Jul 11, 2018 at 10:41
  • Please edit your question and add some sample data and the expected output based on that data. Formatted text please, no screen shots. (edit your question - do not post code or additional information in comments) Commented Jul 11, 2018 at 10:49

1 Answer 1

1

Following query gives you same result (number of services provided by any provider):

SELECT count(distinct s.service)
FROM tbl_services s
INNER JOIN tbl_providers p ON p.provided_services @> ARRAY[s.service]::varchar[]
Sign up to request clarification or add additional context in comments.

3 Comments

thank you kind sir for your answer. p.provided_services is an array, including many services each provider able to provide. So it's not possible to join them.
@pouriadaneshvar would something like INNER JOIN tbl_providers p ON s.service = ANY(p.provided_services) work?
@pouriadaneshvar please, see updated query above. Made if provided_services array contains service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.