USE DataMiningDW
GO

IF EXISTS (SELECT * FROM sys.tables WHERE name='Dictionary') 
TRUNCATE TABLE dbo.Dictionary
GO
IF EXISTS (SELECT * FROM sys.tables WHERE name='EmailsFragments') 
TRUNCATE TABLE dbo.EmailsFragments
GO

SELECT COUNT(*)
FROM dbo.EmailsFragments AS F 
JOIN dbo.EMails AS E ON E.ID=F.ID

SELECT COUNT(*)
FROM dbo.EmailsFragments AS F 
JOIN dbo.EMails AS E ON E.ID=F.ID
WHERE E.Category='SPAM'

SELECT 371*1./22485*100
GO

SELECT E.ID, Term, Frequency, NEWID() as n
FROM dbo.EmailsFragments AS F 
JOIN dbo.EMails AS E ON E.ID=F.ID
WHERE E.Category='SPAM'
UNION ALL
SELECT TOP 2 PERCENT E.ID, Term, Frequency, NEWID() 
FROM dbo.EmailsFragments AS F 
JOIN dbo.EMails AS E ON E.ID=F.ID
WHERE E.Category<>'SPAM'
ORDER BY NEWID();
GO