[SQL] Re: Stored Procedures?

2001-05-28 Thread Reinoud van Leeuwen

On Thu, 24 May 2001 13:45:46 + (UTC), [EMAIL PROTECTED] (Tod
McQuillin) wrote:

>Postgres doesn't have stored procedures in the same way that other
>databases like oracle and sybase do.  But it does have stored functions,
>and they can be used in almost exactly the same way.

In Sybase I am used to the fact that stored procedures can return a
result set to the client. Something like;

create procedure sp_example (@param int) as
begin
  select bla 
  from tablename 
  where somecolumn = @param
end

(of course in pratice stored procedures get a lot more complex than
this :-)

Is something like this posstible in PostgreSQL?

-- 
__
"Nothing is as subjective as reality"
Reinoud van Leeuwen   [EMAIL PROTECTED]
http://www.xs4all.nl/~reinoud
__

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



[SQL] Re: Date manipulation

2001-06-05 Thread Reinoud van Leeuwen

On Thu, 31 May 2001 17:24:54 + (UTC), [EMAIL PROTECTED] (Mark)
wrote:

>
>How does one perform date manipulation within SQL?  For example, SQL
>Server has a dateadd() function that takes a date part, scalar, and the
>date to manipulate.
>
>I have a query that determines the number of days that are between now
>and a particular date that looks something like this:
>
>select datediff (dd, now (), column) as difference_in_days from ...
>
>Anything available short of coding my own function?

Yes: see 
http://www.postgresql.bit.nl/devel-corner/docs/user/functions-formatting.html
and
http://www.postgresql.bit.nl/devel-corner/docs/user/functions-datetime.html
-- 
__
"Nothing is as subjective as reality"
Reinoud van Leeuwen   [EMAIL PROTECTED]
http://www.xs4all.nl/~reinoud
__

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



Re: [SQL] stored procedures: sybase -> postgreSQL ?

2002-09-10 Thread Reinoud van Leeuwen

On Mon, 9 Sep 2002 18:16:07 + (UTC), [EMAIL PROTECTED] (Charles
Hauser) wrote:

>I am trying to port a Sybase table create script to one usable for
>postgreSQL.
>
>(note I am not a DBA)
>
>In particular I am not well versed on how to use/recode the stored
>procedures such as that in the example below.
>
>ALTER TABLE DnaFragment
>ADD PRIMARY KEY (dna_fragment_id)
>go
> 
> exec sp_primarykey DnaFragment,
>   dna_fragment_id
>go
> 
> exec sp_bindrule DnaFragment_type_rule, 'DnaFragment.type'
> exec sp_bindefault Set_To_Current_Date,
>'DnaFragment.date_last_modified'
> exec sp_bindefault Set_to_False, 'DnaFragment.is_obsolete'
>go

Postgresql and Sybase are a lot different in many ways. Lot of the
Postgresql is clearly borrowed from oracle ways of thinking (mind that
this is not neccessary better or worse, it is different).
When using standard SQL, the differences are not really much.
Postgresql uses that ANSI outer join syntax instead of the Sybase =*
notation.
But when you start using Sybase specific features the differences will
become more clean. Rules and defaults are not defined as seperate
entities, so you have to define them inside the table definitions. The
alter table statement is by the way explained on
http://www.postgresql.org/idocs/index.php?sql-altertable.html.

If you want to convert one database definition to another, you might
want to use a tool like Sybase Powerdesigner. You can read the Sybase
definition, and create a Postgresql definition from there. An
evaluation version (fully functional for a limited time) is
downloadable from the Sybase website

-- 
______________
"Nothing is as subjective as reality"
Reinoud van Leeuwen   [EMAIL PROTECTED]
http://www.xs4all.nl/~reinoud

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

http://archives.postgresql.org



Re: [SQL] Database Design tool

2002-11-01 Thread Reinoud van Leeuwen
On Wed, 30 Oct 2002 14:35:23 + (UTC), [EMAIL PROTECTED]
(Johannes Lochmann) wrote:

>> Can anybody take me reference on Database design tool with PostgreSQL 
>> support.

You can use PowerDesigner from Sybase. Windows only. A fully
functional trial version (45 days) is downloadable at www.sybase.com.
it has:
- support for al major databases (Postgresql, Sybase, Oracle, MS SQL,
Mysql)
- support to generate triggers automatically
- nice reporting features
- ability to reverse engineer existing databases (through ODBC of from
a file)
- central repository (database) storage, or storage of your model in
an XML file (which I use to auto generate perl classes from my saved
model)
- UML modeling
- lots of other buzzword-compliant features ";-)

It is an expensive tool, but if you do serious design work, you'll
need a tool like this

-- 
__
"Nothing is as subjective as reality"
Reinoud van Leeuwen   [EMAIL PROTECTED]
http://www.xs4all.nl/~reinoud

---(end of broadcast)---
TIP 3: 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] Insert a description while creating a table

2003-08-14 Thread Reinoud van Leeuwen
On Wed, Aug 13, 2003 at 02:54:59PM +0200, Christoph Haller wrote:
> \dd test shows
> 
>  Object descriptions
>  Schema | Name | Object | Description
> +--++-
> (0 rows)
> 
> This is odd. OK, I know the doc says
> \dd [ pattern ]
> 
> Shows the descriptions of objects matching the pattern, or of all
> visible objects if no argument is given. But in either case,
> only objects that have a description are listed. ("Object" covers
> aggregates, functions, operators, types, relations (tables, views,
> indexes, sequences, large objects), rules, and triggers.) For
> example:
> 
> Nothing about columns.
> But what is the purpose of comments on columns if you can only get them
> via
> select * from pg_description where

Have you tried \d+ tablename? (sorry for the long lines ";-)

drbob=# \dd object
Object descriptions
 Schema |  Name  | Object |   Description
+++-
 public | object | table  | The table that holds all instances of objects.
(1 row)

drbob=# \d+ object
   
   
Table "public.object"
  Column  |Type |  
Modifiers   |  
 
Description
--+-+--+-
 id   | integer | not null default 
nextval('shared_ids'::text) | (global) unique ID for this table
 object_def_id| integer | not null 

| foreign key to object_def.id
 name | character varying(60)   |  

| natural name of this object
 superobject_id   | integer |  

| foreign key to object.id. This field is only filled in when the this 
object is a subobject of another object (EG a port in a router)
 subobject_def_id | integer |  

| foreign key to subobject_def.id. This field is only filled in when this 
object is a subobject of another object (EG a port in a router)
 has_subobject| boolean | not null default 'FALSE' 

| boolean indicating that this object instance has subobjects (EG router 
that has ports)
 date_added   | timestamp without time zone |  

| date this record was added to the database
 user_added   | character varying(20)   |  

| username that added this record to the database
 date_changed | timestamp without time zone |  

| date this record was last changed
 user_changed | character varying(20)   |  

| username that made the last change to this record
Indexes: pk_object primary key btree (id),
 object_pk unique btree (id),
 object_object_def_fk btree (object_def_id),
 object_object_fk btree (superobject_id),
 object_subobject_def_fk btree (subobject_def_id)
Triggers: tdb_object,
  tiua_object,
      tiub_object
-- 
__
"Nothing is as subjective as reality"
Reinoud van Leeuwen[EMAIL PROTECTED]
http://www.xs4all.nl/~reinoud
__

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

   http://archives.postgresql.org


Re: [SQL] Copying rows between tables?

2004-01-13 Thread Reinoud van Leeuwen
On Tue, Jan 13, 2004 at 09:23:48AM -0700, Steve Wampler wrote:
> 
> I know I can select from one table into a new table, but is
> there a way to select from one table into an existing table
> (aside from copying everything to a file, editing the file
> and then copying from that file)?  Without destroying existing
> entries, of course...

insert into desttable (col1, col2, col3)
select col1, col2, col3 from sourcetable
where somecol = somevalue;


-- 
__
"Nothing is as subjective as reality"
Reinoud van Leeuwen[EMAIL PROTECTED]
http://www.xs4all.nl/~reinoud
__

---(end of broadcast)---
TIP 3: 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] comparing nulls

2004-01-20 Thread Reinoud van Leeuwen
On Tue, Jan 20, 2004 at 07:13:12PM +0530, Kenneth Gonsalves wrote:
> in postgres7.1 i had a table where an integer field could be null. There was 
> no default value. a select statement like so:
> 'select * from table where field = null' 
> would give all the rows where that field had no value.
> on porting to 7.3.2, this doesnt work. How to do this?

Because NULL can be read as "unknown". It does not make much sense to 
test wheter a value of an integer is numerically equal to unknown. That is 
why there is the IS operator:

where field IS null

-- 
__
"Nothing is as subjective as reality"
Reinoud van Leeuwen[EMAIL PROTECTED]
http://www.xs4all.nl/~reinoud
__

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


Re: [SQL] problem porting MySQL SQL to Postgres

2004-04-15 Thread Reinoud van Leeuwen
On Thu, Apr 15, 2004 at 11:25:59AM +0100, Dan Field wrote:
> I've stumbled across a query I don't quite understand the error message 
> for.
> 
> This query is pulled from a working MySQL setup:
> 
> SELECT
>   DEWEY_ID, DEWEY_HUNDREDS, DEWEY_TENS, DEWEY_ONES,  DEWEY_POINT_ONES, 
> DEWEY_POINT_TENS, 
>   DEWEY_POINT_HUNDREDS, DEWEY_POINT_THOUSANDS,DEWEY_TYPE, 
>   DEWEY_LANG, DEWEY_SUBJECT  FROM lu_dewey
> WHERE
>   (DEWEY_HUNDREDS = 9) AND
>   (DEWEY_TENS >= 0) AND   
>   (DEWEY_TENS <= 9) AND
>   (DEWEY_ONES = 0 || DEWEY_ONES = NULL) AND
>   (DEWEY_POINT_ONES = 0 || DEWEY_POINT_ONES = NULL) AND
>   (DEWEY_POINT_TENS = 0 || DEWEY_POINT_TENS = NULL) AND
>   (DEWEY_POINT_HUNDREDS = 0 || DEWEY_POINT_HUNDREDS = NULL) AND
>   (DEWEY_POINT_THOUSANDS = 0 || DEWEY_POINT_THOUSANDS = NULL) AND

please use 'IS NULL' instead of '= NULL'

>   (DEWEY_TYPE = 't') AND
>   (DEWEY_LANG = 'en')
> ORDER BY DEWEY_TENS
> 
> 
> However I'm getting the following error:
> 
>  ERROR:  Unable to identify an operator '=' for types 'character' and 
> 'boolean' You will have to retype this query using an explicit cast.

can you post the output of '\d lu_dewey'?

-- 
__
"Nothing is as subjective as reality"
Reinoud van Leeuwen[EMAIL PROTECTED]
http://www.xs4all.nl/~reinoud
__

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


Re: [SQL] UNIQUE columnt depdening on other column???

2004-06-02 Thread Reinoud van Leeuwen
On Wed, Jun 02, 2004 at 02:39:45PM +0300, Andrei Bintintan wrote:
> Hi, 
>  
> I have a problem.
>  
> Let's say I have the following table:
> CREATE TABLE rekl_element(
> id serial PRIMARY KEY,
> active boolean NOT NULL DEFAULT 'y',
> num int4 NOT NULL,
> text varchar(10)
> );
> 
> Now I want that "num" column is "unique" but only for those columns that
> have active='y'. For the columns that have active='f' I don't care if num is
> unique or not. I'm asking this because num will be doubled some times. 

You can use a little trick for this. 
Add a column 'uniqueness' that has a default nextval ('mysequence'::text).
Make an unique index on the columns (num, uniqueness).
Obviously, this will work because the column uniqueness has unique values.

Now write a trigger that sets the uniqueness column to 0 when the active 
column equals 'y'. This will result in:

- unique num columns (or the index will fail) where active = 'y'
- arbitrary num colums (index will always be unique) where active = 'n'


 
-- 
__
"Nothing is as subjective as reality"
Reinoud van Leeuwen[EMAIL PROTECTED]
http://www.xs4all.nl/~reinoud
__

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


Re: [SQL] More efficient OR

2005-02-16 Thread Reinoud van Leeuwen
On Wed, Feb 16, 2005 at 11:02:59AM -0500, Keith Worthington wrote:
> Hi All,
> 
> In several of my SQL statements I have to use a WHERE clause that contains
> mutiple ORs.  i.e.
> 
> WHERE column1 = 'A' OR
>   column1 = 'B' OR
>   column1 = 'C'
> 
> Is there a more efficient SQL statement that accomplishes the same limiting
> functionality?

I do not know wheter it is more efficient in terms of execution, but I can
read this more efficiently:

WHERE column1 in ('A', 'B', 'C')


-- 
______________
"Nothing is as subjective as reality"
Reinoud van Leeuwen[EMAIL PROTECTED]
http://www.xs4all.nl/~reinoud
__

---(end of broadcast)---
TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]


Re: [SQL] Query about SQL in PostgreSQL

2005-04-19 Thread Reinoud van Leeuwen
On Tue, Apr 19, 2005 at 02:48:46AM -0700, Muhammad Nadeem Ashraf wrote:
> Hi,
> I am new user of PostGreSQL 8.0.1. While using it i faced following 
> issue. As SQL is Case insensetive Language So the Uper or Lower cases 
> are not significant. But while using the database there is problem.
> If i Create new Table with name (tblstudent) then upon SQL queries it 
> is fine to use Select * from tblstudent. However i face probel if i give 
> the name of table in Capital letters i.e. if the name is (tblStudent) 
> then upon using Select * from tblStudent, following error is appeard:
> ERROR:  relation "tblst" does not exist 
>  
> And if i use the query Select * from "tblStudent" then it works fine.
>  
>  
> Please help me out in this regard.

What exactly is the problem? Just use the names of tables you have 
created.

-- 
______________
"Nothing is as subjective as reality"
Reinoud van Leeuwen[EMAIL PROTECTED]
http://www.xs4all.nl/~reinoud
__

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


Re: [SQL] ORDER BY CASE ...

2006-02-13 Thread Reinoud van Leeuwen
On Mon, Feb 13, 2006 at 04:35:30PM +0100, Mario Splivalo wrote:
> Am I misusing the ORDER BY with CASE, or, what? :)
> 
> I have a table, messages, half dozen of columns, exposing here just
> three of them:
> 
> pulitzer2=# select id, "from", receiving_time from messages where
> service_id = 20 order by case when 5=5 then 2 else 3 end desc limit 5;

I'm not sure what you are trying to do here, but it seems that an order by 
statement should at least contain something that is part of the resultrow. 
"case when 5=5 then 2 else 3 end desc limit 5" does not contain any column 
to sort on. So I think it will evaluate to some constant value and not 
sorting is really done 

-- 
______
"Nothing is as subjective as reality"
Reinoud van Leeuwen[EMAIL PROTECTED]
http://www.xs4all.nl/~reinoud
__

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


Re: [SQL] oracle to postgres migration question

2010-06-15 Thread Reinoud van Leeuwen
On Tue, Jun 15, 2010 at 04:19:15PM -0400, Stephen Frost wrote:
> * Bruce Momjian (br...@momjian.us) wrote:
> > Joshua Gooding wrote:
> > > Hello,
> > > 
> > > I'm looking for the postgres equivalent of oracles: set numwidth 
> > > command.  Is there an equivalent?
> > 
> > If we knew what it did, we might be able to help you.
> 
> Changes the display-width for numeric values.  SQL*Plus will then
> right-justify the number based on the numwidth value.

Are you migrating the server or the client? This sounds like a client 
issue to me..

Reinoud

-- 
______________
"Nothing is as subjective as reality"
Reinoud van Leeuwenreinou...@n.leeuwen.net
http://reinoud.van.leeuwen.net kvk 27320762
__

-- 
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] Question on COUNT performance

2010-07-14 Thread Reinoud van Leeuwen
On Wed, Jul 14, 2010 at 09:58:10PM +1000, Anders ??stergaard Jensen wrote:
> Hello mailing list,
> 
> I have a performance problem with my postgres 8.4.4 database. The query is
> the following:
> 
> SELECT count(*) AS count_all FROM "plan_events" WHERE (f_plan_event_acl(17,
> plan_events.id))
> 
>QUERY PLAN
> 
> 
>  Aggregate  (cost=2859.77..2859.78 rows=1 width=0) (actual
> time=4641.720..4641.720 rows=1 loops=1)
>->  Seq Scan on plan_events  (cost=0.00..2851.44 rows=3331 width=0)
> (actual time=32.821..4640.116 rows=2669 loops=1)
>  Filter: f_plan_event_acl(17, id)
>  Total runtime: 4641.753 ms
> (4 rows)
> 
> 
> 
> What can I do to improve the performance? 

Have you tried 'select count (1)..."?

Reinoud


-- 
______________
"Nothing is as subjective as reality"
Reinoud van Leeuwenreinou...@n.leeuwen.net
http://reinoud.van.leeuwen.net kvk 27320762
__

-- 
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] Question on COUNT performance

2010-07-14 Thread Reinoud van Leeuwen
On Wed, Jul 14, 2010 at 07:30:39AM -0600, Joshua Tolley wrote:

> > Have you tried 'select count (1)..."?
> 
> If this helps at all, it's unlikely to help much. I remember having seen
> discussion somewhere that there's an optimization such that count(*) and
> count(1) do the same thing anyway, but I can't find it in the code
> immediately. 

oops, I was thinking too much about Sybase (where is makes a huge 
difference, since the optimiser sees that everything it needs can be found 
in the index so the table does not have to be read at all).

Reinoud

-- 
______
"Nothing is as subjective as reality"
Reinoud van Leeuwenreinou...@n.leeuwen.net
http://reinoud.van.leeuwen.net kvk 27320762
__

-- 
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] backup and document views and user functions

2010-08-30 Thread Reinoud van Leeuwen
On Mon, Aug 30, 2010 at 09:21:06PM +0300, David Harel wrote:
> Hi,
> 
> I am looking for an easy way to backup views and functions. I want to 
> store them in our version control system.
> 
> Using pgAdmin I can access them one at a time. I am looking for a better 
> reporting mechanism. psql shell command for such report will be just fine.

pg_dump?


-- 
__
"Nothing is as subjective as reality"
Reinoud van Leeuwenreinou...@n.leeuwen.net
http://reinoud.van.leeuwen.net kvk 27320762
__

-- 
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] Issue with postgres connectivity

2011-01-21 Thread Reinoud van Leeuwen
On Fri, Jan 21, 2011 at 10:45:37AM +, Arindam Hore wrote:
> Hello All,
> 
> I am facing a big problem in postgres connectivity using php application. It 
> is taking almost 10 sec for connection establishment. All my applications 
> were working perfectly 2 days before. Yesterday just it started giving 
> problem. Using pg-admin also it is taking time to connect as well as same 
> with opening server status window or with sql query window.
> 
> Postgres is installed in linux environment. Don't know whether is it due to 
> some virus or something else.
> 
> Please provide me with some guidelines. Ask me any queries.

this smells like DNS resolving...

Reinoud

-- 
__
"Nothing is as subjective as reality"
Reinoud van Leeuwenreinou...@n.leeuwen.net
http://reinoud.van.leeuwen.net kvk 27320762
__

-- 
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] Max column number.

2011-07-12 Thread Reinoud van Leeuwen
On Tue, Jul 12, 2011 at 02:48:26PM -0300, Miguel Angel Conte wrote:

> Something like:
> "If I'm not going to exceed the size limit, then  I can add a new column"

You want to add columns in your application? Are you sure you have the
right datamodel?

Reinoud
-- 
__
"Nothing is as subjective as reality"
Reinoud van Leeuwenreinou...@n.leeuwen.net
http://reinoud.van.leeuwen.net kvk 27320762
__

-- 
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] Max column number.

2011-07-12 Thread Reinoud van Leeuwen
On Tue, Jul 12, 2011 at 03:08:36PM -0300, Miguel Angel Conte wrote:
> Unfortunately It's an inherited data model and I can't make any change for
> now...

but by adding columns you *are* making changes to it...

Reinoud
-- 
__
"Nothing is as subjective as reality"
Reinoud van Leeuwenreinou...@n.leeuwen.net
http://reinoud.van.leeuwen.net kvk 27320762
__

-- 
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] Max column number.

2011-07-13 Thread Reinoud van Leeuwen
On Wed, Jul 13, 2011 at 12:45:45PM -0300, Miguel Angel Conte wrote:
> Hi,
> 
> Thanks for your interest. This app load scv files which change every day
> (sometimes the columns too). The sizes of these files are in avg 15MB. So,
> We load something like 100MB each day. We tried to find a better solution
> but we couldn't, becouse one of the our requirement is not to use a lot of
> space. Also, the app is used to consult these information, and for our
> particular type of select's queries, we get the best performance if the
> information is all into a same row.

Still, you could normalize the data in a better way, and present it to
users or an application with views or stored procedures...

Reinoud

-- 
__
"Nothing is as subjective as reality"
Reinoud van Leeuwenreinou...@n.leeuwen.net
http://reinoud.van.leeuwen.net kvk 27320762
__

-- 
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] why these results?

2011-08-01 Thread Reinoud van Leeuwen
On Mon, Aug 01, 2011 at 04:50:00PM -0600, Wes James wrote:
> select count(*) from table;
> 
> count
> ---
>100
> (1 row)
> 
> 
> is correct
> 
> select count(*) from table where col::text ~~* '%text%';
> 
> count
> ---
>  1
> (1 row)
> 
> is correct.
> 
> But now if I do:
> 
> 
> select count(*) from table where col::text !~~* '%text%';
> count
> ---
>   98
> (1 row)
> 
> Shouldn't it be 99?  That is out of 100 records there is one that has
> "text" in column "col" so the !~~* should return 99 rows.  ??

Wild guess:
select count (*) from table where col is null;
count
------
  1
(1 row)

";-)

Reinoud
-- 
__
"Nothing is as subjective as reality"
Reinoud van Leeuwenreinou...@n.leeuwen.net
http://reinoud.van.leeuwen.net kvk 27320762
__

-- 
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 delete multiple records

2006-09-14 Thread Reinoud van Leeuwen
On Thu, Sep 14, 2006 at 03:07:35PM +0900, Javanesevn wrote:
> Dear all,
> 
> I execute this query on below:
> 
>  delete from PRODUCT
>  where exists (
>   select
> product_id, item_id
>   from PRODUCT
>   where
>research_date < '2006-01-01'
> )
> this query deleted all records data in PRODUCT table. The subquery 
> return only some records.
> Tell me about other idea? and What's wrong from this query.

The easiest way is to do it like this:

delete from PRODUCT
where research_date < '2006-01-01'

But if you really want to use a subquery, you want to relate it to the 
current record:

delete from PRODUCT
where product_in in (
 select product_id
 from PRODUCT
 where research_date < '2006-01-01'
)

In your example you just test if some records exist. They allways do, so 
the where clause evaluates to true for every row


-- 
______________
"Nothing is as subjective as reality"
Reinoud van Leeuwen[EMAIL PROTECTED]
http://www.xs4all.nl/~reinoud
__

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

   http://archives.postgresql.org


Re: [SQL] [ADMIN] Deadlock on transaction

2007-02-12 Thread Reinoud van Leeuwen
On Mon, Feb 12, 2007 at 03:08:27PM -0300, Ezequias Rodrigues da Rocha wrote:
> I mean really deadlock. Other transactions can't access the database until
> the main transaction is complete. 

What you describe sounds like a blocking lock: one transaction uses a 
resource and another has to wait until it is not locked anymore

A deadlock is a special case: two transactions both need resources A and 
B. transaction 1 locks A, and the next moment transaction 2 locks B. Now 
transaction 1 waits for 2 to release B, but at the same time 2 waits for 1 
to release A. This is called a deadlock, or circulair lock.
Postgresql automatically detects such a situation and rolls one of them 
back. The client recieves a 'restartable database error' and that is 
exactly true: ususally another try will work. 

One method of fixing deadlocks is to make sure all your code accesses the 
database in the same order. Alphabetically is easiest to remember...

-- 
__
"Nothing is as subjective as reality"
Reinoud van Leeuwen[EMAIL PROTECTED]
http://www.xs4all.nl/~reinoud
__

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


Re: [SQL] workday function

2007-05-15 Thread Reinoud van Leeuwen
On Tue, May 15, 2007 at 09:51:34AM +0100, Gary Stainburn wrote:
> Hi folks
> 
> I need to be able to add and subtract workdays, something like
> 
> select CURRENT_DATE - '3 work days'::interval;

Would that take holidays into account? (and wich ones?)

-- 
__
"Nothing is as subjective as reality"
Reinoud van Leeuwen[EMAIL PROTECTED]
http://www.xs4all.nl/~reinoud
__

---(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] record fields as columns in reports

2007-06-28 Thread Reinoud van Leeuwen
Hi everybody,

I have a script that runs every night and produces a list of a few error 
conditions and the number.

My manager would like it in a form where he can produce some charts in a 
spreadsheet (that's probably why he's a manager ";-).

So the table I store the errormessages in has a format like this:

CREATE TABLE repport_history
(
  rundate date,
  errordescription character varying(255),
  number bigint
) 


And I would like an output with something like:

  06/22 06/2306/2406/25
ERROR1 10 10   9 8
ERROR2250300 220   200
ERROR3  4  2   0 0


(probably in csv or something like that but that is the easy part ";-)

The problems are:
- how to create a dynamic result type? (is that possible in a stored 
procedure?)
- how to make sure that missing records are reported as 0
  (some errors might not have entries on some dates)


Has somebody ever made a generic solution for something like this? (or do 
I need a reporting tool?)


Thanks in advance,
Reinoud

-- 
__
"Nothing is as subjective as reality"
Reinoud van Leeuwen[EMAIL PROTECTED]
http://www.xs4all.nl/~reinoud
__

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


Re: [SQL] Support for SQL TOP clause?

2008-01-10 Thread Reinoud van Leeuwen
On Thu, Jan 10, 2008 at 02:19:43PM +1100, Phillip Smith wrote:
> SELECT *
> 
> FROM   Individual
> 
> LIMIT 3

Note that you will have to add an 'order by' clause to guarantee 
predictable results...

-- 
__
"Nothing is as subjective as reality"
Reinoud van Leeuwen[EMAIL PROTECTED]
http://www.xs4all.nl/~reinoud
__

---(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