The code below gives me something similar to the table below. What I am looking to do is only return the PROVID that has the max count per PATID.
SELECT PAT_ID AS PATID
, VISIT_PROV_ID AS PROVID
, COUNT(*) AS PROVCOUNT
FROM PAT_ENC
GROUP BY PAT_ID, VISIT_PROV_ID
ORDER BY PAT_ID, COUNT(*) DESC
| PATID | PROVID | PROVCOUNT |
|---|---|---|
| 1 | 3 | 1 |
| 2 | 4 | 6 |
| 2 | 3 | 2 |
| 2 | 8 | 1 |
| 3 | 4 | 6 |
| 4 | 1 | 8 |
| 4 | 2 | 3 |
The table below would be the desired result based on the same data from the previous table.
| PATID | PROVID | PROVCOUNT |
|---|---|---|
| 1 | 3 | 1 |
| 2 | 4 | 6 |
| 3 | 4 | 6 |
| 4 | 1 | 8 |