If you use ed rather than sed you can use a regex address with negative offset:
g/PRIMARY/-1 s/,$/)/ Ex.
$ printf ''g/PRIMARY/-1 s/,$/)/\n,p\nq\n' | ed -s file ID_SOUR_CALENDAR BIGINT NOT NULL DEFAULT 0 COMPRESS 0 ) UNIQUE PRIMARY INDEX ( CALENDAR_DATE ); ID , ID_SOUR ,) PRIMARY INDEX ( CALENDAR_DATE ); or (for in-place editing)
printf ''g/PRIMARY/-1 s/,$/)/\nwq\n' | ed -s file The replacement will only match the first instanceALL instances of /PRIMARY/ unless preceded by- if you only want to replace the first, remove the g modifier.