[SQL] How to realize ROW_NUMBER() in 8.3?

2011-04-20 Thread Emi Lu

Hello,

ROW_NUMBER() is only ready in 8.4. For 8.3, is there a simple way to get 
row_number


select row_number(), col1, col2...
FROM   tableName

Thanks a lot!

Ding Ye

--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql


Re: [SQL] How to realize ROW_NUMBER() in 8.3?

2011-04-20 Thread Vibhor Kumar

On Apr 20, 2011, at 9:15 PM, Emi Lu wrote:

> ROW_NUMBER() is only ready in 8.4. For 8.3, is there a simple way to get 
> row_number
> select row_number(), col1, col2...
> FROM   tableName


Following is a link of deepsz which has a way of implementation of rownum.
http://www.depesz.com/index.php/2007/08/17/rownum-anyone-cumulative-sum-in-one-query/

Thanks & Regards,
Vibhor Kumar
EnterpriseDB Corporation
The Enterprise PostgreSQL Company
vibhor.ku...@enterprisedb.com
Blog:http://vibhork.blogspot.com


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql


Re: [SQL] How to realize ROW_NUMBER() in 8.3?

2011-04-20 Thread Oliveiros d'Azevedo Cristina

If your table is not terribly big, you can
try something like

SELECT a.col1,a.col2, COUNT(*) as row_number
FROM yourTable a,yourTable b
WHERE a.col1 >= b.col1 -- I'm assuming col1 is primary key
GROUP BY a.col1,a.col2
ORDER BY row_number

This is pure SQL, should work in every version...

Best,
Oliveiros

- Original Message - 
From: "Emi Lu" 

To: 
Sent: Wednesday, April 20, 2011 4:45 PM
Subject: [SQL] How to realize ROW_NUMBER() in 8.3?



Hello,

ROW_NUMBER() is only ready in 8.4. For 8.3, is there a simple way to get 
row_number


select row_number(), col1, col2...
FROM   tableName

Thanks a lot!

Ding Ye

--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql


--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql


[SQL]

2011-04-20 Thread Humair Mohammed


I am running into a behavior with a postgresql function with a SETOF 
refcursor's returning multiple columns. Not sure if there is a different way to 
retrieve a SETOF refcursor's with variable columns? Alternatively can I return 
a primitive value and a refcursor from the same function. I tried specifying 
this as OUT parameters without any luck. In Oracle you can pass this in 
functions:
Platform:postgresql-x64-9.0 (PostgreSQL 9.0.1, compiled by Visual C++ build 
1500, 64-bit)Java1.6JDBC4 Postgresql Driver, Version 9.0-801

Function:CREATE OR REPLACE FUNCTION test()  RETURNS SETOF refcursor 
AS$BODY$DECLAREref1 refcursor;ref2 refcursor;BEGIN  OPEN ref1 FOR 
SELECT 1; RETURN NEXT ref1;  OPEN ref2 FOR SELECT 2, 3; 
RETURN NEXT ref2;  RETURN;END;$BODY$  LANGUAGE plpgsql

Java Code:CallableStatement cs = conn.prepareCall("{ call test() }");ResultSet 
rs = cs.executeQuery();
while (rs.next()) { System.out.println(rs.getString(1));ResultSet rs2 = 
(ResultSet)rs.getObject(1); while (rs2.next()) {
ResultSetMetaData rsmd = rs2.getMetaData(); int numberOfColumns = 
rsmd.getColumnCount();System.out.println("numberOfColumns: " + 
numberOfColumns);  System.out.println(rs2.getString(1));   
System.out.println(rs2.getString(2));   }}
Output:numberOfColumns: 11org.postgresql.util.PSQLException: 
The column index is out of range: 2, number of columns: 1.at 
org.postgresql.jdbc2.AbstractJdbc2ResultSet.checkColumnIndex(AbstractJdbc2ResultSet.java:2680)
at 
org.postgresql.jdbc2.AbstractJdbc2ResultSet.checkResultSet(AbstractJdbc2ResultSet.java:2697)
at 
org.postgresql.jdbc2.AbstractJdbc2ResultSet.getString(AbstractJdbc2ResultSet.java:1872)
at PgBlob.test(PgBlob.java:64)at PgBlob.main(PgBlob.java:37)
It appears the second result-set takes in the number of columns from the first 
irrespective of the number of columns from the second. If the change the 
function to return 2 refcursor's with same number of columns then it works as 
expected.
Function:CREATE OR REPLACE FUNCTION test()  RETURNS SETOF refcursor 
AS$BODY$DECLAREref1 refcursor;ref2 refcursor;BEGIN  OPEN ref1 FOR 
SELECT 1, null; RETURN NEXT ref1;  OPEN ref2 FOR SELECT 2, 3;   
  RETURN NEXT ref2;  RETURN;END;$BODY$  LANGUAGE plpgsql
Output:numberOfColumns: 214numberOfColumns: 
223   

[SQL] help on select

2011-04-20 Thread Saulo VenĂ¢ncio
Hi guys,
I need your help.
I have a table called medidas, in this table i have some ocurrences that has
id_medida(primary key) id_ponto (sec_key) and also datetime field as
timestamp.
i would like to know from a set of idpontos, e.g. 10,11,23,24 how can i
get the most recent date that is common to all??
for example, if idponto das date 2011-02-03 but none of others have this
date in the db i dont want this. i want one common for all..
thanks.

*---
**Saulo B. M. VenĂ¢ncio*

Control and Automation Eng. Student
*Associate in Business Management*

T +55 4884121486
*E saulo.venan...@gmail.com
*