[SQL] replace EXCEPT by JOIN

2007-04-12 Thread T E Schmitz
The following construct does what I need but is there a better way of expressing this? select * from ( SELECT DISTINCT section.section_pk FROM ITEM LEFT JOIN product product ON Item.product_fk = product.product_pk LEFT JOIN section section ON product.section_fk = section.section_pk WHERE i

Re: [SQL] triple self-join crawling

2007-03-19 Thread T E Schmitz
Andrew Sullivan wrote: Define "crawling". Also, please post EXPLAIN and, if feasible, Total runtime: 191430.537 ms EXPLAIN ANALYSE output for your case. On Sun, Mar 18, 2007 at 07:51:28PM +, T E Schmitz wrote: The following self join of a table containing 5800 records i

Re: [SQL] triple self-join crawling

2007-03-19 Thread T E Schmitz
T E Schmitz wrote: T E Schmitz wrote: Things improved hugely when I changed the JOIN clauses: see explain analyze below - can this be improved further? LEFT OUTER JOIN history AS past_month ON (past_month.stock = history.stockAND past_month.day >= (history.day - 30)

Re: [SQL] triple self-join crawling

2007-03-19 Thread T E Schmitz
T E Schmitz wrote: The following self join of a table containing 5800 records is crawling: SELECT history.stock, history.day, history.high, history.low, MAX(past_week.high) AS week_high, MAX(past_month.high) AS month_high FROM history INNER JOIN history AS past_month ON

[SQL] triple self-join crawling

2007-03-18 Thread T E Schmitz
The following self join of a table containing 5800 records is crawling: CREATE TABLE history ( stock VARCHAR(30) NOT NULL, day date NOT NULL, open NUMERIC (6,1) NOT NULL, high NUMERIC (6,1) NOT NULL, low NUMERIC (6,1) NOT NULL, close NUMERIC (6,1) NOT NULL, volume NUME

Re: [SQL] join table with itself?

2007-03-15 Thread T E Schmitz
William Leite Araújo wrote: On 15/03/07, *T E Schmitz* <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote: (...) Try join the tables. SELECT present.day, present.low, (MIN(future.day)-present.day) as days2fall FROM history AS present JOIN history

[SQL] join table with itself?

2007-03-15 Thread T E Schmitz
I am not getting to grips with the following query: set-up: Postgresql 8.1 The table HISTORY contains stockmarket data: DAY HIGHLOW 2007/02/28 6286.1 6166.2 2007/02/27 6434.7 6270.5 2007/02/26 6446.8 6401.5 I'd like to produce the following resul

Re: [SQL] import CSV file

2007-03-14 Thread T E Schmitz
T E Schmitz wrote: Rodrigo De León wrote: On 3/13/07, T E Schmitz <[EMAIL PROTECTED]> wrote: This is pretty ingenious! Just a few minor problems: - how does COPY know which column is which? - how do I specify DELIMITER as TAB? See: http://www.postgresql.org/docs/8.2/static/sql-cop

Re: [SQL] import CSV file

2007-03-14 Thread T E Schmitz
Rodrigo De León wrote: On 3/13/07, T E Schmitz <[EMAIL PROTECTED]> wrote: This is pretty ingenious! Just a few minor problems: - how does COPY know which column is which? - how do I specify DELIMITER as TAB? See: http://www.postgresql.org/docs/8.2/static/sql-copy.html Thank you f

Re: [SQL] import CSV file

2007-03-13 Thread T E Schmitz
D'Arcy J.M. Cain wrote: On Tue, 13 Mar 2007 20:38:33 +0000 T E Schmitz <[EMAIL PROTECTED]> wrote: psql -h lolek -U tes -d stockmarket -c "copy history from STDIN CSV" < /tmp/FTSE.csv Also, it's nopt happy about the date format : 2007/02/09 -- Regard

Re: [SQL] import CSV file

2007-03-13 Thread T E Schmitz
Jure Kodzoman wrote: On Tue, 2007-03-13 at 20:38 +, T E Schmitz wrote: I am trying to batch-load a tab-separated CSV file: psql -h lolek -U tes -d stockmarket -c "copy history from '/tmp/FTSE.csv' CSV"; ERROR: could not open file "/tmp/FTSE.csv"

Re: [SQL] import CSV file

2007-03-13 Thread T E Schmitz
D'Arcy J.M. Cain wrote: On Tue, 13 Mar 2007 20:38:33 +0000 T E Schmitz <[EMAIL PROTECTED]> wrote: I am trying to batch-load a tab-separated CSV file: psql -h lolek -U tes -d stockmarket -c "copy history from '/tmp/FTSE.csv' CSV"; ERROR: could not open file

[SQL] import CSV file

2007-03-13 Thread T E Schmitz
I am trying to batch-load a tab-separated CSV file: psql -h lolek -U tes -d stockmarket -c "copy history from '/tmp/FTSE.csv' CSV"; ERROR: could not open file "/tmp/FTSE.csv" for reading: No such file or directory The file exists. Do I need to escape the quotes? set-up: Postgres 8.1, De

Re: [SQL] pg_dump fails (timestamp out of range)

2007-02-26 Thread T E Schmitz
Andrew Sullivan wrote: On Mon, Feb 26, 2007 at 11:05:08AM +, T E Schmitz wrote: I can't thank you enough for the above advice. The test above identified 3 records and once they were removed I was able to dump the DB. You'll be wanting to make sure your hardware is fixed

Re: [SQL] pg_dump fails (timestamp out of range)

2007-02-26 Thread T E Schmitz
Tom Lane wrote: T E Schmitz <[EMAIL PROTECTED]> writes: pg_dump: ERROR: timestamp out of range pg_dump: SQL command to dump the contents of table "server_hit_bin" failed: PQendcopy() failed. You should treat this as a corrupt-data exercise: you need to identify and fix

[SQL] pg_dump fails (timestamp out of range)

2007-02-22 Thread T E Schmitz
Apologies for cross-posting (already sent to pgadmin-support) but I am totally stuck with this: I run an ecommerce system on a webserver, which I want to move to a different machine. However, I am stalled because pg_dump fail

Re: [SQL] psql -F

2006-11-16 Thread T E Schmitz
Joe Conway wrote: T E Schmitz wrote: Alvaro Herrera wrote: T E Schmitz wrote: I have written a shell script to export data: psql -A -t -U $DBUSER -d $DB -c "$QUERY" -F ' ' How can I specify a TAB character with the -F option? Try: psql -A -t -U $DBUSER -d $DB

Re: [SQL] psql -F

2006-11-16 Thread T E Schmitz
Alvaro Herrera wrote: T E Schmitz wrote: I have written a shell script to export data: psql -A -t -U $DBUSER -d $DB -c "$QUERY" -F ' ' Currently, I am using spaces as field separator but what I really want is tabs. How can I specify a TAB character with the -F option?

[SQL] psql -F

2006-11-16 Thread T E Schmitz
I have written a shell script to export data: psql -A -t -U $DBUSER -d $DB -c "$QUERY" -F ' ' Currently, I am using spaces as field separator but what I really want is tabs. How can I specify a TAB character with the -F option? -- Regards, Tarlika Elisabeth Schmitz --

Re: [SQL] max (timestamp,timestamp)

2006-11-14 Thread T E Schmitz
Michael Fuhr wrote: On Mon, Nov 13, 2006 at 07:29:09PM +0500, imad wrote: max (timestamptz, timestamptz) does not exist already. You need to create a simple function in PLpgSQL something like if a > b return a; else return b; Since PostgreSQL 8.1 you can use GREATEST: test=> SELECT greates

[SQL] max (timestamp,timestamp)

2006-11-13 Thread T E Schmitz
I tried the following query but the query fails as "function max (timestamp w. timezone,timestamp w. timezone) does not exist" SELECT id, MAX(last_updated, (SELECT MAX (last_updated) FROM product_category_member WHERE product_category_member.id = product_category.id)) FROM product_category

Re: [SQL] SELECT substring with regex

2006-07-10 Thread T E Schmitz
Emils wrote: 2006/7/7, T E Schmitz <[EMAIL PROTECTED]>: valid entries would be: "28mm F2.8" (prime lens) "30-70mm F4" (zoom lens) "30-70mm F2.8" (zoom lens) "30-100mm F4.5-5.6" (zoom lens with variable speed) If these are the cases, wouldn

Re: [SQL] SELECT substring with regex

2006-07-08 Thread T E Schmitz
Aaron Bono wrote: On 7/7/06, *T E Schmitz* <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote: I am trying to come up with a semi-automatic solution to tidy up some data. If it's got to be done manually via the GUI it would mean a lot of dummy work [for the cus

Re: [SQL] SELECT substring with regex

2006-07-08 Thread T E Schmitz
Rodrigo De Leon wrote: On 7/7/06, T E Schmitz <[EMAIL PROTECTED]> wrote: Sorry, but that would also capture something like 10-30-59mm The pattern describes either a single length (120 millimeters) or a range (30 to 70 millimetres), hence: \\d+(-\\d+)?mm The ? quantifier refers

Re: [SQL] SELECT substring with regex

2006-07-07 Thread T E Schmitz
Erik Jones wrote: T E Schmitz wrote: Gary Stainburn wrote: On Friday 07 July 2006 14:51, T E Schmitz wrote: I would like to split the contents of a column using substring with a regular expression: The column contains something like "150mm LD AD Asp XR Macro" I want to split

Re: [SQL] SELECT substring with regex

2006-07-07 Thread T E Schmitz
Aaron Bono wrote: On 7/7/06, *Rodrigo De Leon* <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote: On 7/7/06, T E Schmitz <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote: > But that takes me to the next problem: > > For the sak

Re: [SQL] SELECT substring with regex

2006-07-07 Thread T E Schmitz
Rodrigo De Leon wrote: On 7/7/06, T E Schmitz <[EMAIL PROTECTED]> wrote: But that takes me to the next problem: For the sake of the example I simplified the regular pattern. In reality, BASE_NAME might be: 28mm 28-70mm So the reg. expr. requires brackets: substring (NAME, '^(\\d

Re: [SQL] SELECT substring with regex

2006-07-07 Thread T E Schmitz
Gary Stainburn wrote: On Friday 07 July 2006 14:51, T E Schmitz wrote: I would like to split the contents of a column using substring with a regular expression: The column contains something like "150mm LD AD Asp XR Macro" I want to split this into "150mm", "LD AD

[SQL] SELECT substring with regex

2006-07-07 Thread T E Schmitz
I would like to split the contents of a column using substring with a regular expression: SELECT substring (NAME, '^\\d+mm') as BASE_NAME, substring (NAME, ??? ) as SUFFIX FROM MODEL The column contains something like "150mm LD AD Asp XR Macro" I want to split this into "150mm", "LD AD Asp XR

Re: [SQL] indexing for left join

2006-01-19 Thread T E Schmitz
Rod Taylor wrote: Sequential despite the indices? Or is this because the tables of my test DB are virtually empty? This is it. PostgreSQL changes strategies with data load. Performance testing must be done on an approximation of the real data (both values and size). Thanks for your responses

Re: [SQL] indexing for left join

2006-01-19 Thread T E Schmitz
Milorad Poluga wrote: Try to execute this modification of your query : SELECT ITEM.ITEM_PK FROM ITEM LEFT JOIN SERIAL_NO ON ( SERIAL_NO.ITEM_FK = ITEM.ITEM_PK AND SERIAL_NO.NO ='WX1234' ) GROUP BY ITEM.ITEM_PK SELECT ITEM.ITEM_PK FROM ITEM LEFT JOIN SERIAL_NO O

[SQL] indexing for left join

2006-01-19 Thread T E Schmitz
I have two tables: TABLE ITEM ( ITEM_PK serial, RETAIL_PRICE numeric (7,2) NOT NULL, ... PRIMARY KEY (ITEM_PK) ) TABLE SERIAL_NO ( SERIAL_NO_PK serial, NO varchar (20) NOT NULL, NAME varchar (20), ITEM_FK integer NOT NULL, PRIMARY KEY (SERIAL_NO_PK) ); common query: SELECT ITEM.ITEM_PK FROM IT

[SQL] TIMESTAMP / summertime

2005-04-06 Thread T E Schmitz
Hello, I *detest* British summertime. This year it took me two days to adjust. Now I am realizing that my program might need some adjusting too: Joking aside, I need some advice regarding TIMESTAMP colums and I can't quite get my head round this at the moment: I created a table TRANSAKTION with a

Re: [SQL] cost of CREATE VIEW ... AS SELECT DISTINCT

2005-03-29 Thread T E Schmitz
Bruno Wolff III wrote: On Tue, Mar 29, 2005 at 14:21:15 +0100, T E Schmitz <[EMAIL PROTECTED]> wrote: If that's the case, I should come up with a different concept to obtain a list of ORIGINs. That may be a good idea, especially if there are lots of rows for each origin value. Th

Re: [SQL] cost of CREATE VIEW ... AS SELECT DISTINCT

2005-03-29 Thread T E Schmitz
Alvaro Herrera wrote: On Tue, Mar 29, 2005 at 02:21:15PM +0100, T E Schmitz wrote: Bruno Wolff III wrote: On Tue, Mar 29, 2005 at 11:07:20 +0100, T E Schmitz <[EMAIL PROTECTED]> wrote: Would the "SELECT DISTINCT origin" always cause a sequential table scan regardless whether ther

Re: [SQL] cost of CREATE VIEW ... AS SELECT DISTINCT

2005-03-29 Thread T E Schmitz
Bruno Wolff III wrote: On Tue, Mar 29, 2005 at 11:07:20 +0100, T E Schmitz <[EMAIL PROTECTED]> wrote: Would the "SELECT DISTINCT origin" always cause a sequential table scan regardless whether there is an index on the origin column or not? It's worse than that, SELECT

Re: [SQL] cost of CREATE VIEW ... AS SELECT DISTINCT

2005-03-29 Thread T E Schmitz
Hello Scott, Scott Marlowe wrote: On Mon, 2005-03-28 at 15:43, T E Schmitz wrote: How expensive would it be to maintain the following VIEW: CREATE VIEW origin AS SELECT DISTINCT origin FROM transaktion if there is in index on transaktion.origin; the table transaktion has thousands of records and

[SQL] cost of CREATE VIEW ... AS SELECT DISTINCT

2005-03-28 Thread T E Schmitz
Hello, How expensive would it be to maintain the following VIEW: CREATE VIEW origin AS SELECT DISTINCT origin FROM transaktion if there is in index on transaktion.origin; the table transaktion has thousands of records and there are only a few distinct origin? -- Regards/Gruß, Tarlika Elisabeth Sc

Re: [SQL] psql encoding problem

2005-03-02 Thread T E Schmitz
Hello Ragnar, Ragnar Hafstað wrote: On Mon, 2005-02-28 at 20:48 +, T E Schmitz wrote: INSERT INTO item (name,retail_price) VALUES ('Cheese Soufflé',7.95,); psql comes back with "invalid byte sequence for encoding "UNICODE": 0xe9" might be the client encoding

[SQL] psql encoding problem

2005-03-01 Thread T E Schmitz
Hello, I am trying to insert the following record: INSERT INTO item (name,retail_price) VALUES ('Cheese Soufflé',7.95,); (I presume you see the accented character in *Soufflé*) psql comes back with "invalid byte sequence for encoding "UNICODE": 0xe9" If I do this via DbVisualizer, the record is in

Re: [SQL] Software for database-visualisation

2005-02-24 Thread T E Schmitz
Kai Hessing wrote: Another question: Which software are you using to visualize your database-structur. We're doing it with Quark, but are not very happy with this. DbVisualizer (free version) http://www.minq.se/products/dbvis/index.html -- Regards/Gruß, Tarlika Elisabeth Schmitz ---

Re: [SQL] aggregate / group by question

2005-02-24 Thread T E Schmitz
Hello Keith, Thank you for your help. Keith Worthington wrote: T E Schmitz wrote: Tables: TRANSAKTION --- KIND ('R' or 'S' for refund or sale) TRANSAKTION_PK PAYMENT_METHOD (cheque, cash, CC) ITEM TRANSAKTION_FK ITEM_PK RETAIL_PRICE DISCOUNT Desired result

[SQL] aggregate / group by question

2005-02-22 Thread T E Schmitz
Hello, I must apologize for not coming up with a more descriptive subject line. I am struggling with the following query and I am not even sure whether what I want to achieve is possible at all: The problem in real-world terms: The DB stores TRANSAKTIONS - which are either sales or refunds: each

[SQL] INDEX and LIKE

2004-11-29 Thread T E Schmitz
Hello, I need to be able to perform wildcard searches on a VARCHAR(100) column of the form SELECT * FROM item WHERE serial_no LIKE '%12345678%' Would an index on serial_no do anything at all for this search? -- Regards/Gruß, Tarlika Elisabeth Schmitz ---(end of broadcast)-

Re: [SQL] tricky GROUP BY / JOIN question

2004-11-09 Thread T E Schmitz
Hello Tom, Tom Lane wrote: T E Schmitz <[EMAIL PROTECTED]> writes: This is *almost* what I need: SELECT BRAND.BRAND_NAME, MODEL.MODEL_NAME, min (ITEM.PRICE),max (ITEM.PRICE) *min (CONDITION.POSITION),max (CONDITION.POSITION)* FROM ITEM left outer join MODEL on MODEL_PK =ITEM.MODEL_FK left

[SQL] tricky GROUP BY / JOIN question

2004-11-07 Thread T E Schmitz
Hello, I'm sorry I couldn't think up a more precise subject line. I would like to know whether the following can be implemented in SQL: The example below joins 4 tables ITEM, BRAND, MODEL and CONDITION. In human understandable terms: a [secondhand] Item is of a particular Model and Brand. The Ite

Re: [SQL] Join Table

2004-11-01 Thread T E Schmitz
Hello Mike/Tom/Richard, Thank you for your replies. Michael Fuhr wrote: On Mon, Nov 01, 2004 at 04:34:32PM +, T E Schmitz wrote: Question: is it necessary/advisable to create an index for the ITEM_FK column? Or is this redundantbecause this column is already one of the PK columns? However

[SQL] Join Table

2004-11-01 Thread T E Schmitz
Hello, I have created the following join table: the two FKs are the PK of the table. Typically, I will need to select rows for a given ITEM_FK. Question: is it necessary/advisable to create an index for the ITEM_FK column? Or is this redundantbecause this column is already one of the PK columns

Re: [SQL] CHECK col A not NULL if col B='x'

2004-09-28 Thread T E Schmitz
Hello again, Martin Marques wrote: El Mar 28 Sep 2004 11:02, T E Schmitz escribió: Is it possible to set up a table CHECK, which ensures that column A is NOT NULL if column B = 'x' ? CONSTRAINT somename CHECK (B <> 'x' OR A IS NOT NULL) I noticed a table constraint ca

Re: [SQL] CHECK col A not NULL if col B='x'

2004-09-28 Thread T E Schmitz
Hola Martin! Martin Marques wrote: El Mar 28 Sep 2004 11:02, T E Schmitz escribió: Is it possible to set up a table CHECK, which ensures that column A is NOT NULL if column B = 'x' ? CONSTRAINT somename CHECK (B <> 'x' OR A IS NOT NULL) This is brilliant. Only detected th

[SQL] CHECK col A not NULL if col B='x'

2004-09-28 Thread T E Schmitz
Hello, Is it possible to set up a table CHECK, which ensures that column A is NOT NULL if column B = 'x' ? -- Regards/Gruß, Tarlika Elisabeth Schmitz ---(end of broadcast)--- TIP 2: you can get off all lists at once with the unregister command (s

Re: [SQL] COUNT(*) to find records which have a certain number of

2004-09-20 Thread T E Schmitz
Hello Greg, You have given me plenty of food for thought. Thank you for taking the time. Currently, the tables have such few records (350, 900, 1000) that performance does not come into it, particularly seeing as this was only needed for a one-shot report. However, I have stached your examples a

Re: [SQL] COUNT(*) to find records which have a certain number of

2004-09-20 Thread T E Schmitz
Hi Rod, Rod Taylor wrote: On Mon, 2004-09-20 at 12:19, T E Schmitz wrote: I figured it eventually. (The only thing I don't know is where to put the ORDER BY.) Try this: SELECT brand_name, model_name FROM (SELECT ... INTERSECT SELECT ...) AS t ORDER BY ...

Re: [SQL] COUNT(*) to find records which have a certain number of

2004-09-20 Thread T E Schmitz
I figured it eventually. (The only thing I don't know is where to put the ORDER BY.) I want to select only those BRAND/MODEL combinations, where the MODEL has more than one TYPE, but only where one of those has TYPE_NAME='xyz'. I am not interested in MODELs with multiple TYPEs where none of them a

[SQL] COUNT(*) to find records which have a certain number of dependencies ?

2004-09-20 Thread T E Schmitz
Hello, I apologize in advance for this garbled message but I've been banging my head against a brick-wall for a while and I just can't figure how to do the following: I have 3 tables BRAND,MODEL,TYPE which are related to each other: BRAND = BRAND_PK BRAND_NAME MODEL = MODEL_PK MODEL_NAME

Re: [SQL] ORDER BY and NULLs

2004-09-20 Thread T E Schmitz
Hello, Greg Stark wrote: Tom Lane <[EMAIL PROTECTED]> writes: The paragraph continues: "If the SELECT command included the clause WHERE phone NOT NULL, PostgreSQL could use the index to satisfy the ORDER BY clause. An index that covers optional (NOT NULL) columns will not be used to speed table

Re: [SQL] ORDER BY and NULLs

2004-09-19 Thread T E Schmitz
Hello Tom, Tom Lane wrote: T E Schmitz <[EMAIL PROTECTED]> writes: Greg Stark wrote: Which book is it? PostgreSQL by Korry Douglas + Susan Douglas, ISBN 0-7357-1257-3; Feb 2003 Hmm, I've heard of that book but never seen it. The authors are not participants in the PG community

Re: [SQL] ORDER BY and NULLs

2004-09-19 Thread T E Schmitz
Hello Greg, Greg Stark wrote: T E Schmitz <[EMAIL PROTECTED]> writes: Quote: "PostgreSQL will not index NULL values. Because an index will never include NULL values, it cannot be used to satisfy the ORDER BY clause of a query that returns all rows in a table." You should just cros

Re: [SQL] ORDER BY and NULLs

2004-09-19 Thread T E Schmitz
Hello Tom, Tom Lane wrote: T E Schmitz <[EMAIL PROTECTED]> writes: I read somewhere that an Index is not going to improve the performance of an ORDER BY if the sort column contains NULLs because NULLs aren't indexed? Whatever you were reading had it pretty badly garbled :-( I just

Re: [SQL] ORDER BY and NULLs

2004-09-19 Thread T E Schmitz
h something that sorts BEFORE the first most value that your TO result can return. Terry Fielder Manager Software Development and Deployment Great Gulf Homes / Ashton Woods Homes [EMAIL PROTECTED] Fax: (416) 441-9085 -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of

[SQL] ORDER BY and NULLs

2004-09-19 Thread T E Schmitz
Hello, I am using PostgreSQL 7.4.2 and as I understand NULL values always sort last. However, I have a table from which select using two numerical sort keys "FROM" and "TO". "TO" might be NULL and I would like to display those rows first (without sorting the column in descending order). Is the

Re: [SQL] double left outer join on the same table

2004-05-02 Thread T E Schmitz
on b1.BRAND_PK =BRAND_1_FK left outer join BRAND as b2 on b2.BRAND_PK =BRAND_2_FK Kind Regards, Tarlika Elisabeth Schmitz Adam Witney wrote: On 2/5/04 5:23 pm, "T E Schmitz" <[EMAIL PROTECTED]> wrote: Hello, I have two tables SECTION and BRAND. SECTION is related to BRAND via two forei

[SQL] double left outer join on the same table

2004-05-02 Thread T E Schmitz
Hello, I have two tables SECTION and BRAND. SECTION is related to BRAND via two foreign keys. I would like to select ALL SECTIONs whether the FKs are null or not and fetch the BRAND attributes in one SQL statement. In other words I need a double outer join. Is this possible at all? The followin