Re: simple SQL question

From: "Kevin Duffy" <KD(at)wrinvestments(dot)com>
To: "Dan McFadyen" <danm(at)cryptocard(dot)com>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: simple SQL question
Date: 2009-03-20 21:20:21
Message-ID: DFC309C8A42633419600522FA8C4AE1AB6C155@mail-01.wrcapital.corp
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Mr. McFadyen:

Thanks for your quick reply.

The coloumns in the SECURITY Table of interest would be:

securitykey serial NOT NULL,

securitytypekey integer,

securitydesc character varying(125),

marketcap_usd numeric(19,6)

where marketcap_usd would be used to join into capitalizationlevel.

I was hoping to create something, how shall we say, tidy".

So I could write something like

Select SECURITY.* , getcaplevelkey( marketcap_usd ) From
security

Or

Select security.*, caplevelkey, caplevelname

From SECURITY, getcaplevel() as gcl

Where marketcap_usd between ( gcl.caplow and gcl.caphigh )

I would expect that in the first option the function would be called for
every security and would have to loop through a cursor for each call.

In the second option where the function is returning a SETOF, how many
times would it be called?

If I put it in parentheses, how many times would it be called?

Any comments from the PostgresSQL gurus out there?

Happy Friday.

KD

________________________________

From: Dan McFadyen [mailto:danm(at)cryptocard(dot)com]
Sent: Friday, March 20, 2009 3:51 PM
To: Kevin Duffy
Subject: RE: [SQL] simple SQL question

Hi again,

You got me curious so I went into my SQL browser and got cracking:

SELECT companyName, capName FROM

(

SELECT companyName, MIN(capitalizationlevel.caplevelmillions) FROM
companies, capitalizationlevel WHERE companymillions <
capitalizationlevel.caplevelmillions GROUP BY companyName

) as something JOIN capitalizationlevel ON something.min=
capitalizationlevel.caplevelname

I don't know what your company table looks like, so that's the closest I
can get.

Enjoy

From: pgsql-sql-owner(at)postgresql(dot)org
[mailto:pgsql-sql-owner(at)postgresql(dot)org] On Behalf Of Kevin Duffy
Sent: March 20, 2009 3:06 PM
To: pgsql-sql(at)postgresql(dot)org
Subject: [SQL] simple SQL question

Hello All:

I would like your input on how I should approach a problem.

Say I have a table of companies and one attribute is the market
capitalization of these companies.

I have another table (definition below) and it contains capitalization
levels.

For example Micro Cap, Mid Cap, and Large Cap. However, the table

CAPITALIZATIONLEVEL, only contains the upper cutoff of the levels.

The question is: What is the most efficient way to assign/join the
capitalization levels to the companies?

I could create a function that given a market cap in millions would
return the matching cap level,

by using a cursor to step through CAPITALIZATIONLEVEL from lowest to
highest.

This function would be declared STABLE.

-or maybe-

a function that RETURNS SETOF and the rows in the set returned would
contain both the lower and

upper limits of the cap level. The lower limit would be calc'ed by
using a cursor to step through

CAPITALIZATIONLEVEL from lowest to highest. This function would be
declared STABLE.

Which method would execute more efficiently?

Thanks for considering my issue.

Kevin Duffy

CREATE TABLE capitalizationlevel

(

capitallevelkey serial NOT NULL,

caplevelname character(10) NOT NULL,

caplevelmillions integer NOT NULL, <-- this is the upper limit

CONSTRAINT pk_capitalizationlevel PRIMARY KEY (capitallevelkey)

)

WITH (OIDS=FALSE);

ALTER TABLE capitalizationlevel OWNER TO postgres;

The information transmitted is intended only for the person or entity to
which it is addressed and may contain confidential and/or privileged
material. Statements and opinions expressed in this e-mail may not
represent those of the company. Any review, retransmission,
dissemination or other use of, or taking of any action in reliance upon,
this information by persons or entities other than the intended
recipient is prohibited. If you received this in error, please contact
the sender immediately and delete the material from any computer.
Please see our legal details at http://www.cryptocard.com
<http://www.cryptocard.com>
CRYPTOCard Inc. is registered in the province of Ontario, Canada with
Business number 80531 6478. CRYPTOCard Europe is limited liability
company registered in England and Wales (with registered number 05728808
and VAT number 869 3979 41); its registered office is Eden Park, Ham
Green, Bristol, BS20 0EB

Browse pgsql-sql by date

  From Date Subject
Next Message DM 2009-03-20 22:40:39 pg_restore error - Any Idea?
Previous Message Staten Oliver 2009-03-20 19:54:18 Postgresql Rules