The other item to key off is DATACODE.
Otherwise you won't remove Releases of these groups.
The best way to determine ADHOCGROUPS (not just the ones with Associated Data):
SELECT * FROM PLUGIN WHERE TYPE = 8 AND DATACODE IS NOT NULL AND DATACODE <> ''
When an Adhoc Group is created, it sets the DATACODE to the PLUGINID.
When releases are created, the releases set the BASEDON to match the PLUGINID of the Original Group (this is the same for all PLUGINS), but in addition, on ADHOC groups the DATACODE is set to the PLUGINID of the Original group.
Also, I do recall that when you Copy ADHOC groups, the BASEDON may not be set (as this is a new group path), but if it is an ADHOC group, the DATACODE field gets populated (again, with the original PLUGINID).
Any entities added to the Groups get added to the ADHOC group table with the DATACODE as the GROUPID.