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:
SQL
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';
ENDwhere 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?


Sarthak VarshneyRank #46 · 32K reputationPosted Jul 7, 2024, 5:00 AM EDT
To select random questions from each domain and insert them into the
tblTestDetailSSMtable, you can use a combination of Common Table Expressions (CTEs) or derived tables to gather a random question from each domain first, and then perform the final insert operation.Here is an example of how you might achieve this:
BEGIN -- Select random questions from each domain WITH RandomMathQuestions AS ( SELECT TOP 1 ti.IdIntrebare FROM tblIntrebare ti JOIN tblProcedura tp ON ti.IdProcedura = tp.IdProcedura WHERE tp.Complexitate = 'GENERAL' AND tp.Specialitate = 'Math' ORDER BY NEWID() ), RandomPhysicsQuestions AS ( SELECT TOP 1 ti.IdIntrebare FROM tblIntrebare ti JOIN tblProcedura tp ON ti.IdProcedura = tp.IdProcedura WHERE tp.Complexitate = 'GENERAL' AND tp.Specialitate = 'Physics' ORDER BY NEWID() ), RandomChemistryQuestions AS ( SELECT TOP 1 ti.IdIntrebare FROM tblIntrebare ti JOIN tblProcedura tp ON ti.IdProcedura = tp.IdProcedura WHERE tp.Complexitate = 'GENERAL' AND tp.Specialitate = 'Chemistry' ORDER BY NEWID() ), -- Repeat for other domains as needed RandomOtherQuestions AS ( SELECT TOP 1 ti.IdIntrebare FROM tblIntrebare ti JOIN tblProcedura tp ON ti.IdProcedura = tp.IdProcedura WHERE tp.Complexitate = 'GENERAL' AND tp.Specialitate = 'Other' ORDER BY NEWID() ) -- Final selection combining all random questions SELECT IdIntrebare, @IdUserTest AS IdUserTest INTO #TempQuestions FROM ( SELECT IdIntrebare FROM RandomMathQuestions UNION ALL SELECT IdIntrebare FROM RandomPhysicsQuestions UNION ALL SELECT IdIntrebare FROM RandomChemistryQuestions UNION ALL SELECT IdIntrebare FROM RandomOtherQuestions ) AS CombinedQuestions -- Insert into tblTestDetailSSM INSERT INTO tblTestDetailSSM (IdIntrebare, IdUserTest) SELECT IdIntrebare, IdUserTest FROM #TempQuestions DROP TABLE #TempQuestions ENDExplanation:
RandomMathQuestions,RandomPhysicsQuestions,RandomChemistryQuestions, etc.) selects a random question from each specified domain usingORDER BY NEWID().UNION ALLoperation combines the results from all CTEs.#TempQuestionsto facilitate the final insert.tblTestDetailSSM.This approach ensures that you get one random question from each specified domain and insert them into the target table. If you need more questions per domain, adjust the
TOPclause accordingly.Marius VasileRank #785 · 2.9K reputationPosted Jul 7, 2024, 5:03 AM EDT
Thank you Sarthak, that was awesome solution!
Sarthak VarshneyRank #46 · 32K reputationPosted Jul 7, 2024, 5:05 AM EDT
You're welcome!
Siva VRank #658 · 3.6K reputationPosted Jul 7, 2024, 5:10 AM EDT
As per your real time example random questions from different domains (Maths, physics, chemistry.. etc).
You can use CTE with Union all using Row_Number() window function in SQL.
;WITH RankedQuestions AS ( SELECT QuestionID, QuestionText, Domain, ROW_NUMBER() OVER (PARTITION BY Domain ORDER BY NEWID()) AS RowNum FROM ( SELECT QuestionID, QuestionText, 'Math' AS Domain FROM MathQuestions UNION ALL SELECT QuestionID, QuestionText, 'Physics' AS Domain FROM PhysicsQuestions UNION ALL SELECT QuestionID, QuestionText, 'Chemistry' AS Domain FROM ChemistryQuestions ) AS AllQuestions ) INSERT INTO tblTestDetail (QuestionID, QuestionText, Domain) SELECT QuestionID, QuestionText, Domain FROM RankedQuestions WHERE RowNum = 1;Here RankedQuestions will
Hope this way of implementation will help you to solve your requirement.