Marius Vasile

Marius Vasile

  • 584
  • 1.8k
  • 138.6k

Insert Into table from multiple select queries

Jul 7 2024 5:43 AM

I have a table with multiple questions from different domains, example: Math, Physics, Chemistry and I have a query to select questions from that table and write them in a new table. What I have is selecting random questions from table with:

BEGIN
	INSERT INTO tblTestDetailSSM (IdIntrebare, IdUserTest) 
	SELECT TOP 10 ti.IdIntrebare AS IdIntrebare, @IdUserTest 
	FROM (SELECT TOP 100 * FROM tblIntrebare ORDER BY NEWID()) ti JOIN tblProcedura tp ON ti.IdProcedura = tp.IdProcedura WHERE tp.Complexitate = 'GENERAL' AND tp.Specialitate = 'SSM';
END

where tp.Specialitate = 'SSM' includes all domains. I want to be able to select random questions, one from each domain and add it to tblTestDetailSSM. So that means multiple select query FROM which to select top 10. How do I do that?


Answers (4)