Showing posts with label facing. Show all posts
Showing posts with label facing. Show all posts

Thursday, March 22, 2012

A challenging query about grouping and order

Greetings !!

I am facing a challenge and hope some one can help me with the query.

I have a school. School have classrooms. Classrooms have students. Classrooms are divided into various sections (Section A, Section B and so on) .Every student is allocated a rollnumber in that section. When a student is transfered from one section to the other, there are gaps in the roll number of other students of the same section. For eg:

Section A

Student 1 Roll no. 1
Student 2 Roll No. 2
Student 3 Roll No 3

Student 4 Roll no 4 and so on.

Now lets say student 2 was transferred to section B, so now there is a gap as roll no 2 is missing in section A

So what should happen is that roll no need to be grouped together based on the section and the rest be readjusted accordingly. So when a student is removed, the roll nos of other students need to be adjusted accordingly. In our case, student 3 will get a roll no of 2, student 4 will have roll no of 3 and so on.

How do I write a query for this. I need to run this as a batch process every fortnight.

Thanks

Suppose if the Roll No is not a foriegn key for other tables then I will agree with your design.

Solution for SQL Server 2005,

Code Snippet

Create Table #data (

[RId] int ,

[Class] int ,

[Section] Char ,

[RollNo] int

);

Insert Into #data Values('1','1','A','1');

Insert Into #data Values('2','1','A','2');

Insert Into #data Values('3','1','A','3');

Insert Into #data Values('4','1','A','4');

Insert Into #data Values('5','1','A','5');

Insert Into #data Values('6','1','A','6');

Insert Into #data Values('7','1','B','1');

Insert Into #data Values('8','1','B','2');

Insert Into #data Values('9','1','B','3');

Insert Into #data Values('10','1','B','4');

Insert Into #data Values('11','1','B','5');

Insert Into #data Values('12','1','B','6');

Insert Into #data Values('13','1','B','7');

Insert Into #data Values('14','1','B','8');

Insert Into #data Values('15','1','B','9');

--Transfer the Student 4 from A section to B section

Update #data Set RollNo=10, Section='B' Where Section='A' And RollNo=4

--To fill the current gap

;WITH CTE

as

(

Select * , Row_Number() OVER (Partition By Class,Section Order By RollNo) NewRollNo

from #data

)

Update

CTE

Set

RollNo = NewRollNo

Where

RollNo <> NewRollNo

|||

For SQL Server 2000,

Code Snippet

Create Table #data (

[RId] int ,

[Class] int ,

[Section] Char ,

[RollNo] int

);

Insert Into #data Values('1','1','A','1');

Insert Into #data Values('2','1','A','2');

Insert Into #data Values('3','1','A','3');

Insert Into #data Values('4','1','A','4');

Insert Into #data Values('5','1','A','5');

Insert Into #data Values('6','1','A','6');

Insert Into #data Values('7','1','B','1');

Insert Into #data Values('8','1','B','2');

Insert Into #data Values('9','1','B','3');

Insert Into #data Values('10','1','B','4');

Insert Into #data Values('11','1','B','5');

Insert Into #data Values('12','1','B','6');

Insert Into #data Values('13','1','B','7');

Insert Into #data Values('14','1','B','8');

Insert Into #data Values('15','1','B','9');

--Transfer the Student 4 from A section to B section

Update #data Set RollNo=10, Section='B' Where Section='A' And RollNo=4

--To fill the current gap

Update #data

Set

RollNo = (Select Count(*) From #data Sub

Where Sub.Class=#data.Class And Sub.Section=#data.Section And Sub.RollNo<= #data.RollNo)

|||

Thanks Manivannan for the solution. I will try the solution and revert back.

Regards,

Lalit

|||

Hi,

Currently the query is for a single classroom. Could you also suggest me how to extend this query to run for multiple classrooms one after the other.

I hope you help me out. Thanks a ton Manivannan.

|||The above query is capable to handle multiple calssrooms also.. Smile

A challenging query about grouping and order

Greetings !!

I am facing a challenge and hope some one can help me with the query.

I have a school. School have classrooms. Classrooms have students. Classrooms are divided into various sections (Section A, Section B and so on) .Every student is allocated a rollnumber in that section. When a student is transfered from one section to the other, there are gaps in the roll number of other students of the same section. For eg:

Section A

Student 1 Roll no. 1
Student 2 Roll No. 2
Student 3 Roll No 3

Student 4 Roll no 4 and so on.

Now lets say student 2 was transferred to section B, so now there is a gap as roll no 2 is missing in section A

So what should happen is that roll no need to be grouped together based on the section and the rest be readjusted accordingly. So when a student is removed, the roll nos of other students need to be adjusted accordingly. In our case, student 3 will get a roll no of 2, student 4 will have roll no of 3 and so on.

How do I write a query for this. I need to run this as a batch process every fortnight.

Thanks

Suppose if the Roll No is not a foriegn key for other tables then I will agree with your design.

Solution for SQL Server 2005,

Code Snippet

Create Table #data (

[RId] int ,

[Class] int ,

[Section] Char ,

[RollNo] int

);

Insert Into #data Values('1','1','A','1');

Insert Into #data Values('2','1','A','2');

Insert Into #data Values('3','1','A','3');

Insert Into #data Values('4','1','A','4');

Insert Into #data Values('5','1','A','5');

Insert Into #data Values('6','1','A','6');

Insert Into #data Values('7','1','B','1');

Insert Into #data Values('8','1','B','2');

Insert Into #data Values('9','1','B','3');

Insert Into #data Values('10','1','B','4');

Insert Into #data Values('11','1','B','5');

Insert Into #data Values('12','1','B','6');

Insert Into #data Values('13','1','B','7');

Insert Into #data Values('14','1','B','8');

Insert Into #data Values('15','1','B','9');

--Transfer the Student 4 from A section to B section

Update #data Set RollNo=10, Section='B' Where Section='A' And RollNo=4

--To fill the current gap

;WITH CTE

as

(

Select * , Row_Number() OVER (Partition By Class,Section Order By RollNo) NewRollNo

from #data

)

Update

CTE

Set

RollNo = NewRollNo

Where

RollNo <> NewRollNo

|||

For SQL Server 2000,

Code Snippet

Create Table #data (

[RId] int ,

[Class] int ,

[Section] Char ,

[RollNo] int

);

Insert Into #data Values('1','1','A','1');

Insert Into #data Values('2','1','A','2');

Insert Into #data Values('3','1','A','3');

Insert Into #data Values('4','1','A','4');

Insert Into #data Values('5','1','A','5');

Insert Into #data Values('6','1','A','6');

Insert Into #data Values('7','1','B','1');

Insert Into #data Values('8','1','B','2');

Insert Into #data Values('9','1','B','3');

Insert Into #data Values('10','1','B','4');

Insert Into #data Values('11','1','B','5');

Insert Into #data Values('12','1','B','6');

Insert Into #data Values('13','1','B','7');

Insert Into #data Values('14','1','B','8');

Insert Into #data Values('15','1','B','9');

--Transfer the Student 4 from A section to B section

Update #data Set RollNo=10, Section='B' Where Section='A' And RollNo=4

--To fill the current gap

Update #data

Set

RollNo = (Select Count(*) From #data Sub

Where Sub.Class=#data.Class And Sub.Section=#data.Section And Sub.RollNo<= #data.RollNo)

|||

Thanks Manivannan for the solution. I will try the solution and revert back.

Regards,

Lalit

|||

Hi,

Currently the query is for a single classroom. Could you also suggest me how to extend this query to run for multiple classrooms one after the other.

I hope you help me out. Thanks a ton Manivannan.

|||The above query is capable to handle multiple calssrooms also.. Smile

Friday, February 24, 2012

64 bit 60 gb db with RAID 10 4 VOLUME

I am working with SQL 2005 64 bit sp1 , i am facing problem continues in
error log file mentioned below, can you please help me
SQL Server has encountered 1 occurrence(s) of I/O requests taking longer
than 15 seconds to complete on file
[E:\ITS247DBFiles\ItSupport247DB1.mdf:MSSQL_DBCC11 ] in database
[ItSupport247DB] (11). The OS file handle is 0x0000000000007F5C. The offset
of the latest long I/O is: 0x0000032dac0000
For more information, see Help and Support Center at
http://go.microsoft.com/fwlink/events.asp.
"Jay" <Jay@.discussions.microsoft.com> wrote in message
news:926CD051-B1BF-481A-AA4D-BA06AB7DBA72@.microsoft.com...
>I am working with SQL 2005 64 bit sp1 , i am facing problem continues in
> error log file mentioned below, can you please help me
> SQL Server has encountered 1 occurrence(s) of I/O requests taking longer
> than 15 seconds to complete on file
> [E:\ITS247DBFiles\ItSupport247DB1.mdf:MSSQL_DBCC11 ] in database
> [ItSupport247DB] (11). The OS file handle is 0x0000000000007F5C. The
> offset
> of the latest long I/O is: 0x0000032dac0000
> For more information, see Help and Support Center at
> http://go.microsoft.com/fwlink/events.asp.
I'd look at the disk array first, make sure you have the latest drivers,
that there's no hardware issues, etc.
Greg Moore
SQL Server DBA Consulting
sql (at) greenms.com http://www.greenms.com
|||Generally indicates a HW problem.
Had this recently, first line of defense is to power-cycle the server,
second is to refresh RAID controller microcode and power-cycle. Doing
this seems to have "fixed" two of our servers where we were getting
this message - except at much, much higher volumes!
J.
On Tue, 27 Feb 2007 19:59:05 -0800, Jay
<Jay@.discussions.microsoft.com> wrote:

>I am working with SQL 2005 64 bit sp1 , i am facing problem continues in
>error log file mentioned below, can you please help me
>SQL Server has encountered 1 occurrence(s) of I/O requests taking longer
>than 15 seconds to complete on file
>[E:\ITS247DBFiles\ItSupport247DB1.mdf:MSSQL_DBCC1 1] in database
>[ItSupport247DB] (11). The OS file handle is 0x0000000000007F5C. The offset
>of the latest long I/O is: 0x0000032dac0000
>For more information, see Help and Support Center at
>http://go.microsoft.com/fwlink/events.asp.
|||very very Thanks to Greg and JXStern,
can you provide some links related this issues, so that i can show to my
boss.it will be more helpfull to me.
waiting for you alls reply.
regards
Jay
"JXStern" wrote:

> Generally indicates a HW problem.
> Had this recently, first line of defense is to power-cycle the server,
> second is to refresh RAID controller microcode and power-cycle. Doing
> this seems to have "fixed" two of our servers where we were getting
> this message - except at much, much higher volumes!
> J.
>
> On Tue, 27 Feb 2007 19:59:05 -0800, Jay
> <Jay@.discussions.microsoft.com> wrote:
>
>
|||Me, I don't have anything, just called the sysadmin guys (emailed,
actually), and they told me this story, then did it, and it worked!
Twice!
These are only fairly current HP servers, so the problem is probably
very, very, very common.
J.
On Wed, 28 Feb 2007 06:55:52 -0800, Jay
<Jay@.discussions.microsoft.com> wrote:
[vbcol=seagreen]
>very very Thanks to Greg and JXStern,
>can you provide some links related this issues, so that i can show to my
>boss.it will be more helpfull to me.
>waiting for you alls reply.
>regards
>Jay
>
>"JXStern" wrote:
|||thanks JXStern,
i am trying for a link to this issues, so that i can tell to my BOSS
thanks thanks very much
jay
"JXStern" wrote:

> Me, I don't have anything, just called the sysadmin guys (emailed,
> actually), and they told me this story, then did it, and it worked!
> Twice!
> These are only fairly current HP servers, so the problem is probably
> very, very, very common.
> J.
>
> On Wed, 28 Feb 2007 06:55:52 -0800, Jay
> <Jay@.discussions.microsoft.com> wrote:
>
>

64 bit 60 gb db with RAID 10 4 VOLUME

I am working with SQL 2005 64 bit sp1 , i am facing problem continues in
error log file mentioned below, can you please help me
SQL Server has encountered 1 occurrence(s) of I/O requests taking longer
than 15 seconds to complete on file
[E:\ITS247DBFiles\ItSupport247DB1.mdf:MSSQL_DBCC11] in database
[ItSupport247DB] (11). The OS file handle is 0x0000000000007F5C. The o
ffset
of the latest long I/O is: 0x0000032dac0000
For more information, see Help and Support Center at
http://go.microsoft.com/fwlink/events.asp."Jay" <Jay@.discussions.microsoft.com> wrote in message
news:926CD051-B1BF-481A-AA4D-BA06AB7DBA72@.microsoft.com...
>I am working with SQL 2005 64 bit sp1 , i am facing problem continues in
> error log file mentioned below, can you please help me
> SQL Server has encountered 1 occurrence(s) of I/O requests taking longer
> than 15 seconds to complete on file
> [E:\ITS247DBFiles\ItSupport247DB1.mdf:MSSQL_DBCC11] in database
> [ItSupport247DB] (11). The OS file handle is 0x0000000000007F5C. The
> offset
> of the latest long I/O is: 0x0000032dac0000
> For more information, see Help and Support Center at
> http://go.microsoft.com/fwlink/events.asp.
I'd look at the disk array first, make sure you have the latest drivers,
that there's no hardware issues, etc.
Greg Moore
SQL Server DBA Consulting
sql (at) greenms.com http://www.greenms.com|||Generally indicates a HW problem.
Had this recently, first line of defense is to power-cycle the server,
second is to refresh RAID controller microcode and power-cycle. Doing
this seems to have "fixed" two of our servers where we were getting
this message - except at much, much higher volumes!
J.
On Tue, 27 Feb 2007 19:59:05 -0800, Jay
<Jay@.discussions.microsoft.com> wrote:

>I am working with SQL 2005 64 bit sp1 , i am facing problem continues in
>error log file mentioned below, can you please help me
>SQL Server has encountered 1 occurrence(s) of I/O requests taking longer
>than 15 seconds to complete on file
>[E:\ITS247DBFiles\ItSupport247DB1.mdf:MSSQL_DBCC11] in database
>[ItSupport247DB] (11). The OS file handle is 0x0000000000007F5C. The
offset
>of the latest long I/O is: 0x0000032dac0000
>For more information, see Help and Support Center at
>http://go.microsoft.com/fwlink/events.asp.|||very very Thanks to Greg and JXStern,
can you provide some links related this issues, so that i can show to my
boss.it will be more helpfull to me.
waiting for you alls reply.
regards
Jay
"JXStern" wrote:

> Generally indicates a HW problem.
> Had this recently, first line of defense is to power-cycle the server,
> second is to refresh RAID controller microcode and power-cycle. Doing
> this seems to have "fixed" two of our servers where we were getting
> this message - except at much, much higher volumes!
> J.
>
> On Tue, 27 Feb 2007 19:59:05 -0800, Jay
> <Jay@.discussions.microsoft.com> wrote:
>
>|||Me, I don't have anything, just called the sysadmin guys (emailed,
actually), and they told me this story, then did it, and it worked!
Twice!
These are only fairly current HP servers, so the problem is probably
very, very, very common.
J.
On Wed, 28 Feb 2007 06:55:52 -0800, Jay
<Jay@.discussions.microsoft.com> wrote:
[vbcol=seagreen]
>very very Thanks to Greg and JXStern,
>can you provide some links related this issues, so that i can show to my
>boss.it will be more helpfull to me.
>waiting for you alls reply.
>regards
>Jay
>
>"JXStern" wrote:
>|||thanks JXStern,
i am trying for a link to this issues, so that i can tell to my BOSS
thanks thanks very much
jay
"JXStern" wrote:

> Me, I don't have anything, just called the sysadmin guys (emailed,
> actually), and they told me this story, then did it, and it worked!
> Twice!
> These are only fairly current HP servers, so the problem is probably
> very, very, very common.
> J.
>
> On Wed, 28 Feb 2007 06:55:52 -0800, Jay
> <Jay@.discussions.microsoft.com> wrote:
>
>

64 bit 60 gb db with RAID 10 4 VOLUME

I am working with SQL 2005 64 bit sp1 , i am facing problem continues in
error log file mentioned below, can you please help me
SQL Server has encountered 1 occurrence(s) of I/O requests taking longer
than 15 seconds to complete on file
[E:\ITS247DBFiles\ItSupport247DB1.mdf:MSSQL_DBCC11] in database
[ItSupport247DB] (11). The OS file handle is 0x0000000000007F5C. The offset
of the latest long I/O is: 0x0000032dac0000
For more information, see Help and Support Center at
http://go.microsoft.com/fwlink/events.asp."Jay" <Jay@.discussions.microsoft.com> wrote in message
news:926CD051-B1BF-481A-AA4D-BA06AB7DBA72@.microsoft.com...
>I am working with SQL 2005 64 bit sp1 , i am facing problem continues in
> error log file mentioned below, can you please help me
> SQL Server has encountered 1 occurrence(s) of I/O requests taking longer
> than 15 seconds to complete on file
> [E:\ITS247DBFiles\ItSupport247DB1.mdf:MSSQL_DBCC11] in database
> [ItSupport247DB] (11). The OS file handle is 0x0000000000007F5C. The
> offset
> of the latest long I/O is: 0x0000032dac0000
> For more information, see Help and Support Center at
> http://go.microsoft.com/fwlink/events.asp.
I'd look at the disk array first, make sure you have the latest drivers,
that there's no hardware issues, etc.
Greg Moore
SQL Server DBA Consulting
sql (at) greenms.com http://www.greenms.com|||Generally indicates a HW problem.
Had this recently, first line of defense is to power-cycle the server,
second is to refresh RAID controller microcode and power-cycle. Doing
this seems to have "fixed" two of our servers where we were getting
this message - except at much, much higher volumes!
J.
On Tue, 27 Feb 2007 19:59:05 -0800, Jay
<Jay@.discussions.microsoft.com> wrote:
>I am working with SQL 2005 64 bit sp1 , i am facing problem continues in
>error log file mentioned below, can you please help me
>SQL Server has encountered 1 occurrence(s) of I/O requests taking longer
>than 15 seconds to complete on file
>[E:\ITS247DBFiles\ItSupport247DB1.mdf:MSSQL_DBCC11] in database
>[ItSupport247DB] (11). The OS file handle is 0x0000000000007F5C. The offset
>of the latest long I/O is: 0x0000032dac0000
>For more information, see Help and Support Center at
>http://go.microsoft.com/fwlink/events.asp.|||very very Thanks to Greg and JXStern,
can you provide some links related this issues, so that i can show to my
boss.it will be more helpfull to me.
waiting for you alls reply.
regards
Jay
"JXStern" wrote:
> Generally indicates a HW problem.
> Had this recently, first line of defense is to power-cycle the server,
> second is to refresh RAID controller microcode and power-cycle. Doing
> this seems to have "fixed" two of our servers where we were getting
> this message - except at much, much higher volumes!
> J.
>
> On Tue, 27 Feb 2007 19:59:05 -0800, Jay
> <Jay@.discussions.microsoft.com> wrote:
> >I am working with SQL 2005 64 bit sp1 , i am facing problem continues in
> >error log file mentioned below, can you please help me
> >
> >SQL Server has encountered 1 occurrence(s) of I/O requests taking longer
> >than 15 seconds to complete on file
> >[E:\ITS247DBFiles\ItSupport247DB1.mdf:MSSQL_DBCC11] in database
> >[ItSupport247DB] (11). The OS file handle is 0x0000000000007F5C. The offset
> >of the latest long I/O is: 0x0000032dac0000
> >
> >For more information, see Help and Support Center at
> >http://go.microsoft.com/fwlink/events.asp.
>|||Me, I don't have anything, just called the sysadmin guys (emailed,
actually), and they told me this story, then did it, and it worked!
Twice!
These are only fairly current HP servers, so the problem is probably
very, very, very common.
J.
On Wed, 28 Feb 2007 06:55:52 -0800, Jay
<Jay@.discussions.microsoft.com> wrote:
>very very Thanks to Greg and JXStern,
>can you provide some links related this issues, so that i can show to my
>boss.it will be more helpfull to me.
>waiting for you alls reply.
>regards
>Jay
>
>"JXStern" wrote:
>> Generally indicates a HW problem.
>> Had this recently, first line of defense is to power-cycle the server,
>> second is to refresh RAID controller microcode and power-cycle. Doing
>> this seems to have "fixed" two of our servers where we were getting
>> this message - except at much, much higher volumes!
>> J.
>>
>> On Tue, 27 Feb 2007 19:59:05 -0800, Jay
>> <Jay@.discussions.microsoft.com> wrote:
>> >I am working with SQL 2005 64 bit sp1 , i am facing problem continues in
>> >error log file mentioned below, can you please help me
>> >
>> >SQL Server has encountered 1 occurrence(s) of I/O requests taking longer
>> >than 15 seconds to complete on file
>> >[E:\ITS247DBFiles\ItSupport247DB1.mdf:MSSQL_DBCC11] in database
>> >[ItSupport247DB] (11). The OS file handle is 0x0000000000007F5C. The offset
>> >of the latest long I/O is: 0x0000032dac0000
>> >
>> >For more information, see Help and Support Center at
>> >http://go.microsoft.com/fwlink/events.asp.
>>|||thanks JXStern,
i am trying for a link to this issues, so that i can tell to my BOSS
thanks thanks very much
jay
"JXStern" wrote:
> Me, I don't have anything, just called the sysadmin guys (emailed,
> actually), and they told me this story, then did it, and it worked!
> Twice!
> These are only fairly current HP servers, so the problem is probably
> very, very, very common.
> J.
>
> On Wed, 28 Feb 2007 06:55:52 -0800, Jay
> <Jay@.discussions.microsoft.com> wrote:
> >very very Thanks to Greg and JXStern,
> >
> >can you provide some links related this issues, so that i can show to my
> >boss.it will be more helpfull to me.
> >
> >waiting for you alls reply.
> >
> >regards
> >
> >Jay
> >
> >
> >
> >"JXStern" wrote:
> >
> >> Generally indicates a HW problem.
> >>
> >> Had this recently, first line of defense is to power-cycle the server,
> >> second is to refresh RAID controller microcode and power-cycle. Doing
> >> this seems to have "fixed" two of our servers where we were getting
> >> this message - except at much, much higher volumes!
> >>
> >> J.
> >>
> >>
> >> On Tue, 27 Feb 2007 19:59:05 -0800, Jay
> >> <Jay@.discussions.microsoft.com> wrote:
> >>
> >> >I am working with SQL 2005 64 bit sp1 , i am facing problem continues in
> >> >error log file mentioned below, can you please help me
> >> >
> >> >SQL Server has encountered 1 occurrence(s) of I/O requests taking longer
> >> >than 15 seconds to complete on file
> >> >[E:\ITS247DBFiles\ItSupport247DB1.mdf:MSSQL_DBCC11] in database
> >> >[ItSupport247DB] (11). The OS file handle is 0x0000000000007F5C. The offset
> >> >of the latest long I/O is: 0x0000032dac0000
> >> >
> >> >For more information, see Help and Support Center at
> >> >http://go.microsoft.com/fwlink/events.asp.
> >>
> >>
>