On 10/14/2015 9:29 PM, Don V Nielsen wrote:
> But I am having problems with the LIMIT statement.  It throws an exception
> no matter what table alias is used: X or x2.  It says "no such column".

Ah, interesting. LIMIT clause doesn't appear to allow correlated 
subqueries; only self-contained expressions. Here goes that idea.

Something like this should work:

select X.*
from addresses X join crrt_net_non_pieces using (zip, crrt)
where net_non_pieces > (
   select count(*) from addresses x2
   where X.zip=x2.zip and X.crrt=x2.crrt and
   (x2.version_id < X.version_id or (x2.version_id = X.version_id and 
x2.rowid < X.rowid))
);

I took the liberty to simplify the ordering expression, for purposes of 
exposition (it's very long and would need to be repeated four times). 
Replace all occurrences of T.version_id with your CASE clause, 
calculated against table T. Also, I'm breaking ties by rowid; your 
original problem statement is underspecified unless there's a total 
order on Addresses.
-- 
Igor Tandetnik

Reply via email to