Firebird SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesFirebird SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 27th, 2007, 06:56 AM
aleplgr aleplgr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 20 aleplgr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 14 m 41 sec
Reputation Power: 0
Nearest to a fixed value

Hi! I'm trying to solve this:
I have this table
PatientID DateIni Dateend Days

For each PatientID there are many Dateend but only one DateIni.
Days is the difference between Dateend and DateIni.

I need to find for each PatientID the Dateend which Days value is nearest to 365.

So for each PatientID I can have 3 cases:
1.For all the Dateend all the Days value are less than 365 return the greatest Dateend
2. There are Dateend which Days value is less than 365 and other which are greater than 365, return the greatest Dateend that's less than 365
3. All the Dateend have all the Days value greater than 365 return the less Dateend

I could GROUP BY Patientid and if for each PatientID there's at least one Dateend which Days <365 return MAX(Dateend) else return MIN (Dateend)
But I don't know how to loop in each group and how to create the groups..

Reply With Quote
  #2  
Old April 30th, 2007, 03:48 AM
aleplgr aleplgr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 20 aleplgr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 14 m 41 sec
Reputation Power: 0
Just to tell that it's done!!! This solves the problem:

Code:
select T1.PatientID, T1.DateIni, min(T1.Dateend) , min(T1.Days)
from ThisTable T1
where not exists(select * from ThisTable T2
where T2.PatientID = T1.PatientID
and T2.Days between T1.Days+1 and 365)
group by 1, 2

Reply With Quote
  #3  
Old April 30th, 2007, 12:39 PM
clivew clivew is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 855 clivew User rank is Sergeant (500 - 2000 Reputation Level)clivew User rank is Sergeant (500 - 2000 Reputation Level)clivew User rank is Sergeant (500 - 2000 Reputation Level)clivew User rank is Sergeant (500 - 2000 Reputation Level)clivew User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 11 h 11 m 43 sec
Reputation Power: 19
Quote:
Originally Posted by aleplgr
Just to tell that it's done!!! This solves the problem:

Code:
select T1.PatientID, T1.DateIni, min(T1.Dateend) , min(T1.Days)
from ThisTable T1
where not exists(select * from ThisTable T2
where T2.PatientID = T1.PatientID
and T2.Days between T1.Days+1 and 365)
group by 1, 2

Can you help me understand how?

With a where clause that appears to exclude any patient who has any Days value between 1 and 365
how do you get the first and second group of results?
Quote:
1.For all the Dateend all the Days value are less than 365 return the greatest Dateend
2. There are Dateend which Days value is less than 365
and other which are greater than 365,
return the greatest Dateend that's less than 365

Also, I am curious about what gets returned with two min clauses.
Do you get two records per patient?
min(T1.Dateend) , min(T1.Days)
min(T1.Days),min(T1.Dateend)

or What?

Thanks,
Clive

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesFirebird SQL Development > Nearest to a fixed value


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT