Self Relationship

Asked

Viewed 144 times

0

I have a schedule of medications.

In it I have:

  • code of the medicinal product (PK)
  • description
  • code ans

There is the possibility that we have more than one drug code for the same ans code.

I need to make an appointment to bring all the medicines that have the same code ans and the code of the different medicine.

Oracle use.

1 answer

1


In this particular case it was not clear whether it really is about self relationship. Maybe you should change your question a little to give more details.

But speaking of your consultation, try using GROUP BY with the clasp HAVING.

SELECT codigo_medicamento, codigo_ans
  FROM medicamentos
 GROUP BY codigo_ans HAVING COUNT(codigo_medicamento) > 1

We count how many codigo_medicamento different we have for each codigo_ans and filter only those in which the count is greater than 1.

I can’t test with ORACLE, but try and return in case of error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.