challenging query to reorder rows on condition

  • Hi,

    Some background:

    I have a school. School have classrooms. Classrooms are divided into various sections (Section A, Section B and so on) . Sections have subsections. Every student is allocated a rollnumber(unique) in that subsection/section. However student is also given a choice to specify his own roll(DesiredRoll) in that section. If more than one student choose the same desired roll no in that subsection/section, there is a [TrackingNo] field that then starts keeping a count.

    I have two tables : Students and StuHistory. The structure of the Student table is as follows :

    IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Student]') AND type in (N'U'))

    BEGIN

    CREATE TABLE [dbo].[Student](

     [RID] [int] NOT NULL,

     [Class] [int] NULL,

     [Section] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,

     [SubSection] [int] NULL,

     [RollNo] [int] NULL,

     [DesiredRoll] [int] NULL,

     [TrackingNo] [int] NULL,

     [Original_rollno] [int] NULL,

     [StudentStatus] [int] NULL

    )

    END

    GO

    For the first unique desired roll no in that subsection/section the tracking no is always 0.

    [StudentStatus] represents the following : (-1 for deleted, 0 for edited, 1 for newly inserted).

    After every fortnight, i have to run a batchquery that does the following:

    1. all students marked with -1 are moved to a table called StuHistory which has the same structure as that of Student.

    2. Now oncethe -1 status students are moved, there will be a gap in the roll no. I want to reallocate the rollnos now, where rollnos = desired roll no taking into consideration the trackingno

    So if 4 students have chosen the desired roll no as 5 and their current roll no is scattered in a subsection lets say 7, 10, 14,16, then while rearranging they will be together(grouped by subsection/section) and will be allocated roll no's 5,6,7,8. The other students will be moved down based on their desired roll nos. Over here i have to also fill the gaps caused because of the students who were deleted.

    How do i write query for this? I have been struggling.

    Script :

    INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo], [Original_rollno],  [StudentStatus] )

    VALUES (1, 1, N'A', 1, 1, 1, 0, 0, 1)

    INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo], [Original_rollno],  [StudentStatus] )

    VALUES (2, 1, N'A', 1, 2, 2, 0, 0, 1)

    INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo], [Original_rollno],  [StudentStatus] )

     VALUES (3, 1, N'A', 1, 3, 1, 1,0,1)

    INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo], [Original_rollno],  [StudentStatus] )

    VALUES (4, 1, N'A', 12, 1, 1, 0,-1)

    INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo], [Original_rollno],  [StudentStatus] )

    VALUES (5, 1, N'A', 12, 2, 1, 1, 0, 1)

    INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo], [Original_rollno],  [StudentStatus] )

    VALUES (6, 1, N'A', 12, 3, 2, 0, 0, 1)

     

    INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo], [Original_rollno],  [StudentStatus] )

     VALUES (7, 1, N'B', 5, 1, 3, 0, 0, 1)

    INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo], [Original_rollno],  [StudentStatus] )

    VALUES (8, 1, N'B', 5, 2, 3, 1, 0 ,1)

    INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo], [Original_rollno],  [StudentStatus] )

    VALUES (9, 1, N'B', 5, 3, 3, 2, 0, 1)

    INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo], [Original_rollno],  [StudentStatus] )

    VALUES (10, 1, N'B', 5, 4, 2, 0, 0, 1)

    INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo], [Original_rollno],  [StudentStatus] )

    VALUES (11, 1, N'B', 5, 5, 2, 1, 0, 1)

    INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo], [Original_rollno],  [StudentStatus] )

    VALUES (12, 1, N'B', 10, 1, 1, 0, 0, 1)

    INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo], [Original_rollno],  [StudentStatus] )

    VALUES (13, 1, N'B', 10, 2, 1, 1, 0, 1 )

    INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo], [Original_rollno],  [StudentStatus] ) 

    VALUES (14, 1, N'B', 10, 3, 1, 2, 0, -1)

    INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo], [Original_rollno],  [StudentStatus] )

    VALUES (15, 1, N'B', 10, 4, 2, 0, 0, 1)

    Thanks.

  • Asked and answered here

    http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=88129

     


    N 56°04'39.16"
    E 12°55'05.25"

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply