🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.interbase.sql
Article #352

Re: Delete min does not work

#352
From: Wayne Niddery (TeamB)
Date: Wed, 19 Mar 2008 17:19
60 lines
3014 bytes
"Alejandro" <aleplgr@yahoo.es> wrote in message 
news:47e127be$1@newsgroups.borland.com...
>
> There are many dates for each patient and for selecting 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

If all you want is minimum date for each patient, regardless of treatment, 
then this should be more efficient by using Group By:

 SELECT T.NUMPAT, MIN(T.DATE)
 FROM MYTABLE T
 GROUP BY T.NUMPAT

> But for deleting the minimum date for each patient I'm trying this:
> DELETE
> FROM MYTABLE T
> WHERE
> T.DATE=
> (SELECT MIN(Q.DATE) FROM
> MYTABLE Q WHERE T.NUMPAT = Q.NUMPAT )
>
> But deletes the whole table, I don't know why..

Because the only criteria you've set is that a record match a selected date 
without regard to which patient, and as it deletes, it will match each next 
record to the now existing minimum date.

InterBase cannot compare two fields using a single subquery and so there's 
really no way to formulate a single delete statement to do this. The only 
way I can see to do it is via a stored procedure. The following procedure 
will work:

CREATE PROCEDURE "DELETEOLDESTTREAMENT"
AS
declare variable numpat integer;
declare variable tdate date;
begin
  for select numpat, min(tdate)
    from patients
    group by numpat
  into numpat, tdate do
  begin
    delete from patients where numpat = :numpat and tdate = :tdate;
  end
end

Run this each time you want the oldest treatment for each patient deleted:
  execute procedure deleteoldesttreatment

-- 
Wayne Niddery - TeamB (www.teamb.com)
Winwright, Inc. (www.winwright.ca) 
Technical Details
Message-ID: <47e14b0b$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!feeder3.cambrium.nl!feeder1.cambrium.nl!feed.tweaknews.nl!83.128.0.11.MISMATCH!news-out1.kabelfoon.nl!newsfeed.kabelfoon.nl!bandi.nntp.kabelfoon.nl!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <47e127be$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 114a0e,a87d74dec5eac2
X-Google-Attributes: gid114a0e,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Wayne Niddery \(TeamB\)" <wniddery@chaffaci.on.ca>
Newsgroups: borland.public.interbase.sql
References: <47e127be$1@newsgroups.borland.com>
In-Reply-To: <47e127be$1@newsgroups.borland.com>
Subject: Re: Delete min does not work
Date: Wed, 19 Mar 2008 13:19:08 -0400
Lines: 61
MIME-Version: 1.0
Content-Type: text/plain;
 format=flowed;
 charset="iso-8859-1";
 reply-type=original
Content-Transfer-Encoding: 7bit
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Windows Mail 6.0.6000.16480
X-MimeOLE: Produced By Microsoft MimeOLE V6.0.6000.16545
NNTP-Posting-Host: 24.137.214.213
Message-ID: <47e14b0b$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1205947147 24.137.214.213 (19 Mar 2008 10:19:07 -0700)
X-Authenticated-User: wniddery
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!feeder3.cambrium.nl!feeder1.cambrium.nl!feed.tweaknews.nl!83.128.0.11.MISMATCH!news-out1.kabelfoon.nl!newsfeed.kabelfoon.nl!bandi.nntp.kabelfoon.nl!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail