If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > History query question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-03-07, 07:18
David_nyh David_nyh is offline
Registered User
 
Join Date: Nov 2004
Posts: 6
History query question

A new question.

I have a school history table: ID_HISTORY, ID_STUDENT, ID_SCHOOL, DATE_CREATED.

When I give a date like 01/01/2007 up in the query, the query need to search the records in the school history table where the id from the students is null. And when the id is null it need to find the id from the student from a record with the same id from the school and a date that is less but higher than the other record I gave up.

I already tried to do this but with no luck. It always give me a error.

SELECT A.ID_HISTORY, A.ID_STUDENT, A.ID_SCHOOL, A.DATE_CREATED FROM SCHOOL_HISTORY A WHERE A.DATE_CREATED =
(
SELECT MAX(B.DATE_CREATED) FROM SCHOOL_HISTORY B WHERE B.ID_SCHOOL = A.ID_SCHOOL AND CAST(B.DATE_CREATED AS DATE) <= '01/01/2007' AND
(
SELECT C.ID_STUDENT FROM SCHOOL_HIST C WHERE CAST(C.DATE_CREATED AS DATE) = '01/01/2007' AND C.ID_SCHOOL = A.ID_SCHOOL
)
IS NULL
)

Sorry for my bad English

Thanks in advance

Nyh

Last edited by David_nyh; 09-04-07 at 14:01.
Reply With Quote
  #2 (permalink)  
Old 09-04-07, 14:04
David_nyh David_nyh is offline
Registered User
 
Join Date: Nov 2004
Posts: 6
A new question. See post above.
Reply With Quote
  #3 (permalink)  
Old 09-04-07, 15:12
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
I do not understand what you want. Maybe if you give an example of the data in the table and what the query should produce.


Andy
Reply With Quote
  #4 (permalink)  
Old 09-05-07, 01:52
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
Do you mean something like

SELECT DISTINCT A.ID_HISTORY , A.ID_SCHOOL , A.DATE_CREATED, B.ID_STUDENT
FROM
SCHOOL_HISTORY A ,
SCHOOL_HISTORY B
WHERE A.ID_STUDENT IS NULL
AND A.ID_SCHOOL = B.ID_SCHOOL
AND B.DATE_CREATED > A.DATE_CREATED
AND B.DATE_CREATED < your-given-date
AND B.ID_STUDENT IS NOT NULL;



You want to find records where the student Id is null
WHERE A.ID_STUDENT IS NULL
and for those records you want to find rows with the same school id
AND A.ID_SCHOOL = B.ID_SCHOOL
and a date that is less [than the specified date]
AND B.DATE_CREATED < your-given-date
but higher the date of the other record given
AND B.DATE_CREATED > A.DATE_CREATED
and, of course, the student id should be given in that record
AND B.ID_STUDENT IS NOT NULL
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On