[SQL] pivot-like transformation

2004-06-20 Thread Torsten Lange
Hello,   
I have a table with measurement values and columns like this:   
analyses(id, sample_id, parameter[temperatur...], value, unit[°C...], error)   
   
With PL/PgSQL at the end I want try to perform a pivot-like arrangement of 
these data:   
sample_id|Temperature [°C]|parameter2 [mg/L]|...|parameterN [L/year]   
-++-+---+---   
5|   23.00|0.036|...|  35.1   
   
My first attempts to only give back the original table within a function   
failed. There are tons of examples to select one value into a variable... 
But for more I browsed through the net and my book but I couldn't find 
anything that works. I'm new to PL/PgSQL. Could anyone show me a _very_simple_ 
example of how to to read (the columns I like) from a table and return the 
columns (I like) using tablename%rowtype and an other possiblity if  
exists.  
  
I also found very few sources about handling of arrays and how to fill them up  
with query results... things like this. Does anyone know a more comprehensive  
source?  
  
Thank 
you, 
Torsten 

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


Re: [SQL] pivot-like transformation

2004-06-21 Thread Torsten Lange
Joe Conway schrieb:
Torsten Lange wrote:
Hello,   I have a table with measurement values and columns like 
this:   analyses(id, sample_id, parameter[temperatur...], value, 
unit[?C...], error)  With PL/PgSQL at the end I want try 
to perform a pivot-like arrangement of these data:   
sample_id|Temperature [?C]|parameter2 [mg/L]|...|parameterN 
[L/year]   
-++-+---+---   
5|   23.00|0.036|...|  
35.1   

Not a direct answer with respect to plpgsql, but for pivot table 
functionality see the contrib/tablefunc function crosstab().
I don't like the idea to have a table in another schema in order to 
create a pivot-like arrangement. Anyway, this wasn't really my need. I 
got stuck at the very basics (sorry for that) - reading data from a 
table, doing something with them, like printing to the screen. I tried 
the example at
http://www.postgresql.org/docs/7.4/static/plpgsql-declarations.html#PLPGSQL-DECLARATION-ROWTYPES
and got the error message something like "table_name returns more then 
one row...". I cannot recall it accurately since I did it at home and 
now I'm at work.

I also found very few sources about handling of arrays and how to 
fill them up  with query results... things like this. Does anyone 
know a more comprehensive  source?  

Have you looked at the online docs?
http://www.postgresql.org/docs/7.4/static/arrays.html
http://www.postgresql.org/docs/7.4/static/functions-array.html
http://www.postgresql.org/docs/7.4/static/functions-comparisons.html
http://www.postgresql.org/docs/7.4/static/sql-expressions.html#SQL-SYNTAX-ARRAY-CONSTRUCTORS
...at first
Best regards, Torsten
---(end of broadcast)---
TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]


Re: [SQL] Access Update error: solution.

2010-05-21 Thread Torsten Lange

I think I also found one useful switch. In the ODBC-driver's setting there is 
one "extra"  or so field, where 4 possible values can be entered: 0x0, 0x1, 
0x2... If I remember right with 0x2 the driver mimics an SQL-Server auto 
increment, which Access acknowledges.

However, does anyone knows good resources to learn more about triggers? Can 
also be a book to buy. I need an after insert/update trigger to update a 
PostGIS geometry column of a table after someone has typed in a x,y-location 
into x and y columns.

Thx,Torsten



Am Freitag, 21. Mai 2010 17:23:12 schrieb Little, Douglas:
> A comment on the MS access/PG problem.
>
> We experienced this problem as well and I isolated the problem to access's
> support of timestamp. PG defaults to timestamp(6),   While access only
> supports timestamp(2).
>
> When access fills the grid from the table, the values are truncated to
> ts(2).
>
> As the grid is updated, a sql update statement is prepared in the
> background. all fields except for those updated(by the grid) are used in
> the construction of the where clause. For most cases this means all of
> timestamp columns are compared.  Ie Update remote x
> Where local.ts1 = remote.ts1  and .
> The PG timestamp is compared to the local access copy.
> Because of the truncation  no row is found in PG, and Access reports that
> someone else updated the record.
>
> We found the solution to be that all timestamp columns needed to be set to
> timestamp(0).  Once we did this the issue went away.
>
> Back to the original problem.
> Serial is just a notational convenience. When created the pg engine will
> rewrite the statement to 1) Generate a sequence, then 2) create column as
> int and a default value as nextval(seqName).   The datatype remains
> recorded as Serial, which is not a standard SQL datatype.
>
> You can manually do the same yourself - create the sequence, then assign
> the column default, or easier - create the table as serial, then change the
> datatype to int. PG will automatically create the seq and assign the
> default.  Changing the datatype will leave the default and sequence intact.
>
> Once datatype is int, Access will recognize it.
>
> Doug
>
>
> -Original Message-
> From: pgsql-sql-ow...@postgresql.org
> [mailto:pgsql-sql-ow...@postgresql.org] On Behalf Of Craig Ringer Sent:
> Friday, May 21, 2010 3:16 AM
> To: Richard Broersma
> Cc: tla...@gwdg.de; pgsql-sql@postgresql.org
> Subject: Re: [SQL]
>
> On 21/05/2010 9:56 AM, Richard Broersma wrote:
> > On Thu, May 20, 2010 at 5:52 PM,  wrote:
> >> I'm new to triggers in PostgreSQL. I have to create a trigger on insert
> >> to increment a sequence to overcome MS-Access' limitation in
> >> acknowledging serial "datatype".
> >
> > Uh?  Access doesn't need to acknowledge the serial datatype.
> > At-least in recent versions auto increment is recognized by MS-Access
> > just fine (I'm guessing this is due to the Return clause which the
> > ODBC driver automatically calls).
>
> Really?
>
> I had problems with Access complaining that the object it just inserted
> had vanished, because the primary key Access had in memory (null) didn't
> match what was stored (the generated PK). I had to fetch the next value
> in the PK sequence manually and store it in Access's record before
> inserting it to work around this.
>
> I wouldn't be surprised if this actually turned out to just require some
> bizarre ODBC driver parameter change, but I never figured it out and I
> couldn't find any info about it on the 'net.
>
> For the original poster: I posted some information about this at the
> time I was working on it, so search the archives of this list for MS
> Access.
>
> I eventually ditched Access entirely as the user who was demanding the
> use of MS Access relented (phew!), so I put together a simple web-app to
> do what they wanted in a day. Hopefully I'll never need to go near ODBC
> again, because it's a truly "special" way to talk to PostgreSQL.
>
> --
> Craig Ringer
>

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