[SQL] Number of rows in a cursor ?

2005-08-24 Thread Bo Lorentsen

Hi ...

Is it possible to get the total number of rows found in a cursor, or 
must I make a count(*) and then a select ?


/BL

---(end of broadcast)---
TIP 3: Have you checked our extensive FAQ?

  http://www.postgresql.org/docs/faq


Re: [SQL] Number of rows in a cursor ?

2005-08-24 Thread Thomas Borg Salling

Bo Lorentsen wrote:

Is it possible to get the total number of rows found in a cursor, or 
must I make a count(*) and then a select ?


Perhaps:
GET DIAGNOSTICS rc = ROW_COUNT;

(see http://archives.postgresql.org/pgsql-novice/2003-06/msg00143.php 
and 
http://www.postgresql.org/docs/8.0/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-DIAGNOSTICS)


/Thomas.


---(end of broadcast)---
TIP 1: if posting/reading through Usenet, please send an appropriate
  subscribe-nomail command to [EMAIL PROTECTED] so that your
  message can get through to the mailing list cleanly


Re: [SQL] Tuple insert missing query in ongoing transaction

2005-08-24 Thread Szűcs Gábor

Dear DAQ,

Thanks for the native answer ;) For those who have concerns reading 
Hungarian, here is an excerpt including my answer.


You adviced me to issue an ACCESS EXCLUSIVE lock on table "shift" in 
transaction B, before issuing the actual insert, so that the insert will 
wait till the end of transaction S.


Actually, you pointed me to the right way of the PostgreSQL doc and gave me 
the inspiration to actually read about lock modes :) As far as I understood 
things:


1. Shouldn't it be enough to lock in EXCLUSIVE mode, thus allowing others to 
access (select only) table "shift"? What's more, since any ins/upd/del on a 
table aquires ROW EXCLUSIVE mode, shouldn't it be enough to just acquire 
SHARE mode? It wouldn't even conflict with concurrent SHARE locks of other 
clients inserting into table B.


2. Am I reading right that acquiring a lock in a BEFORE trigger is too late? 
(not mentioning that trigger firing order is undefined) If so, is there 
another way to provide a server-side automatic lock before the insert, say, 
writing an ON INSERT rule on table "barcode"?


3. I can't see why shouldn't this work with updates and deletes (on any of 
the two tables), but since I'm new to this topic, it's better to ask: should 
I have any further concerns?


TIA,

--
G.


On 2005.08.23. 18:26, daq wrote:

SG> Dear Gurus,

SG> I know this is the typical case of transaction use, I just seem to lack the 
SG> appropriate education of what exactly happens and whether I may be able to 
SG> detect it.


SG> I have two queries, one affecting the other.

SG> 1. INSERT INTO barcode.
SG> A BEFORE INSERT/UPDATE trigger checks if there's an appropriate tuple in 
SG> table "shift" for this tuple (matching day, shift-of-the-day and workplace) 
SG> and denormalizes fields.


SG> 2. INSERT INTO shift.
SG> An AFTER INSERT/UPDATE/DELETE trigger updates rows in table barcode, forcing 
SG> the abovementioned check for shift.


SG> The second one is a long process, taking about 20 sec to finish. Imagine the 
SG> following scenario:


SG> x:xx:00 INSERT INTO shift.
SG> Transaction "S" begins.
SG> It updates several rows, but not the not-yet-inserted row.
SG> x:xx:10 INSERT INTO barcode (... appropriate for above-inserted shift ...)
SG> Transaction "B" begins.
SG> It checks but does not find the corresponding shift.
SG> x:xx:11 Transaction "B" ends.
SG> x:xx:20 Transaction "S" ends.

SG> In such scenarios, sometimes we get "Deadlock detected." That's OK since the 
SG> transactions actually cross each other's way.


SG> But not always. In about 100 inserts, now we have the first case that did 
SG> not show any trace of that something went wrong.


SG> 1. Is there a way to detect such "crossing" transactions?
SG> 2. Is there a thorough article on deadlocks, how and when do they happen?
SG> 3. Maybe a section of the postgresql doc clarifying when do tuples get 
locked?

A helyzet elkerulesere talan tudok egy modszert. A barcode tablaba
insertalast szervezd tranzakcioba, es access exclusive moddal lockold
az elejen(mindenkeppen meg az insert elott) a shift tablat. Igy mig "S" 
tranzakcio folyamatban van,
addig a "B" nem indulhat el az exclusiv lock igenye miatt, es mig a
"B" var a sorara, addig ujabb "S"-t sem enged elindulni. Igy
elkerulheted az egymast keresztbe vero tranzakciokat, ami a
deadlockhoz szokott vezetni.

DAQ




---(end of broadcast)---
TIP 3: Have you checked our extensive FAQ?

  http://www.postgresql.org/docs/faq


Re: [SQL] Number of rows in a cursor ?

2005-08-24 Thread neil.saunders
You cannot count the number of rows in a cursor, unfortunately. I recently ran 
in to this problem.

As far as I am aware, the only way to count them is to either iterate through 
them, 
or if you are only expecting one or two, perform multiple FETCHES and test if 
the 
record set returned is empty.

http://archives.postgresql.org/pgsql-sql/2005-08/msg00208.php

OPEN cur_overlap FOR EXECUTE 'SELECT *, ';

FETCH cur_overlap INTO row_one;
FETCH cur_overlap INTO row_two; 

IF (row_two.id IS NULL) THEN 


King regards,

Neil.



This message is for the designated recipient only and may contain privileged, 
proprietary, or otherwise private information.  If you have received it in 
error, please notify the sender immediately and delete the original.  Any other 
use of the email by you is prohibited.

---(end of broadcast)---
TIP 6: explain analyze is your friend


Re: [SQL] Number of rows in a cursor ?

2005-08-24 Thread Bo Lorentsen

Thomas Borg Salling wrote:


Bo Lorentsen wrote:

Is it possible to get the total number of rows found in a cursor, or 
must I make a count(*) and then a select ?



Perhaps:
GET DIAGNOSTICS rc = ROW_COUNT;


Ok, and when I use the C interface the "DECLARE .." function will return 
the row count ?


Can I use PQntuples( res ) after the "DECLARE CURSOR" statement (if so, 
it don't work for me :-)) ?


/BL

---(end of broadcast)---
TIP 1: if posting/reading through Usenet, please send an appropriate
  subscribe-nomail command to [EMAIL PROTECTED] so that your
  message can get through to the mailing list cleanly


Re: [SQL] Number of rows in a cursor ?

2005-08-24 Thread Bo Lorentsen

[EMAIL PROTECTED] wrote:


You cannot count the number of rows in a cursor, unfortunately. I recently ran 
in to this problem.
 

How sad, then I have to repeat the query, first for counting and last 
for data fetch :-(


/BL

---(end of broadcast)---
TIP 4: Have you searched our list archives?

  http://archives.postgresql.org


[SQL] SQLException - SET AUTOCOMMIT TO OFF is no longer supported

2005-08-24 Thread Dinesh Pandey








I have created datasource but getting this error on JBOSS
startup.

 

Using: PostgreSQL 8.0

JBOSS: JBOSS-3.2.6

 

16:09:37,093 WARN  [TransactionImpl] XAException:
tx=TransactionImpl:XidImpl [FormatId=257, GlobalId=dinesh//1, BranchQual=]
errorCode=XA_UNKNOWN(0)

org.jboss.resource.connectionmanager.JBossLocalXAException:
Error trying to start local tx: ; - nested throwable:
(org.jboss.resource.JBossResourceException: 

SQLException;
- nested throwable: (java.sql.SQLException: ERROR:  SET AUTOCOMMIT TO OFF
is no longer supported

))

    at
org.jboss.resource.connectionmanager.TxConnectionManager$LocalXAResource.start(TxConnectionManager.java:654)

    at
org.jboss.tm.TransactionImpl.startResource(TransactionImpl.java:1196)

 

    at
org.jboss.tm.TransactionImpl.enlistResource(TransactionImpl.java:649)

 

Thanks
Dinesh Pandey

 








Re: [SQL] SQLException - SET AUTOCOMMIT TO OFF is no longer supported

2005-08-24 Thread Dinesh Pandey








After using correct version of JDBC driver
I am able to start JBOSS server.



 

Thanks
Dinesh Pandey













 

I have created datasource but getting this error on JBOSS
startup.

 

Using: PostgreSQL 8.0

JBOSS: JBOSS-3.2.6

 

16:09:37,093 WARN  [TransactionImpl] XAException:
tx=TransactionImpl:XidImpl [FormatId=257, GlobalId=dinesh//1, BranchQual=]
errorCode=XA_UNKNOWN(0)

org.jboss.resource.connectionmanager.JBossLocalXAException:
Error trying to start local tx: ; - nested throwable:
(org.jboss.resource.JBossResourceException: 

SQLException;
- nested throwable: (java.sql.SQLException: ERROR:  SET AUTOCOMMIT TO OFF
is no longer supported

))

    at
org.jboss.resource.connectionmanager.TxConnectionManager$LocalXAResource.start(TxConnectionManager.java:654)

    at
org.jboss.tm.TransactionImpl.startResource(TransactionImpl.java:1196)

 

    at
org.jboss.tm.TransactionImpl.enlistResource(TransactionImpl.java:649)

 

Thanks
Dinesh Pandey

 








Re: [SQL] Number of rows in a cursor ?

2005-08-24 Thread Russell Simpkins

[EMAIL PROTECTED] wrote:

You cannot count the number of rows in a cursor, unfortunately. I recently 
ran in to this problem.


How sad, then I have to repeat the query, first for counting and last for 
data fetch :-(


/BL



If you need a count, why not just execute one of the methods to get a count. 
i.e.e select count(id) ... 


---(end of broadcast)---
TIP 5: don't forget to increase your free space map settings


Re: [SQL] SQLException - SET AUTOCOMMIT TO OFF is no longer supported

2005-08-24 Thread Russell Simpkins




 

  
  16:09:37,093 WARN  
  [TransactionImpl] XAException: tx=TransactionImpl:XidImpl [FormatId=257, 
  GlobalId=dinesh//1, BranchQual=] 
  errorCode=XA_UNKNOWN(0)
  org.jboss.resource.connectionmanager.JBossLocalXAException: 
  Error trying to start local tx: ; - nested throwable: 
  (org.jboss.resource.JBossResourceException: 
  SQLException; 
  - nested throwable: (java.sql.SQLException: ERROR:  SET AUTOCOMMIT TO OFF 
  is no longer supported
  ))
      
  at 
  org.jboss.resource.connectionmanager.TxConnectionManager$LocalXAResource.start(TxConnectionManager.java:654)
      
  at 
  org.jboss.tm.TransactionImpl.startResource(TransactionImpl.java:1196)
   
      
  at 
  org.jboss.tm.TransactionImpl.enlistResource(TransactionImpl.java:649)
   
  ThanksDinesh 
  Pandey


Re: [SQL] SQLException - SET AUTOCOMMIT TO OFF is no longer supported

2005-08-24 Thread Russell Simpkins



 

  
  I have created datasource but 
  getting this error on JBOSS startup.
   
  Using: 
  PostgreSQL 8.0
  JBOSS: 
  JBOSS-3.2.6
   
  16:09:37,093 WARN  
  [TransactionImpl] XAException: tx=TransactionImpl:XidImpl [FormatId=257, 
  GlobalId=dinesh//1, BranchQual=] 
  errorCode=XA_UNKNOWN(0)
  org.jboss.resource.connectionmanager.JBossLocalXAException: 
  Error trying to start local tx: ; - nested throwable: 
  (org.jboss.resource.JBossResourceException: 
  SQLException; 
  - nested throwable: (java.sql.SQLException: ERROR:  SET AUTOCOMMIT TO OFF 
  is no longer supported
  ))
      
  at 
  org.jboss.resource.connectionmanager.TxConnectionManager$LocalXAResource.start(TxConnectionManager.java:654)
      
  at 
  org.jboss.tm.TransactionImpl.startResource(TransactionImpl.java:1196)
   
      
  at 
  org.jboss.tm.TransactionImpl.enlistResource(TransactionImpl.java:649)
   
  ThanksDinesh 
  Pandey
I think you need to get a 
more up to date version of your JDBC 
Driver


Re: [SQL] Number of rows in a cursor ?

2005-08-24 Thread John DeSoi


On Aug 24, 2005, at 6:31 AM, Bo Lorentsen wrote:

How sad, then I have to repeat the query, first for counting and  
last for data fetch :-(


No, you can use the MOVE command and read how many rows you moved  
with something like


MOVE LAST IN mycursor;

http://www.postgresql.org/docs/8.0/interactive/sql-move.html



John DeSoi, Ph.D.
http://pgedit.com/
Power Tools for PostgreSQL


---(end of broadcast)---
TIP 4: Have you searched our list archives?

  http://archives.postgresql.org


Re: [SQL] SQLException - SET AUTOCOMMIT TO OFF is no longer supported

2005-08-24 Thread Joel Fradkin








I am getting ready to start using jboss
with postgres (newer to jboss then postgres).

Is there a mailing list for java postgres
support?

Can you post the connection elements for
JBOSS or is that something documented in the JDBC driver?

 



Joel Fradkin



 



Wazagua, Inc.
2520 Trailmate Dr
Sarasota, Florida 34243
Tel.  941-753-7111 ext 305



 



[EMAIL PROTECTED]
www.wazagua.com
Powered by Wazagua
Providing you with the latest Web-based technology & advanced tools.
© 2004. WAZAGUA, Inc. All rights reserved. WAZAGUA, Inc
 This email message is for the use of the intended recipient(s) and may
contain confidential and privileged information.  Any unauthorized review,
use, disclosure or distribution is prohibited.  If you are not the
intended recipient, please contact the sender by reply email and delete and
destroy all copies of the original message, including attachments.



 




 



-Original Message-
From:
[EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Dinesh Pandey
Sent: Wednesday, August 24, 2005
7:22 AM
To: 'PostgreSQL'
Subject: Re: [SQL] SQLException -
SET AUTOCOMMIT TO OFF is no longer supported

 

After
using correct version of JDBC driver I am able to start JBOSS server.



 

Thanks
Dinesh Pandey













 

I have created datasource but
getting this error on JBOSS startup.

 

Using:
PostgreSQL 8.0

JBOSS:
JBOSS-3.2.6

 

16:09:37,093 WARN 
[TransactionImpl] XAException: tx=TransactionImpl:XidImpl [FormatId=257,
GlobalId=dinesh//1, BranchQual=] errorCode=XA_UNKNOWN(0)

org.jboss.resource.connectionmanager.JBossLocalXAException:
Error trying to start local tx: ; - nested throwable:
(org.jboss.resource.JBossResourceException: 

SQLException; - nested throwable: (java.sql.SQLException:
ERROR:  SET AUTOCOMMIT TO OFF is no longer supported

))

   
at org.jboss.resource.connectionmanager.TxConnectionManager$LocalXAResource.start(TxConnectionManager.java:654)

   
at org.jboss.tm.TransactionImpl.startResource(TransactionImpl.java:1196)

 

   
at org.jboss.tm.TransactionImpl.enlistResource(TransactionImpl.java:649)

 

Thanks
Dinesh Pandey

 








Re: [SQL] Number of rows in a cursor ?

2005-08-24 Thread Bo Lorentsen

Russell Simpkins wrote:

If you need a count, why not just execute one of the methods to get a 
count. i.e.e select count(id) ...


The reason why i don't  just make a count and then a selection of data 
is performance.


/BL


---(end of broadcast)---
TIP 9: In versions below 8.0, the planner will ignore your desire to
  choose an index scan if your joining column's datatypes do not
  match


Re: [SQL] Number of rows in a cursor ?

2005-08-24 Thread Michael Fuhr
On Wed, Aug 24, 2005 at 02:50:07PM +0200, Bo Lorentsen wrote:
> Russell Simpkins wrote:
> >If you need a count, why not just execute one of the methods to get a 
> >count. i.e.e select count(id) ...
> 
> The reason why i don't  just make a count and then a selection of data 
> is performance.

...which is the same reason a cursor doesn't know how many rows it will
fetch until you fetch them all (or MOVE to the end of the cursor,
which fetches the rows internally).

-- 
Michael Fuhr

---(end of broadcast)---
TIP 1: if posting/reading through Usenet, please send an appropriate
   subscribe-nomail command to [EMAIL PROTECTED] so that your
   message can get through to the mailing list cleanly


[SQL] How to join several selects

2005-08-24 Thread Josep Sanmartí

Hello,
I have a 'big' problem:
I have the following table users(name, start_time, end_time), a new row 
is set whenever a user logs into a server.  I want to know how many 
users have logged in EVERYDAY between 2 different dates. The only idea 
that I have is making several select (one for each day):
   SELECT COUNT(name) FROM users WHERE start_time between "startDate" 
and "startDate+1"
   SELECT COUNT(name) FROM users WHERE start_time between "startDate+1" 
and "startDate+2"

   ...
I would like to know if its possible to make it in 1 sql statement or 
just which is the best efficient way to solve it.

By the way, I use Postgres 7.4.

Thanks!

--
Josep Sanmarti
Analista de Projectes

Grup OpenWired, S.L.
Caballero, 87 - 08029 - Barcelona (Spain)
Tel (+34) 93/410 75 70 - Fax (+34) 93/419 45 91


---(end of broadcast)---
TIP 2: Don't 'kill -9' the postmaster


Re: [SQL] How to join several selects

2005-08-24 Thread Sean Davis
On 8/24/05 9:46 AM, "Josep Sanmartí" <[EMAIL PROTECTED]> wrote:

> Hello,
> I have a 'big' problem:
> I have the following table users(name, start_time, end_time), a new row
> is set whenever a user logs into a server.  I want to know how many
> users have logged in EVERYDAY between 2 different dates. The only idea
> that I have is making several select (one for each day):
>   SELECT COUNT(name) FROM users WHERE start_time between "startDate"
> and "startDate+1"
>   SELECT COUNT(name) FROM users WHERE start_time between "startDate+1"
> and "startDate+2"
>   ...
> I would like to know if its possible to make it in 1 sql statement or
> just which is the best efficient way to solve it.
> By the way, I use Postgres 7.4.


See:

http://www.postgresql.org/docs/8.0/interactive/sql-select.html#SQL-UNION
like:

SELECT COUNT(name) FROM users WHERE start_time between "startDate"
 and "startDate+1"
union
SELECT COUNT(name) FROM users WHERE start_time between "startDate+1"
 and "startDate+2"

Sean


---(end of broadcast)---
TIP 4: Have you searched our list archives?

   http://archives.postgresql.org


Re: [SQL] How to join several selects

2005-08-24 Thread Rod Taylor
On Wed, 2005-08-24 at 15:46 +0200, Josep Sanmartí wrote:
> Hello,
> I have a 'big' problem:
> I have the following table users(name, start_time, end_time), a new row 
> is set whenever a user logs into a server.  I want to know how many 
> users have logged in EVERYDAY between 2 different dates. The only idea 
> that I have is making several select (one for each day):
> SELECT COUNT(name) FROM users WHERE start_time between "startDate" 
> and "startDate+1"
> SELECT COUNT(name) FROM users WHERE start_time between "startDate+1" 
> and "startDate+2"

You have columns named "startDate+2" or are you adding 2 days to a
column named "startDate"?

-- 


---(end of broadcast)---
TIP 9: In versions below 8.0, the planner will ignore your desire to
   choose an index scan if your joining column's datatypes do not
   match


Re: [SQL] How to join several selects

2005-08-24 Thread Michael Glaesemann


On Aug 24, 2005, at 11:03 PM, Sean Davis wrote:

On 8/24/05 9:46 AM, "Josep Sanmartí" <[EMAIL PROTECTED]>  
wrote:




Hello,
I have a 'big' problem:
I have the following table users(name, start_time, end_time), a  
new row

is set whenever a user logs into a server.  I want to know how many
users have logged in EVERYDAY between 2 different dates. The only  
idea

that I have is making several select (one for each day):
  SELECT COUNT(name) FROM users WHERE start_time between "startDate"
and "startDate+1"
  SELECT COUNT(name) FROM users WHERE start_time between "startDate 
+1"

and "startDate+2"
  ...
I would like to know if its possible to make it in 1 sql statement or
just which is the best efficient way to solve it.
By the way, I use Postgres 7.4.




See:

http://www.postgresql.org/docs/8.0/interactive/sql-select.html#SQL- 
UNION

like:

SELECT COUNT(name) FROM users WHERE start_time between "startDate"
 and "startDate+1"
union
SELECT COUNT(name) FROM users WHERE start_time between "startDate+1"
 and "startDate+2"


I'm guessing he wants something more like this, so he knows which  
period is which:


SELECT count_1, count_2
FROM (
SELECT COUNT(name) AS count_1
FROM users
WHERE start_time BETWEEN startDate AND startDate + 1
) as period_1
CROSS JOIN (
SELECT COUNT(name) AS count_2
FROM users
WHERE start_time BETWEEN startDate + 1 AND startDate + 2
) as period_2

Though, you could do the same thing using UNION like this:

SELECT 'period_1'::text as period, COUNT(name) AS num_of_users
FROM users
WHERE start_time BETWEEN startDate AND startDate + 1

UNION

SELECT 'period_2'::text as period, COUNT(name) AS num_of_users
FROM users
WHERE start_time BETWEEN startDate + 1 AND startDate + 2

And of course, using EXPLAIN ANALYZE will help decide which is more  
performant.


Michael Glaesemann
grzm myrealbox com



---(end of broadcast)---
TIP 4: Have you searched our list archives?

  http://archives.postgresql.org


Re: [SQL] Number of rows in a cursor ?

2005-08-24 Thread Bo Lorentsen

Michael Fuhr wrote:


...which is the same reason a cursor doesn't know how many rows it will
fetch until you fetch them all (or MOVE to the end of the cursor,
which fetches the rows internally).
 

So, Postgresql is not hidding something for me, it just, like me, don't 
know ?


/BL

---(end of broadcast)---
TIP 2: Don't 'kill -9' the postmaster


Re: [SQL] How to join several selects

2005-08-24 Thread Zac

Josep Sanmartí wrote:

Hello,
I have a 'big' problem:
I have the following table users(name, start_time, end_time), a new row 
is set whenever a user logs into a server.  I want to know how many 
users have logged in EVERYDAY between 2 different dates. The only idea 
that I have is making several select (one for each day):
   SELECT COUNT(name) FROM users WHERE start_time between "startDate" 
and "startDate+1"
   SELECT COUNT(name) FROM users WHERE start_time between "startDate+1" 
and "startDate+2"

   ...
I would like to know if its possible to make it in 1 sql statement or 
just which is the best efficient way to solve it.

By the way, I use Postgres 7.4.

Thanks!


SELECT
date_trunc('day', start_time) as day, count(name)
FROM
users
WHERE
start_time between "startDate" AND "endDate"
GROUP BY
day;

---(end of broadcast)---
TIP 1: if posting/reading through Usenet, please send an appropriate
  subscribe-nomail command to [EMAIL PROTECTED] so that your
  message can get through to the mailing list cleanly


Re: [SQL] Number of rows in a cursor ?

2005-08-24 Thread Michael Fuhr
On Wed, Aug 24, 2005 at 04:29:00PM +0200, Bo Lorentsen wrote:
> Michael Fuhr wrote:
> >...which is the same reason a cursor doesn't know how many rows it will
> >fetch until you fetch them all (or MOVE to the end of the cursor,
> >which fetches the rows internally).
>
> So, Postgresql is not hidding something for me, it just, like me, don't 
> know ?

Right -- when you open a cursor PostgreSQL doesn't know how many
rows it will return.  PostgreSQL selects a query plan based on an
*estimate* of how many rows the query will return, but until you
fetch all the rows you can't know for sure how many rows there will
be.

-- 
Michael Fuhr

---(end of broadcast)---
TIP 9: In versions below 8.0, the planner will ignore your desire to
   choose an index scan if your joining column's datatypes do not
   match


Re: [SQL] how to do a select * and decrypt a column at the same

2005-08-24 Thread Scott Marlowe
On Tue, 2005-08-16 at 14:53, The One wrote:
> Hello,
>  
> I have a table with one encrypted column.
> How can I do a select statement such that it will select all columns
> from the table and at the same time will decrypt it too?

A view should be able to do that...

---(end of broadcast)---
TIP 2: Don't 'kill -9' the postmaster


[SQL] Can EXCEPT Be Used for To Solve This Problem?

2005-08-24 Thread Lane Van Ingen
Given the following data in a table named 'foo' :
 id   update_time   description
 22005-08-24 00:10:00   transaction1
 22005-08-24 00:22:00   transaction2
 22005-08-24 00:34:00   transaction3
 22005-08-24 00:58:00   transaction4

I want to select 2nd oldest transaction from foo (transaction 3). The
solution below
works, but I think there may be a better way. Does anyone else have a better
idea?

select * from foo f1
  join
  (select id, update_time
   from foo
   except
   select id, max(update_time) as update_time
   from foo
   group by id) f2
using (id, update_time)
order by 1, 2 desc limit 1;



---(end of broadcast)---
TIP 2: Don't 'kill -9' the postmaster


Re: [SQL] Can EXCEPT Be Used for To Solve This Problem?

2005-08-24 Thread Michael Fuhr
On Wed, Aug 24, 2005 at 04:47:16PM -0400, Lane Van Ingen wrote:
> Given the following data in a table named 'foo' :
>  id   update_time   description
>  22005-08-24 00:10:00   transaction1
>  22005-08-24 00:22:00   transaction2
>  22005-08-24 00:34:00   transaction3
>  22005-08-24 00:58:00   transaction4
> 
> I want to select 2nd oldest transaction from foo (transaction 3). The
> solution below
> works, but I think there may be a better way. Does anyone else have a better
> idea?

Do you want the 2nd oldest transaction from the entire table?  If
so then the following should work:

SELECT *
FROM foo
ORDER BY update_time
OFFSET 1
LIMIT 1;

If that's not what you're after then please elaborate.

-- 
Michael Fuhr

---(end of broadcast)---
TIP 6: explain analyze is your friend


Re: [SQL] Can EXCEPT Be Used for To Solve This Problem?

2005-08-24 Thread Tom Lane
"Lane Van Ingen" <[EMAIL PROTECTED]> writes:
> I want to select 2nd oldest transaction from foo (transaction 3).

Can't you just do

select * from foo order by update_time desc offset 1 limit 1

regards, tom lane

---(end of broadcast)---
TIP 3: Have you checked our extensive FAQ?

   http://www.postgresql.org/docs/faq


Re: [SQL] Can EXCEPT Be Used for To Solve This Problem?

2005-08-24 Thread Michael Fuhr
On Wed, Aug 24, 2005 at 05:34:49PM -0600, Michael Fuhr wrote:
> On Wed, Aug 24, 2005 at 04:47:16PM -0400, Lane Van Ingen wrote:
> > Given the following data in a table named 'foo' :
> >  id   update_time   description
> >  22005-08-24 00:10:00   transaction1
> >  22005-08-24 00:22:00   transaction2
> >  22005-08-24 00:34:00   transaction3
> >  22005-08-24 00:58:00   transaction4
> > 
> > I want to select 2nd oldest transaction from foo (transaction 3). The

I just noticed that transaction3 isn't the 2nd oldest, it's the 3rd
oldest and the 2nd newest.  What are you really trying to do?

> > solution below
> > works, but I think there may be a better way. Does anyone else have a better
> > idea?
> 
> Do you want the 2nd oldest transaction from the entire table?  If
> so then the following should work:
> 
> SELECT *
> FROM foo
> ORDER BY update_time
> OFFSET 1
> LIMIT 1;

Flaw: this query assumes that the 2nd record in the ordered result
set is the 2nd oldest transaction, which isn't necessarily true.
If the update_time values aren't unique, then the 2nd record could
have the oldest time and not the 2nd oldest time.  Is that why you
were using EXCEPT?  To exclude all instances of the oldest time?

-- 
Michael Fuhr

---(end of broadcast)---
TIP 4: Have you searched our list archives?

   http://archives.postgresql.org


Re: [SQL] Number of rows in a cursor ?

2005-08-24 Thread Bo Lorentsen

Michael Fuhr wrote:


Right -- when you open a cursor PostgreSQL doesn't know how many
rows it will return.  PostgreSQL selects a query plan based on an
*estimate* of how many rows the query will return, but until you
fetch all the rows you can't know for sure how many rows there will
be.
 

So if i make a but data set as result of a cursor I only "pay" for the 
rows I actually fetch ?


/BL

---(end of broadcast)---
TIP 1: if posting/reading through Usenet, please send an appropriate
  subscribe-nomail command to [EMAIL PROTECTED] so that your
  message can get through to the mailing list cleanly