Skip to content
Advertisement

Select distinct rows where all values are in group

I have a table that stores a one to many relationsion (caseid to code). It is possible for one caseid to have many codes.

I would like to select all rows where all the codes for a given caseid are contained within a group of codes. If a caseid is associated with any code that is not in this group, then exclude it, regardless of if all the other codes are in the group.

I would then like to build a table where each unique caseid has a single row and four Boolean columns (one for each code I am looking for) denoting if that code is present.

Here is my query so far:

An example table may be:

It should come out as:

Advertisement

Answer

You can use conditional aggregation as follows:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement