🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Delete min does not work

#354
From: Alejandro
Date: Thu, 20 Mar 2008 11:03
66 lines
2962 bytes
"Wayne Niddery \(TeamB\)" <wniddery@chaffaci.on.ca> wrote:
>"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) 
>

THANKS SO MUCH WAYNE!!
MUCHAS GRACIAS!!!!!!
Technical Details
Message-ID: <47e2448b$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!feeder3.cambrium.nl!feeder1.cambrium.nl!feed.tweaknews.nl!news2.euro.net!newsfeed.freenet.de!newspeer1.de.telia.net!newspeer4.de.telia.net!de.telia.net!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> <47e14b0b$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: "Alejandro" <aleplgr@yahoo.es>
Sender: "Alejandro" <aleplgr@yahoo.es>
Reply-To: "Alejandro" <aleplgr@yahoo.es>
Subject: Re: Delete min does not work
Newsgroups: borland.public.interbase.sql
X-User-Info: 80.58.205.32     
X-Forwarded-For: 80.25.195.180, 192.168.1.66
References: <47e127be$1@newsgroups.borland.com> <47e14b0b$1@newsgroups.borland.com>
NNTP-Posting-Host: 207.105.83.66
Message-ID: <47e2448b$1@newsgroups.borland.com>
Date: 20 Mar 2008 04:03:39 -0700
X-Trace: newsgroups.borland.com 1206011019 207.105.83.66 (20 Mar 2008 04:03:39 -0700)
Lines: 69
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!feeder3.cambrium.nl!feeder1.cambrium.nl!feed.tweaknews.nl!news2.euro.net!newsfeed.freenet.de!newspeer1.de.telia.net!newspeer4.de.telia.net!de.telia.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail