Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query Help


Query Help

Author
Message
yogi123
yogi123
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 394
Hello

I need one help to develop logic

please help me to this


create table #Enrollement
(
StudentID Varchar(10),
SchoolID Int,
EntryDate int,
EntryCode Char(3),
WithdrawalDate int,
WithdrawalCode char(3),
SchoolYear int
)

insert into #Enrollement values ('0082600',101,20120830,'A',20120128,NULL,2012)
insert into #Enrollement values ('0082600',103,20130201,'A',20991231,NULL,2012)

create table #Student
(
StudentKey int,
StudentID Varchar(10),
CurrentIEPStatus int,
FRLunch int,
EnrolledSchoolID int,
EnrolledSchoolKey int,
StartDate int,
EndDate int,
CurrentFlag int
)

insert into #Student values (740681,'0082600',0,0,101,6540,20120830,20120930,0)
insert into #Student values (740682,'0082600',0,1,101,6540,20120930,20991231,1)



in requirement, based on EndDate in #Student, I need to display StudentKey and EnrolledSchoolKey.

I try this


select Distinct
E.StudentID,
MAX(S.StudentKey) AS StudentKey,
--S.EnrolledSchoolID,
E.SchoolID,
MAX(s.EnrolledSchoolKey) AS SchoolKey,
E.EntryDate,
E.EntryCode,
E.WithDrawalDate,
E.WithDrawalCode from #Student s
join
#Enrollement E
on E.StudentID = S.StudentID
where S.EndDate <=e.withdrawalDate
and s.StudentID = '0082600'
group by
E.StudentID,
E.SchoolID,
E.EntryDate,
E.EntryCode,
E.WithDrawalDate,
E.WithDrawalCode,
E.SchoolYear
order by StudentKey



and i got output as below


StudentID StudentKey SchoolID SchoolKey EntryDate EntryCode WithDrawalDate WithDrawalCode
0082600 740682 103 6540 20130201 A 20991231 NULL


but desired out put is


StudentID StudentKey SchoolID SchoolKey EntryDate EntryCode WithDrawalDate WithDrawalCode
0082600 740681 101 6540 20120830 A 20120128 NULL
0082600 740682 103 6540 20130201 A 20991231 NULL


Please help me to do this
autoexcrement
autoexcrement
SSC Veteran
SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)

Group: General Forum Members
Points: 204 Visits: 777
Sorry, I would like to help, but there are just too many things about all of this that don't make sense to me.

For example, why are you (seemingly) storing students more than once in the STUDENT table?

And shouldn't there be another table for SCHOOLS, with a single primary key (ID) that you would reference from other tables such as STUDENT, rather than having "schoolid" and "schoolkey" both stored in STUDENT?

If you alter your design so it is properly normalized, you will eliminate redundant and incoherent data, and it will vastly simplify your queries.


"If I had been drinking out of that toilet, I might have been killed." -Ace Ventura
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24261 Visits: 37981
autoexcrement (4/11/2013)
Sorry, I would like to help, but there are just too many things about all of this that don't make sense to me.

For example, why are you (seemingly) storing students more than once in the STUDENT table?

If you alter your design so it is properly normalized, you will eliminate redundant and incoherent data, and it will vastly simplify your queries.


Happens to be a normal occurance in a SIS apparently. It was when I worked at a school district. The student table had one (or more) records for each student enrolled for that school year.

My question is what is the value of S.EndDate when this query was run.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
yogi123
yogi123
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 394
Lynn Pettis (4/11/2013)
autoexcrement (4/11/2013)
Sorry, I would like to help, but there are just too many things about all of this that don't make sense to me.

For example, why are you (seemingly) storing students more than once in the STUDENT table?

If you alter your design so it is properly normalized, you will eliminate redundant and incoherent data, and it will vastly simplify your queries.


Happens to be a normal occurance in a SIS apparently. It was when I worked at a school district. The student table had one (or more) records for each student enrolled for that school year.

My question is what is the value of S.EndDate when this query was run.


basically we capture changes during a year so Student table has multiple records.
Start and EndDate is for that purpose of track those changes
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24261 Visits: 37981
yogi123 (4/11/2013)
Lynn Pettis (4/11/2013)
autoexcrement (4/11/2013)
Sorry, I would like to help, but there are just too many things about all of this that don't make sense to me.

For example, why are you (seemingly) storing students more than once in the STUDENT table?

If you alter your design so it is properly normalized, you will eliminate redundant and incoherent data, and it will vastly simplify your queries.


Happens to be a normal occurance in a SIS apparently. It was when I worked at a school district. The student table had one (or more) records for each student enrolled for that school year.

My question is what is the value of S.EndDate when this query was run.


basically we capture changes during a year so Student table has multiple records.
Start and EndDate is for that purpose of track those changes


Quite familiar with this having worked at a k-12 public school district for 5 years. Unfortunately it is difficult to tell you why you got the results you did instead of the expected results since we can't see the data used.

You need to provide us with a reasonable facsimile of your problem (tables, sample data in a readily consummable format -- i.e. cut/paste/run in SSMS) for us to really help.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Mansfield
Mansfield
Old Hand
Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)

Group: General Forum Members
Points: 370 Visits: 394
Lynn Pettis (4/11/2013)
My question is what is the value of S.EndDate when this query was run.


I'm taking it to mean that we need to "anchor" the #student table to return only a single version of each StudentID. With that in mind, will something like this work?


select e.StudentID,
s.StudentKey,
e.SchoolID,
s.EnrolledSchoolKey,
e.EntryDate,
e.EntryCode,
e.WithDrawalDate,
e.WithDrawalCode
from #student s
join #enrollement e on s.enddate >= e.WithdrawalDate and s.StudentID = e.StudentID
where s.StudentID = '0082600'
and s.CurrentFlag = 1


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search