Skip to content
Advertisement

SQL group by ID for multiple IDs using case when statements

I started with a list of patients with multiple codes (at multiple times of year) and need to split the patients into groups based on if they have a code or combo of codes and those that don’t qualify are excluded from the list. I have already created flags (0,1) for each set of codes. But the problem is that a patient can qualify or disqualify on another row. What I’d like is one row per patient which I can then determine the appropriate group per patient. Below is the two ways I’ve tried but I can’t figure out how to roll up by ID and/or the new column.

1st code I tried:

2nd code I tried:

HAVE

WANT

Advertisement

Answer

You seem to want conditional aggregation. Your question is a little hard to follow, but the idea is:

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