Re: [SQL] GROUP BY on a column which might exist in one of two tables

2008-06-25 Thread hubert depesz lubaczewski
On Wed, Jun 25, 2008 at 09:50:18AM -0400, Mark Stosberg wrote: > hits > hit_id > partner_id > > views > view_id > partner_id > > There is of course a "partners" table with a "partner_id" column. > > My target result is more like > > partner_id > total_views > total_hits select co

Re: [SQL] Unable to create function which takes no arguments

2008-06-09 Thread hubert depesz lubaczewski
On Mon, Jun 09, 2008 at 12:05:52PM -0400, Michael Eshom wrote: > I am trying to create a "UNIX_TIMESTAMP()" function in PostgreSQL, which > will return the current timestamp. However, whenever I try to add this > function in phpPgAdmin, it says 'Syntax error at or near ")" at > character 28'. y

Re: [SQL] Find all instances of a column in the entire database.

2008-05-16 Thread hubert depesz lubaczewski
On Fri, May 16, 2008 at 11:51:20PM +1000, Gavin 'Beau' Baumanis wrote: > I am hoping that you might be able to give me some assistance with the > following task! > I have a database with nearly 200 tables and I need to find all tables > that contain a column of myColumnName. select * from info

Re: [SQL] Difference in columns

2008-05-11 Thread hubert depesz lubaczewski
On Sun, May 11, 2008 at 01:37:52PM -0400, Mag Gam wrote: > Any thoughts about this? 1. will there be any gaps in between dates? if yes, what should be diff be then? 2. can't you calculate it in client application? 3. is usage of pl/pgsql acceptable (i think this will be the fastest way to do it in

Re: [SQL] After updating dataset the record goes to the end of the dataset

2008-04-22 Thread hubert depesz lubaczewski
On Tue, Apr 22, 2008 at 12:31:54PM +0200, Nacef LABIDI wrote: > When I have tested this with SQLServer it works well, since the rows doesn't > change position on the DB. > I hope that you understand my issue and I will provide any explanations if > someting isn't clear enough. well. in postgresql

Re: [SQL] After updating dataset the record goes to the end of the dataset

2008-04-22 Thread hubert depesz lubaczewski
On Tue, Apr 22, 2008 at 12:10:41PM +0200, Nacef LABIDI wrote: > Yes I don't issue any sort statement, and I indeed want the data to be show > as it is stored in the database. But after updating a row (I don't update > the ID, just some fields), it keeps its same place on the DB but jumps to > the e

Re: [SQL] Having a mental block with (self) outer joins

2008-04-21 Thread hubert depesz lubaczewski
On Mon, Apr 21, 2008 at 03:48:23PM +0200, Thomas Kellerer wrote: > name, id, parent_id > ROOT, 1, NULL > CHILD1, 2, 1 > CHILD2, 3, 1 > > I would have expected the following result: > > ROOT, NULL > ROOT, CHILD1 > ROOT, CHILD2 > > but the row with (ROOT,NULL) is not returned. why would you expe

Re: [SQL] Create on insert a unique random number

2008-03-19 Thread hubert depesz lubaczewski
On Tue, Mar 18, 2008 at 01:40:42PM -0500, Campbell, Lance wrote: > This is not a security approach. It is more about not giving obvious > access to people that want to mess around. 1. keep primary key using standard serial. it will make your life a bit simpler. 2. add column for text random ident

Re: [SQL] currval() within one statement

2008-01-22 Thread hubert depesz lubaczewski
On Tue, Jan 22, 2008 at 10:16:30AM +0300, silly_sad wrote: > INSERT INTO ttt (a,b) SELECT currval('ttt_id_seq'), 'const' FROM ttt2; > Where the trigger before insert on ttt is defined and this trigger calls > nextval('ttt_id_seq'). > I was surprised having different values of currval() in ttt.a >

Re: [SQL] statement-level trigger sample out there?

2007-11-30 Thread hubert depesz lubaczewski
On Thu, Nov 29, 2007 at 04:04:54PM +0100, Christian Kindler wrote: > I have a realy big table (> 2'000'000 records). every second there are > several inserts and updates. the thing is i need a last row reference > depending on a foreing_key. > > something like this: > > id, foreign_key, last_

Re: [SQL] ALL() question

2007-11-14 Thread hubert depesz lubaczewski
On Wed, Nov 14, 2007 at 02:39:02PM +0100, Julien Cigar wrote: > With the following I got a syntax error: > select specimen_id > from specimen_test_bits > where test_bit_id = all(1,2,3,4); where test_bit_id in (1,2,3,4) group by specimen_id having count(distinct test_bit_id) = 4; depesz -- qui

Re: [SQL] Optimize querry sql

2007-09-17 Thread hubert depesz lubaczewski
On Mon, Sep 17, 2007 at 09:17:58AM +0200, Stanislas de Larocque wrote: > Have you advices to optimize the query please ? for some many rows the 400ms looks quite reasonable. the best thing you can make to speed things up is to calculate the counts with triggers. depesz -- quicksil1er: "postgre

Re: [SQL] Optimize querry sql

2007-09-14 Thread hubert depesz lubaczewski
On Fri, Sep 14, 2007 at 03:02:59PM +0200, Stanislas de Larocque wrote: > I want to optimize my query sql (execution time : 2665 ms) : SELECT b.idxreseller, sum(a.nbrq), b.namereseller from stat a, reseller b where b.asp=6 and a.idxreseller=b.reseller and a.month=d

Re: [SQL] Optimize querry sql

2007-09-14 Thread hubert depesz lubaczewski
On Fri, Sep 14, 2007 at 12:26:00PM +0200, Stanislas de Larocque wrote: > Explain my sql querry : did you notice, that andreas asked: > > Show us the output from EXLAIN ANALYSE . depesz -- quicksil1er: "postgres is excellent, but like any DB it requires a highly paid DBA. here's my CV!" :) htt

Re: [SQL] fetch first rows of grouped data

2007-08-28 Thread hubert depesz lubaczewski
On Tue, Aug 28, 2007 at 08:00:42AM -0500, Michael Glaesemann wrote: > >you can try to use "distinct on". > I considered that as well, but couldn't think of a way to return more you're right and i was wrong. i simply missed the word "two" in original question. sorry. depesz -- quicksil1er: "po

Re: [SQL] fetch first rows of grouped data

2007-08-28 Thread hubert depesz lubaczewski
On Mon, Aug 27, 2007 at 06:36:47PM +0200, Claudia Kosny wrote: > Is there a way to do this with one query? > I am using PostgreSQL 7.4. you can try to use "distinct on". depesz -- quicksil1er: "postgres is excellent, but like any DB it requires a highly paid DBA. here's my CV!" :) http://www.d

Re: [SQL] Trigger Procedure Error: NEW used in query that is not in a rule

2007-08-11 Thread hubert depesz lubaczewski
On Sat, Aug 11, 2007 at 02:45:09AM -0500, Javier Fonseca V. wrote: > I think that it's working alright except for the next line: doing this in plpgsql is very complicated (or even impossible assuming that any table can have the same trigger). i would rather suggest using pl/perl - writing somethin

Re: [SQL] Index usage in order by with multiple columns in order-by-clause

2007-08-11 Thread hubert depesz lubaczewski
On Fri, Aug 10, 2007 at 04:53:12PM +0200, Andreas Joseph Krogh wrote: > I have the following test-case: > > CREATE TABLE test( > name varchar PRIMARY KEY, > value varchar NOT NULL, > created timestamp not null > ); > > create index test_lowernamevalue_idx ON test ((lower(name) || lower(value)));

Re: [SQL] Best Fit SQL query statement

2007-08-11 Thread hubert depesz lubaczewski
On Fri, Aug 10, 2007 at 08:13:46PM -0500, Rodrigo De León wrote: > On 8/10/07, hubert depesz lubaczewski <[EMAIL PROTECTED]> wrote: > > unfortunatelly this query will be hard to optimize. > > Uh, how about > > SELECT MAX(t1) > FROM t1 > WHERE '9849'

Re: [SQL] Best Fit SQL query statement

2007-08-10 Thread hubert depesz lubaczewski
On Fri, Aug 10, 2007 at 04:40:34PM -0300, Fernando Hevia wrote: > Found your query is shorter and clearer, problem is I couldn't have it use > an index. Thought it was a locale issue but adding a 2nd index with > varchar_pattern_ops made no difference. > In result, it turned out to be too slow in c

Re: [SQL] Using function like where clause

2007-08-10 Thread hubert depesz lubaczewski
On Mon, Aug 06, 2007 at 04:44:29PM -0300, Ranieri Mazili wrote: > 1) Can I use a function that will return a string in a where clause like > bellow? > 2) Can I use a function that will return a string to return the list of > columns that I want to show like below? not in sql. you can in pl/pgsql

Re: [SQL] Best Fit SQL query statement

2007-08-10 Thread hubert depesz lubaczewski
On Mon, Aug 06, 2007 at 01:57:07AM -0700, Kiran wrote: > Could anyone help me in writing Best Fit SQL statement. > Suppose we have table t1 with coloumn t1 (text) with following rows. > 98456 > 98457 > 9845 > 9846 > 984 > 985 > 98 > 99 > and if I query on 98456 the result must be 98456, > However

[SQL] tree structures in sql - my point of view (with request of comment from joe celko)

2002-09-03 Thread Hubert depesz Lubaczewski
see my idea, is it worth it, do you know any better way to store trees in sql? best regards depesz -- hubert depesz lubaczewski http://www.depesz.pl/ Mój Boże, spraw abym milczał, dopóki się nie upewni

Re: [SQL] RFC822 Checker

2002-09-03 Thread Hubert depesz Lubaczewski
in my queue because the MTA (correctly) refuses to process > some of the bogus things that users enter by mistake. you might want to check: ftp://ftp.cpan.org/CPAN/authors/Tom_Christiansen/scripts/ckaddr.gz since this is in pure perl, making a pl/perl function should be pretty simple. depesz --

Re: [SQL] SQL problem with aggregate functions.

2002-07-22 Thread Hubert depesz Lubaczewski
table; should work the way you want it. anyway, i belive that making this: select field_group, field, count(*) from table where field in ('D','R','X') group by field_group, field; and then processing results in client application, should be a little bit

[SQL] creating tables with different character set?

2001-02-23 Thread hubert depesz lubaczewski
to make another database (with another postmaster process), but this is definetly not easy way. any other options? depesz -- hubert depesz lubaczewski http://www.depesz.pl/ najwspanialszą rzeczą

[SQL] how to alter/drop check contraint?

2000-12-20 Thread hubert depesz lubaczewski
ert or update when something occurs? depesz -- hubert depesz lubaczewski najwspanialszą rzeczą jaką dało nam nowoczesne społeczeństwo, jest niesamowita wręcz łatwość unikania kontaktów z nim ...

Re: [SQL] How to represent a tree-structure in a relational database

2000-12-14 Thread hubert depesz lubaczewski
QUE AS ' BEGIN IF NEW.active=''t''::bool and OLD.active=''f''::bool THEN UPDATE groups SET active=''t''::bool WHERE id=NEW.group_id; END IF; RETURN NEW; END; ' LANGUAGE 'plpgsql'; CREATE TRIG

Re: [SQL] subselects

2000-12-02 Thread hubert depesz lubaczewski
m in (190) and rep.a = dir.u; this should (i guess) work perfectly depesz -- hubert depesz lubaczewski najwspanialszą rzeczą jaką dało nam nowoczesne społeczeństwo, jest niesamowita wręcz łatwość unikania kontaktów z nim ...

[SQL] how to write it in most efficient way?

2000-11-09 Thread hubert depesz lubaczewski
d be a better/faster way to do it. my tables have primary keys, foreign key (groups.id <=> g_order.group_id), indices. any idea how to write a better select to do what i need? or maybe the one i wrote is the best one? depesz -- hubert depesz lubaczewski

[SQL] Question about ordering views

2000-11-05 Thread hubert depesz lubaczewski
epesz -- hubert depesz lubaczewski najwspanialszą rzeczą jaką dało nam nowoczesne społeczeństwo,

[SQL] Time penalty on VIEWS on VIEWS

2000-11-05 Thread hubert depesz lubaczewski
some kind of time penalty. I'm wondering how much it is. regards, depesz -- hubert depesz lubaczewski najwspanialszą rzeczą jaką dało nam nowoczesne społeczeństwo, jest niesamowita wręcz łatwość uni

Re: [SQL] Synonyms

2000-10-30 Thread hubert depesz lubaczewski
gt; within a select I can make a query from mulitple databases. ( the usual > DB.Table or DB:table or DB@table dont seem to work) don't know how informix works, but why dont you just use a view? create view synonym as select * from table; am i missing something? depesz -- huber

[SQL] problem with select where like ']'

2000-10-17 Thread hubert depesz lubaczewski
n doesn't work i tried escaping of it like \], \\] or even \\\] but none of these worked. right now i'm using comparison: where substring (...) = '...' and it works even with those "]" signs. but i'm just wondering why like ']&#x