-- Sources-DeleteAfterSplit.sql
/*
Deletes lumped sources and related citations, image tags, webtags, repository tags
after using Sources-CreateExtremelySplitFromCitations.sql
2013-06-30 Tom Holden ve3meo
*/

-- lumped sources that have been split
DELETE FROM SourceTable
WHERE SourceID IN 
(
 SELECT DISTINCT SourceID 
 FROM xSplitSourceTable
 )
;

-- citations of the now deleted lumped sources
DELETE FROM CitationTable
WHERE SourceID
NOT IN
(
 SELECT SourceID
 FROM SourceTable
 )
;

-- Image Tags
DELETE FROM MediaLinkTable
WHERE OwnerType = 4 -- Citation tag
AND OwnerID
NOT IN
(
 SELECT CitationID
 FROM CitationTable
 )
;

DELETE FROM MediaLinkTable
WHERE OwnerType = 3 -- Source tag
AND OwnerID
NOT IN
(
 SELECT SourceID
 FROM SourceTable
 )
;

-- WebTags
DELETE FROM URLTable
WHERE OwnerType = 4 -- Citation tag
AND OwnerID
NOT IN
(
 SELECT CitationID
 FROM CitationTable
 )
;

DELETE FROM URLTable
WHERE OwnerType = 3 -- Source tag
AND OwnerID
NOT IN
(
 SELECT SourceID
 FROM SourceTable
 )
;

-- Repository tags
DELETE FROM AddressLinkTable
WHERE OwnerType = 3 -- Source tag
AND OwnerID
NOT IN
(
 SELECT SourceID
 FROM SourceTable
 )
;
-- END


