From: | "Ramakrishnan Muralidharan" <ramakrishnanm(at)pervasive-postgres(dot)com> |
---|---|
To: | <lucas(at)presserv(dot)org>, <pgsql-sql(at)postgresql(dot)org> |
Subject: | Re: Function or Field? |
Date: | 2005-05-03 11:49:00 |
Message-ID: | 02767D4600E59A4487233B23AEF5C59922C2A2@blrmail1.aus.pervasive.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
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
);
insert into products values ( 1 , 'Test product 1' );
insert into products values ( 2 , 'Test product 2' );
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 = p.id
group by p.id , p.description
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
-----Original Message-----
From: pgsql-sql-owner(at)postgresql(dot)org
[mailto:pgsql-sql-owner(at)postgresql(dot)org]On Behalf Of lucas(at)presserv(dot)org
Sent: Tuesday, May 03, 2005 1:47 AM
To: pgsql-sql(at)postgresql(dot)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
From | Date | Subject | |
---|---|---|---|
Next Message | lucas | 2005-05-03 11:58:24 | Re: Function or Field? |
Previous Message | Harald Fuchs | 2005-05-03 10:59:48 | Re: Trimming the cost of ORDER BY in a simple query |