Modified Tag query to avoid duplicated ids. Closes #427

Modified sql query to avoid duplicated ids.
Fixes #427