SELECTsd.ID,
sd.string,
sd.category,
ce.experiment
FROM @SampleData sd
INNER JOIN (
SELECT category,
CASEWHEN category % 7 = 0 THEN 'A'
WHEN category % 3 = 0 THEN 'B'
ELSE 'C'
END AS experiment
FROM @SampleData
) ce
ON sd.category = ce.category
ORDER BY ce.experiment, sd.string
Following on from Dwain's comments, another issue is non-atomic data, category is not just a primary key, it also denotes the experiment. If you have a CategoryExperiment relation, then it's easy street.