Re: [SQL] Complicated "group by" question

2004-08-25 Thread Andrew Perrin
Excellent - thanks, Josh! -- Andrew J Perrin - http://www.unc.edu/~aperrin Assistant Professor of Sociology, U of North Carolina, Chapel Hill [EMAIL PROTECTED] * andrew_perrin (at) unc.edu On Wed, 25 Aug 2004, Josh Berkus wrote:

[SQL] Complicated "group by" question

2004-08-25 Thread Andrew Perrin
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 "accept". I think I should

[SQL] "Best practice" advice

2003-01-17 Thread Andrew Perrin
I'm developing the second stage of a database that will eventually be used to model networks of references between documents. I already have a table of core documents, and the next step is to track what documents each of these core documents refers to. (Is this confusing enough already?) The rela

Re: [SQL] RFC: A brief guide to nulls

2003-01-15 Thread Andrew Perrin
*Disclaimer:* I do not have any formal training in database theory. However, I have done quite a lot of work with databases in practice, and have some understanding of "missing values" in statistics. I would tend to think of the "no applicable value" case of a NULL as being the result of poor or l

Re: [SQL] unnecessary updates

2002-10-30 Thread Andrew Perrin
One strategy is to use some sort of middleware that takes care of this. On a project I did a few years ago, I used a perl module that read the record from Postgres and made it into a perl object. The object contained a variable, "changed", that reflected whether anything had actually changed in the

Re: [SQL] error...what to do?

2002-10-18 Thread Andrew Perrin
Read the error text: > beckerbalab2=> SELECT ffix_ability.name, ffix_ability.cost ^ > beckerbalab2-> FROM ffix_can_learn NATURAL JOIN ffix_ability > beckerbalab2-> WHERE ffix_can_learn.character_name = 'Zidane' > beckerbalab2-> EXCEPT --this is the differe

Re: [SQL] Case Sensitive "WHERE" Clauses?

2002-09-26 Thread Andrew Perrin
No, I don't think it's supposed to be case-sensitive. In any case, whether it's supposed to be or not, it certainly isn't in practice. Solutions include: SELECT * FROM People WHERE lower(first_name)='jordan'; and: SELECT * FROM People WHERE first_name ~* 'Jordan'; ap

Re: [SQL] SQL formatter?

2002-09-26 Thread Andrew Perrin
o maybe you could just write a sql one and > it would just work. Of course I've never used one and don't know anything > about it really so I could be wrong :) > > -philip > > On Wed, 25 Sep 2002, Andrew Perrin wrote: > > > Does anyone know of a routin

[SQL] SQL formatter?

2002-09-25 Thread Andrew Perrin
Does anyone know of a routine for formatting SQL statements in a structured way? Standalone or for emacs would be fine. I'm thinking of something that could take a long SQL text statement and format it, e.g.: select foo from bar where baz and bop and not boo; becomes SELECT foo FROM

Re: [SQL] LIMIT within UNION?

2002-09-12 Thread Andrew Perrin
On Thu, 12 Sep 2002, Stephan Szabo wrote: > On Thu, 12 Sep 2002, Andrew Perrin wrote: > > > Greetings- > > > > I have a table of participants to be contacted for a study. Some are in > > the "exposure" group, others in the "control" group

Re: [SQL] LIMIT within UNION?

2002-09-12 Thread Andrew Perrin
On 12 Sep 2002, Roland Roberts wrote: > >>>>> "Andrew" == Andrew Perrin <[EMAIL PROTECTED]> writes: > ... > Can you do this via a subselect: > > SELECT * FROM > ( SELECT ... FROM participants > WHERE typenr=1 AND > U

Re: [SQL] LIMIT within UNION?

2002-09-12 Thread Andrew Perrin
Assistant Professor of Sociology, U of North Carolina, Chapel Hill [EMAIL PROTECTED] * andrew_perrin (at) unc.edu On Thu, 12 Sep 2002, Tom Lane wrote: > Andrew Perrin <[EMAIL PROTECTED]> writes: > > SELECT ... FROM participants > > WHERE typenr=1 AND > > UNION >

[SQL] LIMIT within UNION?

2002-09-12 Thread Andrew Perrin
Greetings- I have a table of participants to be contacted for a study. Some are in the "exposure" group, others in the "control" group. This is designated by a column, typenr, that contains 1 for exposure, 2 for control. The complication is this: I need to select 200 total. The 200 number should

Re: [SQL] behavior of ' = NULL' vs. MySQL vs. Standards

2001-06-12 Thread ANDREW PERRIN
Interesting - my experience is that Access, at least, generally treats NULL's correctly: (This was done under Access 2000): create table foo (name text(20)) insert into foo values ("bar"); insert into foo values ("bar"); insert into foo values ("bar"); insert into foo values ("bar"); insert into

Re: [SQL] Case Insensitive Queries

2001-06-05 Thread ANDREW PERRIN
Try: - The ILIKE operator, for example, SELECT * FROM account WHERE username ILIKE "test"; - upper() or lower(), for example, SELECT * FROM accont WHERE lower(username) = "test"; - Andrew J. Perrin - Assistant Professor of Sociology

Re: [SQL] Select most recent record?

2001-05-16 Thread Andrew Perrin
Except that he wants max(timestamp) by id; perhaps a GROUP BY would help, something like (untested): select max(timestamp) from log group by id; Tom Lane wrote: > > "Marc Sherman" <[EMAIL PROTECTED]> writes: > > I'd like to select the newest (max(timestamp)) row for each id, > > before a given

Re: [SQL] use of arrow keys to traverse history

2001-04-25 Thread Andrew Perrin
These do suggest (although not require) that the *user* postgres will be running bash when logged in. To check for sure, do: finger postgres which will give you the current shell among other things. However, this doesn't speak to what I think you're asking, which is command history and completi

Re: [SQL] serial type; race conditions

2001-03-29 Thread Andrew Perrin
I ditto what Bruce said - trying to get a true sequence without gaps is a losing battle. Why don't you, instead, use a serial column as the real sequence, and then a trigger that simply inserts max(foo) + 1 in a different column? Then when you need to know the column, do something like: SELECT nu

Re: [SQL] all views in database broken at once

2001-03-24 Thread Andrew Perrin
, Tom Lane wrote: > Andrew Perrin <[EMAIL PROTECTED]> writes: > > fgdata=# \d sx_l_m_r_a > > ERROR: cache lookup of attribute 197 in relation 47074 failed > > fgdata=# select * from pg_views; > > ERROR: cache lookup of attribute 317 in relation 48494 failed > &g

[SQL] all views in database broken at once

2001-03-24 Thread Andrew Perrin
Greetings- I'm in a bit of a pickle. I rebuilt a big query on top of which lots of little queries rest, so as to use some new columns in the query. Now, I get error messages when trying to access any view that SELECTs from the rebuilt query: fgdata=# \d sx_l_m_r_a ERROR: cache lookup of attrib

Re: [SQL] trigger output to a file

2001-03-23 Thread Andrew Perrin
ax you can get what you're looking for by simply JOINing Records and Data. Then, when you want to "change" a record - say, for example, Andrew Perrin moves from Berkeley to Chapel Hill, thereby changing phones from 510-xxx- to 919-xxx- - you actually *add* a new record, with a hi

Re: [SQL] creating "job numbers"

2001-03-22 Thread Andrew Perrin
Check out nextval() and currval(). They do exactly what you need. They're also specific to the current backend, so you can guarantee that the same value won't be passed to two different frontend sessions. nextval('sequencename') -> the number that will be assigned next in

Re: [SQL] Rule/currval() issue

2001-03-14 Thread Andrew Perrin
Entirely untested, but how about replacing currval() in your first try with nextval()? My theory is that the compilation of the function is happening before the INSERT happens; therefore the sequence hasn't been incremented yet; therefore there's no currval() for this backend instance. If you use

Re: [SQL] SQL Dummy Needs Help

2001-03-12 Thread Andrew Perrin
Well, you should probably get yourself a good SQL book :) but here's a try (untested). How about something like: SELECT DISTINCT title_no, paidto_date FROMtable1, table2 WHERE table1.title_no = table2.title_no AND table1.effect_date <> table2.paidto_date; Again

Re: [SQL] PL/PgSQL and NULL

2001-03-11 Thread Andrew Perrin
trom wrote: > > On Sun, Mar 11, 2001 at 10:38:10PM +0100, Peter Eisentraut wrote: > > Andrew Perrin writes: > > > > > I'm trying to write what should be a simple function that returns the > > > minimim of two integers. The complication is that when one of th

[SQL] PL/PgSQL and NULL

2001-03-11 Thread Andrew Perrin
Greetings- I'm trying to write what should be a simple function that returns the minimim of two integers. The complication is that when one of the two integers is NULL, it should return the other; and when both are NULL, it should return NULL. Here's what I've written: CREATE FUNCTION min(int4,

[SQL] recompiling to use gnu readline?

2001-03-10 Thread Andrew Perrin
Folks- I inadvertantly compiled pg 7.0.3 without gnu readline installed, so I now don't have access to lots of the nice command-line utilities it would have conveyed. Is there any way to add in the functionality now, or do I need to recompile? If I need to recompile, how can I do so without wipin

Re: [SQL] How do I use text script containing SQL?

2001-03-06 Thread Andrew Perrin
psql \i filename.txt -Andy Perrin "Jeff S." wrote: > > I want to build my tables by placing all the sql > statements in a file. What is the correct way to use > this file with psql? > > Example: My text file has this in it: > > CREATE TABLE table1 ( >table1_id serial, >field1 char(5

Re: [SQL] create function w/indeterminate number of args?

2001-03-02 Thread Andrew Perrin
',' || arg2 || ',' || arg3 || ',' || arg4 || ',' || > arg5) > > i.e.: > > create function minimum(text) returns integer > > and then do the parsing internally ('specially if you're using perl). > Pretty bad, but it's an opti

[SQL] create function w/indeterminate number of args?

2001-02-25 Thread Andrew Perrin
Greetings. I find myself in need of a minimum() function. This is different from the min() function, which operates across records; what I need is to be able to say: UPDATE foo SET newcol = minimum(field1, field2, field3, field4); >From what I can tell there is no such beast built in, but I wou

Re: [SQL] sum(bool)?

2001-02-23 Thread Andrew Perrin
Or how about just: SELECT count(*) FROM tablename WHERE a > b; -- Andrew J Perrin - Ph.D. Candidate, UC Berkeley, Dept. of Sociology Chapel Hill, North Carolina, USA - http://demog.berkeley.edu/~aperrin [EMAIL PROTECTE

Re: [SQL] Help retrieving lastest record

2001-02-16 Thread Andrew Perrin
How 'bout these: fgdata=# select * from fruit order by dt desc limit 1; number | fruit | dt +-+ 20 | Oranges | 2000-06-07 00:00:00-05 (1 row) fgdata=# select * from fruit where fruit='Apples' order by dt desc limit 1; number |

[SQL] "Subclassing" in SQL

2001-02-06 Thread Andrew Perrin
I hope that title line is reasonably accurate. Here's what I'm trying to do, and would love it anyone can provide guidance. I have a table of utterances in a focus group setting; each record contains an identifier for the speaker and group, as well as the length of the utterance (in words) and th