X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 3A42A535E8 for ; Tue, 3 May 2005 08:49:07 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 15204-02 for ; Tue, 3 May 2005 11:49:04 +0000 (GMT) Received: from ausimss.pervasive.com (ausimss.pervasive.com [66.45.103.246]) by svr1.postgresql.org (Postfix) with ESMTP id F3B79535D9 for ; Tue, 3 May 2005 08:49:03 -0300 (ADT) Received: from ausbayes1.aus.pervasive.com ([172.16.8.6]) by ausimss.pervasive.com with InterScan Messaging Security Suite; Tue, 03 May 2005 06:49:03 -0500 Received: from BLRMAIL1.aus.pervasive.com ([10.3.16.60]) by ausbayes1.aus.pervasive.com with Microsoft SMTPSVC(5.0.2195.6713); Tue, 3 May 2005 06:49:02 -0500 X-MimeOLE: Produced By Microsoft Exchange V6.0.6603.0 content-class: urn:content-classes:message Subject: Re: Function or Field? MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Date: Tue, 3 May 2005 17:19:00 +0530 Message-ID: <02767D4600E59A4487233B23AEF5C59922C2A2@blrmail1.aus.pervasive.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [SQL] Function or Field? Thread-Index: AcVPVQ1NYOokLFrhRGaf6mv8J9V0wgAgS4HA From: "Ramakrishnan Muralidharan" To: , X-OriginalArrivalTime: 03 May 2005 11:49:02.0927 (UTC) FILETIME=[1A177DF0:01C54FD6] X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.087 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200505/61 X-Sequence-Number: 21374 Hi I have tried with the following example CREATE TABLE products( id serial primary key, description varchar(50) ); CREATE TABLE vendding( id serial primary key, date_ date, product integer=20 ); insert into products values ( 1 , 'Test product 1' );=20 insert into products values ( 2 , 'Test product 2' );=20 insert into vendding values( 1 , '2005-05-01' , 1 ); insert into vendding values( 2 , '2005-05-02' , 1 ); insert into vendding values( 3 , '2005-05-03' , 1 ); insert into vendding values( 4 , '2005-05-04' , 1 ); insert into vendding values( 5 , '2005-05-05' , 1 ); insert into vendding values( 6 , '2005-05-06' , 1 ); insert into vendding values( 7 , '2005-05-07' , 1 ); insert into vendding values( 8 , '2005-05-01' , 2 ); insert into vendding values( 9 , '2005-05-02' , 2 ); insert into vendding values( 10 , '2005-05-03' , 2 ); insert into vendding values( 11 , '2005-05-04' , 2 ); insert into vendding values( 12 , '2005-05-05' , 2 ); insert into vendding values( 13 , '2005-05-06' , 2 ); insert into vendding values( 14 , '2005-05-07' , 2 ); SELECT p.id , p.description , max(v.date_) from products p , vendding v = where v.product =3D p.id group by p.id , p.description=20 order by p.id The above select statement bring the last vending record for each = product. I hope you are looking for this kind of output.. Regards, R.Muralidharan =20 -----Original Message----- From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]On Behalf Of lucas@presserv.org Sent: Tuesday, May 03, 2005 1:47 AM To: pgsql-sql@postgresql.org Subject: [SQL] Function or Field? Hi. What is the better way to store the last record for a translation??? I.E: The data for the last product vendding. What is better: a) Create a field in "product" table and create a Trigger (before = insert or update into vendding table) to alter this field. b) Create a view or function that check the all venddings (in vendding = table) for the specified product and return the last vendding information? a) CREATE TABLE products( id serial primary key, description varchar(50), last_vendding date() --Is correct to use this field??? ); CREATE TABLE vendding( id serial primary key, date_ date, product integer references (products) ); CREATE TRIGGER TG_change_products_last_vendding_field on table vendding = BEFORE INSERT OR UPDATE FOR EACH ROW EXECUTE procedure change_products_last_vendding(); b) CREATE TABLE products ( id serial primary key, description varchar(50) ); CREATE TABLE vendding( id serial primary key, date_ date, product integer references (products) ); CREATE VIEW last_product_change as SELECT * from vendding order by = date_ desc limit 1; --Okay, this view will return the last record and not the last = record for a product... but its a example. I am asking it becouse I have used CLIPPER(dbase) for my old programs = and in DBASE the view/check function that will check for each select is not functional. And I need to create a field in all table references, but in = DBASE this fields allways broken and I need to recheck it. Thank you. ---------------------------(end of broadcast)--------------------------- TIP 8: explain analyze is your friend