
>Bruce Momjian <pgman@candle.pha.pa.us>

>
>> emkxp01=> delete from deltest s1 where s1.respdate not in ( select 
>> min(s2.respdate) from deltest s2 where s1.sessionid = s2.sessionid);
>> ERROR:  parser: parse error at or near "s1"
>> emkxp01=> 
>
>Don't use s1.  Just refer to native deltest in the subquery.  That
>should reference the outer table.

That doesn't seem to work as 3 rows are deleted and not just the
two duplicates.

emkxp01=> delete from deltest where respdate not in ( select min(s2.respdate) 
from deltest s2 where sessionid = s2.sessionid);
DELETE 3
emkxp01=> select * from deltest;
 sessionid |           respdate           
-----------+------------------------------
         1 | Tue 07 Dec 22:32:01 1999 GMT
(1 row)

emkxp01=> 

Keith.


Wed, 8 Dec 1999 00:48:12 +0000 (GMT)
Keith Parks <emkxp01@mtcc.demon.co.uk>
Re: [HACKERS] Table aliases in delete statements?
> >Don't use s1.  Just refer to native deltest in the subquery.  That
> >should reference the outer table.
> 
> That doesn't seem to work as 3 rows are deleted and not just the
> two duplicates.
> 
> emkxp01=> delete from deltest where respdate not in ( select min(s2.respdate) 
> from deltest s2 where sessionid = s2.sessionid);
> DELETE 3
> emkxp01=> select * from deltest;
>  sessionid |           respdate           
> -----------+------------------------------
>          1 | Tue 07 Dec 22:32:01 1999 GMT
> (1 row)

No.  Use:

emkxp01=> delete from deltest where respdate not in ( select min(s2.respdate) 
from deltest s2 where deltest.sessionid = s2.sessionid);
                      ^^^^^^^

-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  maillist@candle.pha.pa.us            |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Tue, 7 Dec 1999 20:05:18 -0500 (EST)
Bruce Momjian <pgman@candle.pha.pa.us>
Re: [HACKERS] Table aliases in delete statements?