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:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old March 19th, 2008, 05:36 AM
jennyferlai jennyferlai is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Posts: 11 jennyferlai User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 57 m 47 sec
Reputation Power: 0
Delete min

Hi! I have a patient's table with 3 fields:
NUMPAT is the number of the patient
TREATMENT is a treatment code
DATE a date
There are many dates for each patient and I need to select the minimun date for each patient. This SQL works fine:

SELECT T.NUMPAT,T.TREATMEN T,T.DATE
FROM MYTABLE T
WHERE
T.DATE=
(SELECT MIN(Q.DATE) FROM
MYTABLE Q WHERE T.NUMPAT = Q.NUMPAT )
order by T.NUMPAT

By now I need to delete that minimun date for each patient, and simply change the first SELECT for a DELETE is not working... how could I?
Sorry for this so simple question...

Reply With Quote
  #2  
Old March 19th, 2008, 05:55 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,686 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 16 h 27 m 51 sec
Reputation Power: 259
Why something like this shouldn't work? What error message do you get?
sql Code:
Original - sql Code
  1. DELETE
  2. FROM
  3.  pat_hist p
  4. WHERE
  5.  p.pat_date =
  6. (
  7.   SELECT
  8.    MIN(a.pat_date)
  9.   FROM
  10.    pat_hist a
  11.   WHERE
  12.    a.pat_Name = p.pat_name
  13. )

Reply With Quote
  #3  
Old March 19th, 2008, 06:01 AM
shammat shammat is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 962 shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 14 h 50 m 46 sec
Reputation Power: 57
Code:
DELETE FROM 
FROM MYTABLE 
WHERE "DATE" = (SELECT MIN(Q.DATE) 
FROM MYTABLE Q WHERE MYTABLE.NUMPAT = Q.NUMPAT)
should work.
But I strongly recommend not to use a reserved word (DATE) as a column name!
Comments on this post
pabloj agrees!
Annie79 agrees!

Reply With Quote
  #4  
Old March 19th, 2008, 07:10 AM
jennyferlai jennyferlai is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Posts: 11 jennyferlai User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 57 m 47 sec
Reputation Power: 0
Yes, something like this should work, but does not, I don't know why, it deletes the whole table

DELETE
FROM
MYTABLE p
WHERE
p.DATA =
(
SELECT
MIN(a.DATA)
FROM
MYTABLE a
WHERE
a.C_HISTORIA = p.C_HISTORIA
)

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesFirebird SQL Development > Delete min


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 5 hosted by Hostway