[SQL] Joins involving functions

2006-04-10 Thread Jeff Boes
I have a function that returns a row from a table, e.g.: create table foo (a integer); create function f_foo (integer) returns foo language 'sql' as $foo$ select * from foo where a = $1 limit 1 $foo$ create table bar (b integer); select * from bar cross join f_foo(bar.b) as foo; Unfortunatel

Re: [SQL] psql client: technique for applying default values to :variables?

2006-01-16 Thread Jeff Boes
Michael Glaesemann wrote: > > On Jan 14, 2006, at 23:54 , Jeff Boes wrote: > >> Tony Wasson wrote: > >> >> Sure, but that was not my question. I want to be able to set the >> variable on the command line, BUT have it default to a value inside >> the SQL

Re: [SQL] psql client: technique for applying default values to :variables?

2006-01-14 Thread Jeff Boes
Tony Wasson wrote: On 1/11/06, Jeff Boes <[EMAIL PROTECTED]> wrote: Stumped: is there any way to set up default values for psql variables within the .SQL file itself? Obviously, I can do something like: $ psql -f my_script -v MYVAR=${myvar:-mydefault} but I would prefer t

[SQL] psql client: technique for applying default values to :variables?

2006-01-11 Thread Jeff Boes
Stumped: is there any way to set up default values for psql variables within the .SQL file itself? Obviously, I can do something like: $ psql -f my_script -v MYVAR=${myvar:-mydefault} but I would prefer to have the value stored with the .SQL file, e.g. (if this actually worked): \set MYVAR COALE

Re: [SQL] How to alias table columns in result?

2005-08-11 Thread Jeff Boes
nori wrote: > Ok I got it. There is no way to alias all columns of some table with > some "prefix" that will be visible in result except to alias each > column. Only other way would be to write a view for each table, then write all your queries against the views. ---(end o

Re: [SQL] Breakdown results by month

2005-08-04 Thread Jeff Boes
Henry Ortega wrote: > I have the ff table: > > id |total| effective|end_date > john 6 01-01-200502-28-2005 > john 8 03-01-200506-30-2005 > > How can I return: > id |total| effective

[SQL] CREATE TABLE AS SELECT

2005-07-29 Thread Jeff Boes
lly a "username" constructor, making "jboes" out of "Jeff Boes"). The odd thing is that function f() also looks into the table "foo" to see if the value it's constructing is truly unique; if it is not, it tacks on a "1", "2", etc. until i

[SQL] Foreign key with check?

2005-07-27 Thread Jeff Boes
Given a table like this: create table primary ( a integer primary key, b boolean ); And another like this: create table secondary ( a integer, some_other_fields ); I would like a foreign key constraint on the "secondary" table that looks something like: foreign key (a, true) reference

Re: [SQL] DROP TYPE without error?

2005-04-07 Thread Jeff Boes
being generated automatically, based on data found in a database. If I understand you correctly, then this might be useful: begin; select now(); \o tmp.tmp \qecho 'drop type \"foofookitty\";' \o \! psql -f tmp.tmp select now(); commit; -- Jeff Boes

Re: [SQL] Calendar Function

2005-02-03 Thread Jeff Boes
?column? - 2004-02-29 00:00:00 (1 row) Thus, given the original response to your question: select * from calendar('1 feb 2004', ( '1 feb 2004'::date + '1 month'::interval - '1 day'::interval )::date); -- Jeff Boes

[SQL] How to recognize trigger-inserted rows?

2004-10-27 Thread Jeff Boes
I have a table with an INSERT/UPDATE/DELETE statement trigger. The trigger's action is to insert "jobs" into a queue noting that the table has changed. A number of other tables have FK relationships with this table, and they have their own statement triggers that fire on DELETE. When I delete

[SQL] How to recognize trigger-inserted rows?

2004-10-26 Thread Jeff Boes
I have a table with an INSERT/UPDATE/DELETE statement trigger. The trigger's action is to insert "jobs" into a queue noting that the table has changed. A number of other tables have FK relationships with this table, and they have their own statement triggers that fire on DELETE. When I delete a n

[SQL] psql variable interpolation from command line

2004-10-01 Thread Jeff Boes
why won't variable interpolation work when the "-c" flag is used? -- Jeff Boes vox 269.226.9550 ext 24 Database Engineer fax 269.349.9076 Nexcerpt, Inc. http://www.nexcerpt.com ..

Re: [SQL] psql variable interpolation from command line

2004-10-01 Thread Jeff Boes
Ugh, never mind. I finally saw the reason in the 'psql' documentation. Missed it the first time. (And the second, and third, ...) -- Jeff Boes vox 269.226.9550 ext 24 Database Engineer fax 269.349.9076 Nex

Re: [SQL] Complicated "group by" question

2004-09-02 Thread Jeff Boes
Andrew Perrin wrote: I have a table of people ("reviewers"), a table of review assignments ("assign"), and a table of review acceptances ("accept"). I would like to be able to write a query to return the latest (e.g., max(assign_date)) assignment for each reviewer, plus the acc_id field from "accep

[SQL] Trigger and function not on speaking terms

2004-08-02 Thread Jeff Boes
Hmm, this is puzzling me: create or replace function fn_foo(text) returns trigger as ' begin # Do some stuff with $1 end; ' language 'plpgsql'; CREATE FUNCTION create table bar (aaa text); CREATE TABLE create trigger trg_bar after insert or update on bar execute procedure fn_foo('string'); ERROR:

[SQL] Inherited tables and new fields

2004-07-20 Thread Jeff Boes
This feels like a flaw in the way inherited tables work. I have a "template" table used to create other tables (but not by inheritance; instead the "daughter" tables are created via create table draft_00123 as select * from draft_template where false; This is done for somewhat historical reasons,

Re: [SQL] FW: "=" operator vs. "IS"

2004-07-01 Thread Jeff Boes
I'm just curious - why is it not possible to use the "=" operator to compare values with NULL? I suspect that the SQL standard specified it that way, but I can't see any ambiguity in an expression like "AND foo.bar = NULL". Is it because NULL does not "equal" any value, and the expression should b

[SQL] Formatting problems with negative intervals, TO_CHAR

2004-06-06 Thread Jeff Boes
This seems ... well, counter-intuitive at least: (using Pg 7.4.1) # select to_char('4 minutes'::interval - '5 minutes 30 seconds'::interval, 'mi:ss'); to_char - -1:-3 (1 row) Why is the trailing zero lost? Why are there two minus signs? I would expect '-1:30'. Likewise, # select to_char('

Re: [SQL] not really SQL but I need info on BLOBs

2004-05-07 Thread Jeff Boes
In the same vein ... We are at present using BLOBs to store images (but not very many) and generated output (mostly HTML and XML). The data being stored doesn't require BLOB use because of size; it's mostly the binary nature of the data. Lack of satisfaction with the BLOB support (generally diffi

Re: [SQL] SQL challenge--top 10 for each key value?

2004-04-19 Thread Jeff Boes
Troels Arvin wrote: See http://www.acm.org/sigmod/record/issues/0403/index.html#standards for an article which summarizes the news in SQL:2003. This is a very useful page; thank you for creating it and for noting it in this thread! -- (Posted from an account used as a SPAM dump. If you really wan

[SQL] SQL challenge--top 10 for each key value?

2004-04-08 Thread Jeff Boes
e" TRUE, and enough non-"include" rows to make N. I might end up with more than N rows for a given value of "query" if there were more than N with "include" set. I headed off in the direction of groups of SELECTs and UNIONs, and quit when I got to som

Re: [SQL] Dramatic slowdown of sql when placed in a function

2004-03-09 Thread Jeff Boes
l_a=; vs. EXPLAIN select * from foobar_table where col_a=::; -- ~~~~| Genius may have its limitations, but stupidity is not Jeff Boes | thus handicapped. [EMAIL PROTECTED] |--Elbert Hubbard (1856-1915), American author ---(end of broadca

Re: [SQL] Cool ORDER BY feature

2004-02-06 Thread Jeff Boes
s: create table priorities as select 'LO' as priority, 1 as sort_order union select 'N', 2 union select 'HI', 3; select * from testtable join priorities using (priority) order by sort_order; This last approach is what I've used with code-tables in our system

[SQL] Rule won't let me NOTIFY, no matter how hard I try

2004-01-13 Thread Jeff Boes
is error: WARNING: plpgsql: ERROR during compile of fn_notify near line 5 ERROR: syntax error at or near "" What gives? Must I use a trigger to get around this? -- Jeff Boes vox 269.226.9550 ext 24 Database Engineer

[SQL] Need query to separate rows by one field's value

2003-11-18 Thread Jeff Boes
t;a" such that the sum of "b" values is as close to one-half of the total sum of "b" over all rows. That is, select 2.0*sum(b) from foo where a in (...); would be approximately the same as select sum(b) from foo; Likewise, select 2.0*sum(b) from foo where a NOT in

Re: [SQL] Data type conversion

2003-10-03 Thread Jeff Boes
Jan Pips wrote: How to convert the interval type into integer, float or any "countable" data type at the time of table creation using select ... into? I'm guessing you want something like this: SELECT EXTRACT('EPOCH' FROM INTERVAL '1 days 4 hours 15 minu

Re: [SQL] EXCEPT Queries

2003-02-20 Thread Jeff Boes
UBSCRIBER_NAME" , "B"."ACCOUNT_NUMBER" FROM "B" > > How about a SELECT DISTINCT ON? SELECT DISTINCT ON (account_number) subscriber_name, account_number FROM (SELECT 1 AS sort_order, subscriber_name, account_number FROM &quo

[SQL] Timezone conversion

2003-02-14 Thread Jeff Boes
Is there a straightforward way to change the display of a time to another timezone? For example, my server's environment is EST (-0500); I want to convert timestamps for display in PST (-0800). What I've been doing is to use a Pl/Pgsql function to subtract the current timezone-offset and add in t

Re: [SQL] escape single quote in INSERT command

2002-11-22 Thread Jeff Boes
) VALUES (?)|, undef, q|O'Malley|) or die $DBI::errstr; $dbh->commit; By using the '?' placeholder format for statements, you can pass in any data without having to worry about quoting. -- Jeff Boes vox 616.226.9550 ext 24 Database Engineer

Re: [SQL] calculating interval

2002-11-22 Thread Jeff Boes
way. > The easiest way would be to write a function in a language that supports some kind of date library. I'm most familiar with Perl and its Date::Manip library, so that's what I would turn to. -- Jeff Boes vox 616.226.9550 ext 24 Database Engi

[SQL] Quartile (etc) ranking in a SQL statement?

2002-11-07 Thread Jeff Boes
(142 rows) How can I write a query that will return these as 1 | aaa | 1251 2 | aba | 1197 3 | cax | 1042 ... | ... | ... 142 | axq | 23 -- Jeff Boes vox 616.226.9550 ext 24 Database Engineer fax 616.349.9076 Nexcerpt

[SQL] Query to evaluate space used

2002-09-18 Thread Jeff Boes
elect a.relname, b.relname, 0, b.relpages from pg_class a join pg_class b on (b.relname like 'pg_toast_' || a.relfilenode || '%') where a.relkind = 'r' ) as pg_class group by relname order by sum(relpages) desc offset 20) as "Others") as rows o

[SQL] Large number of lookups

2002-08-30 Thread Jeff Boes
ect second_val union ...) as TEMP_TABLE using (SECONDARY_KEY_FLD) likewise, either taking some number of my desired values in "chunks" or all at once. What's likely to work better, and why? (I'm going off to write a benchmark script, but I'd like to hear some theoretic

Re: [SQL] "reverse()" on strings

2002-08-26 Thread Jeff Boes
h (iscachable); The same approach can be used to provide a regex match with Perl syntax, but I don't have an example of that coded up and at hand. -- Jeff Boes vox 269.226.9550 ext 24 Database Engineer f

[SQL] to_char() and order-by

2002-06-27 Thread Jeff Boes
Perhaps this was covered in the 7.2.x release notes, but it's mystifying us: select to_char(time_link,'999.99') from stat_fetch where time_link is not null order by 1 desc limit 10; (time_link is a double-precision column.) Under 7.1.3, this returns results like: to_char - 9.99

Re: [SQL] ORDER BY case insensitive?

2001-10-03 Thread Jeff Boes
In article <PXEu7.602317$[EMAIL PROTECTED]>, "Bob Swerdlow" <[EMAIL PROTECTED]> wrote: > How do I get the rows sorted in a case insensitive way? > SELECT * FROM MyTable ORDER BY Name; Try SELECT * FROM MyTable ORDER BY upper(Name); (or &#

[SQL] Re: rpt

2001-06-12 Thread Jeff Boes
it is natively built in or some 3rd party > tool that I can use. Depends on what platform you want to run the report generator on. If you're happy with a Windows platform, you could connect Access or Crystal Reports, or a large number of other query