Re: [SQL] How do I convert an integet to a timestamp?

2005-09-13 Thread Eugene E.

Wei wrote:

Hi.

I followed the doc and tried "select CAST(1126547334 AS timestamp)" and
I only got an error response that says: ERROR:  cannot cast type
integer to timestamp without time zone.

What is the proper way to do the conversion?


select 123456789::abstime::timestamp;

---(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] SELECT: retrieve only 2 rows next to known row

2005-09-13 Thread Marcin Piotr Grondecki
Dnia Fri, Sep 09, 2005 at 04:23:00PM +0400.424.r. ([EMAIL PROTECTED]), Nikolay 
Samokhvalov napisal(a):
> Hi,
> 
> My knowledge of PostgreSQL's SQL is not good, but I know ISO/ANSI
> SQL:2003 (basics) quite well.
> 
> I've encountered with following task. I have one SELECT statement with
> ORDER BY clause; and know, that result set for this SELECT contains
> row with ID = 1000 (just for example).
> I don't know the position of this row in result set, but I want to
> retrieve 2 rows that are next to this  one.
> 
> First of all, I don't want (cannot) write PL/pgSQL function.
> 
> So, I should use CREATE TEMP SEQUENCE to associate all rows in result
> set with their order numbers (in MySQL. for example, I would use
> temporary variable num in SELECT: something like 'select @num := @num
> + 1', but here I cannot, can't I?)
> 
> Then, as I know, PostgreSQL doesn't support standard statement WITH,
> that probaby would help me with this task.
> 
> Any ways to solve this problem? Is it possible to make only one query?
> (at least with one row in result set - e.g., with the row _following_
> after my one)
> 
> I'd appreciate any help.
I've created some model of your problem.
CREATE TABLE "foo" ("id" serial, "val" text); -- (this "model" is named "foo", 
as you see :]).
Then I've inserted some values; 'SELECT * FROM "foo" ORDER BY ("val")' gives 
now:
 id | val
+-
  4 | a
  2 | b
  3 | c
  1 | d
  5 | e
  7 | f
  6 | g
Now we'd like to "find" row where val='d' and this row neighbours.
This question results with our "center" row and next one:
SELECT * FROM "foo" WHERE ("val">='d') ORDER BY ("val") LIMIT 2;
Previous row:
SELECT * FROM "foo" WHERE ("val"<'d') ORDER BY ("val") DESC LIMIT 1;
Is it expected result in task similar to yours?
Ah, 'Is it possible to make only one query?'.
Yep, by "unioning" two given questions:
(SELECT * FROM "foo" WHERE ("val">='d') ORDER BY ("val") LIMIT 2) UNION 
ALL (SELECT * FROM "foo" WHERE ("val"<'d') ORDER BY ("val") DESC LIMIT 1) ORDER 
BY ("val");
Effect:
 id | val
+-
  3 | c
  1 | d
  5 | e
As desired.
Now we can change field/s to watch values for (we were looking into "val", now 
we'd like to do same work on "id"). 
(SELECT * FROM "foo" WHERE ("id">='5') ORDER BY ("id") LIMIT 2) UNION 
ALL (SELECT * FROM "foo" WHERE ("id"<'5') ORDER BY "id" DESC LIMIT 1) ORDER BY 
("id");
It gives: 
 id | val
+-
  4 | a
  5 | e
  6 | g
Once again result as expected in our dreams! :]]]

Change table into desired one, change ordering into desired one and... your 
problem'll be solved, I hope... :]?

regards
-- 
Marcin Piotr Grondecki


pgpG5iHUzmcVy.pgp
Description: PGP signature


Re: [SQL] refer a column as a varible name?

2005-09-13 Thread gherzig
Im using pg 8.0. I personally dont have problem in using pl/perl, but none
of my companions is a perl programmer, so i guess the IF..THEN seems to be
an easy (altough not preferable) solution.

Thank you very much Michael.

Gerardo
> On Mon, Sep 12, 2005 at 12:21:22PM -0300, [EMAIL PROTECTED] wrote:
>> suppose the
>>
>> type mycolumn as (field1, varchar, field2 varchar)
>> and
>>
>> field_name = ''field1''
>>
>> and returnValue declared as mycolumn
>> ...
>> can i say returnValue.$field_name = ''ok''?
>
> To achieve this in PL/pgSQL you'll need to use a conditional statement
> (IF field_name = 'field1' THEN ...).  I'm not sure if a solution
> involving EXECUTE is possible; if so then it's probably non-obvious.
>
> What version of PostgreSQL are you using, and do you have a requirement
> to use PL/pgSQL?  In 8.0 PL/Perl can return composite types and such
> an assignment would be trivial:
>
> CREATE TYPE mycolumn AS (field1 varchar, field2 varchar);
>
> CREATE FUNCTION foo(varchar) RETURNS mycolumn AS $$
> my $field_name = $_[0];
> my $returnValue = {$field_name => "ok"};
> return $returnValue;
> $$ LANGUAGE plperl IMMUTABLE STRICT;
>
> SELECT * FROM foo('field1');
>  field1 | field2
> +
>  ok |
> (1 row)
>
> SELECT * FROM foo('field2');
>  field1 | field2
> +
> | ok
> (1 row)
>
> --
> 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
>
>


-- 
Gerardo Herzig
Direccion General de Organizacion y Sistemas
Facultad de Medicina
U.B.A.

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

   http://archives.postgresql.org