Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2009
    Location
    India
    Posts
    14

    Unhappy Unanswered: How to Assign ID Randomly

    Dear All,

    I am in great trouble. I am having 2 Tables: QuestionPaper & Employee.

    The QuestionPaper is having following details:
    Code:
    --------------------------------
    QuestionID | ExamID | SectionID
    --------------------------------
    232        |   18   |    20
    233        |   18   |    20
    234        |   18   |    20
    235        |   18   |    20
    236        |   18   |    20
    237        |   18   |    20
    238        |   18   |    20
    239        |   18   |    20
    240        |   18   |    20
    241        |   18   |    20
    262        |   18   |    21
    263        |   18   |    21
    264        |   18   |    21
    265        |   18   |    21
    266        |   18   |    21
    267        |   18   |    21
    268        |   18   |    21
    269        |   18   |    21
    270        |   18   |    21
    271        |   18   |    21
    292        |   18   |    22
    293        |   18   |    22
    294        |   18   |    22
    295        |   18   |    22
    296        |   18   |    22
    297        |   18   |    22
    298        |   18   |    22
    299        |   18   |    22
    300        |   18   |    22
    301        |   18   |    22
    --------------------------------
    and the Employee table contains:
    Code:
    ---------------
    ExamID | EmpID
    ---------------
      18   |  119
      18   |  219
      18   |  359
      18   |  297
      18   |  384
      18   |  245
      18   |  117
      18   |  269
    ---------------
    Now from the Above 2 tables, I have to create a T-SQL, which will assign 5 Unique QuestionID RANDOMLY as per SectionID and ExamID to each EmpID, e.g.
    Code:
    ----------------------------------------
    ExamID | EmpID | SectionID | QuestionID
    ----------------------------------------
      18   |  119  |    20     |    233
      18   |  119  |    20     |    237
      18   |  119  |    20     |    232
      18   |  119  |    20     |    241
      18   |  119  |    20     |    239
      18   |  119  |    21     |    262
      18   |  119  |    21     |    269
      18   |  119  |    21     |    265
      18   |  119  |    21     |    268
      18   |  119  |    21     |    263
      18   |  119  |    22     |    301
      18   |  119  |    22     |    299
      18   |  119  |    22     |    297
      18   |  119  |    22     |    295
      18   |  119  |    22     |    293
      18   |  219  |    20     |    236
      18   |  219  |    20     |    233
      18   |  219  |    20     |    241
      18   |  219  |    20     |    232
      18   |  219  |    20     |    239
      18   |  219  |    21     |    265
      18   |  219  |    21     |    266
      18   |  219  |    21     |    267
      18   |  219  |    21     |    268
      18   |  219  |    21     |    269
      18   |  219  |    22     |    292
      18   |  219  |    22     |    294
      18   |  219  |    22     |    296
      18   |  219  |    22     |    298
      18   |  219  |    22     |    300
    ----------------------------------------
    Hope I can make you understand my query, please help.

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    This is kind of random...

    SELECt TOP 5 QuestionID FROM YourTable ORDER BY NewID()
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  3. #3
    Join Date
    Apr 2012
    Posts
    3
    SELECT e.*,e1.SectionID,e1.QuestionID
    FROM Employee e
    CROSS APPLY (SELECT ROW_NUMBER() OVER (PARTITION BY SectionID ORDER BY NEWID()) AS Rn,*
    FROM QuestionPaper
    WHERE ExamID = e.ExamID
    )e1
    WHERE Rn <=5
    ORDER BY e.EmpID,e.ExamID,e1.SectionID,e1.QuestionID

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •