Re: [GENERAL] SQL Question

2014-04-01 Thread Robert DiFalco
Heh, scratch that, the EXISTS query DOES work. Is this the most efficient way to perform this kind of query? Thanks! On Tue, Apr 1, 2014 at 1:21 PM, Robert DiFalco robert.difa...@gmail.comwrote: I have two queries I would like to combine into one. I have a table that represents a user's

Re: [GENERAL] SQL Question

2014-04-01 Thread Paul Jungwirth
Is this the most efficient way to perform this kind of query? I don't think there is one answer that's always correct, but you could compare it with a LEFT OUTER JOIN. There are lots of articles and blog posts about EXISTS vs OUTER JOIN vs IN, for all the major RDBMSes. Note that not all these

Re: [GENERAL] SQL Question

2014-04-01 Thread John R Pierce
On 4/1/2014 1:27 PM, Robert DiFalco wrote: Heh, scratch that, the EXISTS query DOES work. Is this the most efficient way to perform this kind of query? Thanks! I would try and express that as a left outer join, and use (c2.owner_id IS NOT NULL) as your boolean field (or something like that)

Re: [GENERAL] SQL question on chunking aggregates

2014-03-04 Thread Merlin Moncure
On Tue, Mar 4, 2014 at 1:49 PM, Owen Hartnett o...@clipboardinc.com wrote: Hi all: I have a table that has multiple records for a single owner_id. I'm able to use array_arg to combine the records into a single row, which works fine. I'm using this sql: select owner_id,

Re: [GENERAL] SQL question on chunking aggregates

2014-03-04 Thread David Johnston
Merlin Moncure-2 wrote On Tue, Mar 4, 2014 at 1:49 PM, Owen Hartnett lt; owen@ gt; wrote: It looks like I should be able to use the window function to do this, but I've been unsuccessful. The following runs, but doesn't seem to have any effect: select owner_id,

Re: [GENERAL] SQL question re aggregates joins

2010-01-28 Thread Thom Brown
On 28 January 2010 21:32, Scott Ribe scott_r...@killerbytes.com wrote: OK, this does not work: select max(t1.When), t1.Pt_Id, t2.DateOfBirth from PtStaffAccess t1, Person t2 where t1.Pt_Id = t2.id group by t1.Pt_Id; But this does: select max(t1.When), t1.Pt_Id, min(t2.DateOfBirth) from

Re: [GENERAL] SQL question re aggregates joins

2010-01-28 Thread Scott Ribe
You can't include an aggregate in the select if you don't group by non-aggregates, so it should be: select max(t1.When), t1.Pt_Id, t2.DateOfBirth from PtStaffAccess t1, Person t2 where t1.Pt_Id = t2.id group by t1.Pt_Id, t2.DateOfBirth; I was aware that I could alternatively group by all

Re: [GENERAL] SQL question re aggregates joins

2010-01-28 Thread Dann Corbit
-Original Message- From: pgsql-general-ow...@postgresql.org [mailto:pgsql-general- ow...@postgresql.org] On Behalf Of Scott Ribe Sent: Thursday, January 28, 2010 2:10 PM To: Thom Brown Cc: pgsql-general Subject: Re: [GENERAL] SQL question re aggregates joins You can't include

Re: [GENERAL] SQL question re aggregates joins

2010-01-28 Thread Tom Lane
Scott Ribe scott_r...@killerbytes.com writes: Given that t2.id is the primary key, grouping by any other column of t2 is really redundant. I know *what* SQL won't allow me to do, I'm interested in knowing if there's some reason *why* other than historical... SQL92 says so. More recent

Re: [GENERAL] SQL Question - Recursion

2008-08-26 Thread Guy Rouillier
ktr73 wrote: Hi - Was wondering if anyone could help / had some thoughts. I am building a model for a client, and right now doing customer attrition modeling. Basically, the number of customers in this period is equal to: Beg # Customers + customers added this period - attrition Ending #

Re: [GENERAL] SQL question

2008-03-28 Thread Adam Rich
I have a table that has 3 date columns : create table xyz ( xyz_id integer, date1 timestamp, date2 timestamp, date3 timestamp ) I want to select in a query the xyz_id and the max date column for each row something like : create table temp2 as select xyz_id (max date?) where

Re: [GENERAL] SQL question: checking all required items

2007-08-17 Thread Carlos Ortíz
? Try Select * from people where person_id in ( Select person_ID from Items_for_people group by Person_id Having Count(*) = ( Select count(*) from Items Where is_required = true)) Or something like that. That's the idea. Probe it and tell us. (May be the sintaxis it's not

Re: [GENERAL] SQL question: checking all required items

2007-08-11 Thread Raymond O'Donnell
On 10/08/2007 22:03, Carlos Ortíz wrote: Select * from people where person_id in ( Select person_ID from Items_for_people group by Person_id Having Count(*) = ( Select count(*) from Items Where is_required = true)) That seems to work fine! I'd only change having count(*) =

Re: [GENERAL] SQL question: checking all required items

2007-08-11 Thread Raymond O'Donnell
On 10/08/2007 21:42, Scott Marlowe wrote: Show us the query when you're done, I'm sure there are enough folks who'd like to see your solution. Here's what I came up with: select distinct ip.person_id from items_for_people ip where exists ( ( select item_id from items

Re: [GENERAL] SQL question: checking all required items

2007-08-10 Thread Scott Marlowe
On 8/10/07, Raymond O'Donnell [EMAIL PROTECTED] wrote: - how can I find those people who don't have _all_ of the items which are marked required? In other words, how do I select those rows in people which don't have a corresponding row in items_for_people for *each* row in items which has

Re: [GENERAL] SQL question: checking all required items

2007-08-10 Thread Raymond O'Donnell
On 10/08/2007 21:29, Scott Marlowe wrote: select table1.id from table1 where table1.id is not in (select id from table2); Duh! I should have thought of that thanks for that, and apologies for the stupidity (blame it on the glass of wine I had with dinner!). Ray.

Re: [GENERAL] SQL question: checking all required items

2007-08-10 Thread Scott Marlowe
On 8/10/07, Raymond O'Donnell [EMAIL PROTECTED] wrote: On 10/08/2007 21:29, Scott Marlowe wrote: select table1.id from table1 where table1.id is not in (select id from table2); Duh! I should have thought of that thanks for that, and apologies for the stupidity (blame it on the glass

Re: [GENERAL] SQL Question - Group By and % results per row

2007-03-15 Thread Bruno Wolff III
On Mon, Mar 12, 2007 at 12:53:11 -0700, Mike [EMAIL PROTECTED] wrote: How do I get access to the total of all clicks on per row basis so I can divide it? The only solution that comes to my mind is create a subquery that does a (select count(*) from... where... ) of the original grouped by

Re: [GENERAL] SQL Question - Using Group By

2007-02-26 Thread Alban Hertroys
[EMAIL PROTECTED] wrote: You could use COUNT() in conjunction with NULLIF: select Type, count(nullif(Active, false)) as Active Count, count(nullif(Active, true)) as Inactive Count, 100 * count(nullif(Active, false)) / count(*) as Active Percent from table_name group by Type Tom Lane

Re: [GENERAL] SQL Question - Using Group By

2007-02-25 Thread [EMAIL PROTECTED]
You could use COUNT() in conjunction with NULLIF: select Type, count(nullif(Active, false)) as Active Count, count(nullif(Active, true)) as Inactive Count, 100 * count(nullif(Active, false)) / count(*) as Active Percent from table_name group by Type On Feb 23, 2:50 pm, Mike [EMAIL PROTECTED]

Re: [GENERAL] SQL Question - Using Group By

2007-02-25 Thread Mike
Thank you! Exactly what I needed. Mike On Feb 23, 4:42 pm, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: You could use COUNT() in conjunction with NULLIF: select Type, count(nullif(Active, false)) as Active Count, count(nullif(Active, true)) as Inactive Count, 100 * count(nullif(Active,

Re: [GENERAL] SQL Question - Using Group By

2007-02-25 Thread Tomas Vondra
Hi, I have a question about using Group By. On a table like this: Type (varchar) | Active (boolean) Type One | False Type Two | True Type One | True Type Fifty | Flase

Re: [GENERAL] sql question; checks if data already exists before

2006-06-23 Thread Kostas Maistrelis
nuno wrote: hi, there. i'm trying to write a SQL statement which does the following things. 1. checks if data already exists in the database 2. if not, insert data into database otherwise skip. Check this thread : http://archives.postgresql.org/pgsql-general/2005-10/msg01787.php

Re: [GENERAL] sql question; checks if data already exists before inserted

2006-06-22 Thread Martijn van Oosterhout
On Wed, Jun 21, 2006 at 10:59:42PM -0700, nuno wrote: insert into student (studentid, fname, lname) select 'JORDANMICHAEL', 'Michale', 'Jordan' from student where studentid not in (select studentid from student); however, this does not seem to work. it does not insert data even if it does

Re: [GENERAL] sql question; checks if data already exists before inserted

2006-06-22 Thread Guy Rouillier
nuno wrote: hi, there. i'm trying to write a SQL statement which does the following things. 1. checks if data already exists in the database 2. if not, insert data into database otherwise skip. for example, i'd like to insert a student called 'Michael Jordan' whose ID is 'JORDANMICHAEL'

Re: [GENERAL] [SQL] Question on indexes

2005-12-19 Thread Alvaro Herrera
Emil Rachovsky wrote: Hi, Can anyone show me a simple way of creating an index in PostGre like that: create index indName on someTable(someIntColumn DESC) ? Not using that particular syntax, but you can do that if you create the appropiate operator classes. Note that if you want to use

Re: [GENERAL] [SQL] question

2005-08-29 Thread Matt A.
The issue has been solved thanks to a custom nullif_int() function. Which if anyone has the same issue, it was solved with... CREATE FUNCTION nullif_int(text) RETURNS integer AS 'SELECT nullif($1,)::int;' LANGUAGE SQL; SELECTS were not the issue; INSERT INTO a non-text column was the

Re: [GENERAL] SQL question.

2005-06-01 Thread Jim Buttafuoco
how about (untested) select * from ( SELECT DISTINCT ON (linkid) * FROM all_links WHERE uid='2' AND DATE_TRUNC('day',read_date) = DATE_TRUNC('day',TIMESTAMP '2005-06-01') ORDER BY linkid ) A ORDER BY read_date DESC limit 100 -- Original Message --- From: FC [EMAIL

Re: [GENERAL] SQL question.

2005-06-01 Thread Martijn van Oosterhout
On Wed, Jun 01, 2005 at 04:40:48PM +0200, FC wrote: Hello SQL Aces ! I want to do a select on a table distinct on linkid and sorted by date. I have try this How about a subquery?: SELECT * FROM ( SELECT DISTINCT ON (linkid) * FROM all_links WHERE uid='2' AND

Re: [GENERAL] SQL question.

2005-06-01 Thread FC
I need to remember to keep things simple... Works fine, thanks. Fred On Jun 1, 2005, at 5:20 PM, Jim Buttafuoco wrote: how about (untested) select * from ( SELECT DISTINCT ON (linkid) * FROM all_links WHERE uid='2' AND DATE_TRUNC('day',read_date) = DATE_TRUNC ('day',TIMESTAMP

Re: [GENERAL] SQL Question

2005-04-17 Thread Alex
Julian Scarfe wrote: From: Alex [EMAIL PROTECTED] - How can i select only the newest record for each ProdId ? 100| 2005-04-01 200| 2005-04-01 DISTINCT ON was made for this and on the similar tables I have performs rather more efficiently than using a subquery. select distinct on (ProdId)

Re: [GENERAL] SQL Question

2005-04-16 Thread Julian Scarfe
From: Alex [EMAIL PROTECTED] - How can i select only the newest record for each ProdId ? 100| 2005-04-01 200| 2005-04-01 DISTINCT ON was made for this and on the similar tables I have performs rather more efficiently than using a subquery. select distinct on (ProdId) ProdId , LastUpdate

Re: [GENERAL] SQL Question

2005-04-15 Thread Thomas Kellerer
On 15.04.2005 13:58 Alex wrote: Hi, i have a table ProdId | LastUpdate ---+ 100| 2005-04-01 100| 2005-03-01 100| 2005-02-01 200| 2005-04-01 200| 2005-03-01 200| 2005-02-01 - How can i select only the newest record for each ProdId ? 100

Re: [GENERAL] SQL Question

2005-04-15 Thread GIROIRE, Nicolas (COFRAMI)
For the first request (How can i select only the newest record for each ProdId ?), you can do : select * from test.tableProd u where u.LastUpdate = (select max(t.LastUpdate) from test.tableProd t where

Re: [GENERAL] SQL Question

2005-04-15 Thread Richard Huxton
GIROIRE, Nicolas (COFRAMI) wrote: For the first request (How can i select only the newest record for each ProdId ?), you can do : select * from test.tableProd u where u.LastUpdate = (select max(t.LastUpdate) from test.tableProd t where u.ProdId = t.ProdId) Although this only

Re: [GENERAL] SQL Question

2005-04-15 Thread GIROIRE, Nicolas (COFRAMI)
(COFRAMI) Cc : Alex; pgsql-general@postgresql.org Objet : Re: [GENERAL] SQL Question GIROIRE, Nicolas (COFRAMI) wrote: For the first request (How can i select only the newest record for each ProdId ?), you can do : select * from test.tableProd u where u.LastUpdate = (select max(t.LastUpdate

Re: [GENERAL] SQL Question

2005-04-15 Thread jday
select max(lastupdate),prodid from tablename group by prodid -Original Message- From: Thomas Kellerer [EMAIL PROTECTED] Subj: Re: [GENERAL] SQL Question Date: Fri Apr 15, 2005 7:24 am Size: 621 bytes To: pgsql-general@postgresql.org On 15.04.2005 13:58 Alex wrote: Hi, i have

Re: [GENERAL] SQL Question

2005-04-15 Thread Thomas Kellerer
[EMAIL PROTECTED] wrote on 15.04.2005 16:42: select max(lastupdate),prodid from tablename group by prodid Even better :) Thomas ---(end of broadcast)--- TIP 8: explain analyze is your friend

Re: [GENERAL] SQL Question

2005-04-15 Thread Bruno Wolff III
On Fri, Apr 15, 2005 at 21:58:31 +1000, Alex [EMAIL PROTECTED] wrote: Hi, i have a table ProdId | LastUpdate ---+ 100| 2005-04-01 100| 2005-03-01 100| 2005-02-01 200| 2005-04-01 200| 2005-03-01 200| 2005-02-01 - How can i select only the

Re: [GENERAL] sql question

2005-03-14 Thread Ragnar Hafstað
On Sun, 2005-03-13 at 23:13 -0600, George Essig wrote: On Fri, 11 Mar 2005 13:26:07 +0100, Steven Verhoeven [EMAIL PROTECTED] wrote: [snip problem] select id, fref as ref from my_table union select id, mref as ref from my_table; union ALL (see other replies) gnari

Re: [GENERAL] sql question

2005-03-14 Thread Chris Travers
Steven Verhoeven wrote: Hi all My table definition : id | fref | mref --+---+-- 1 | 23| 25 2 | 24| 28 3 | 25| 31 4 | 26| 34 My problem : i need a query that results in this : id |ref --+-- 1

Re: [GENERAL] sql question

2005-03-13 Thread Vincent Hikida
SELECT t1.id , t1.fref FROM t1 UNION ALL SELECT t2.id , t2.mref FROM t2 - Original Message - From: Steven Verhoeven To: pgsql-general@postgresql.org ; [EMAIL PROTECTED] Sent: Friday, March 11, 2005 4:36 AM Subject: [GENERAL] sql question Hi all My table

Re: [GENERAL] sql question

2005-03-13 Thread Russell Smith
On Fri, 11 Mar 2005 11:36 pm, Steven Verhoeven wrote: Hi all My table definition : id | fref | mref --+---+-- 1 | 23| 25 2 | 24| 28 3 | 25| 31 4 | 26| 34 My problem : i need a query that results in

Re: [GENERAL] sql question

2005-03-13 Thread Klint Gore
On Fri, 11 Mar 2005 13:36:17 +0100, Steven Verhoeven [EMAIL PROTECTED] wrote: Hi all My table definition : id | fref | mref --+---+-- 1 | 23| 25 2 | 24| 28 3 | 25| 31 4 | 26| 34 My problem : i need

Re: [GENERAL] sql question

2005-03-13 Thread George Essig
On Fri, 11 Mar 2005 13:26:07 +0100, Steven Verhoeven [EMAIL PROTECTED] wrote: My table definition : id | fref | mref --+---+-- 1 | 23| 25 2 | 24| 28 3 | 25| 31 4 | 26| 34 i need a query that results in

Re: [GENERAL] SQL question

2004-11-08 Thread Uwe C. Schroeder
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Thanks for your answers Greg Vincent. Although I solved the problem by a change of schema - I'm happy that I have something to digest I didn't know before. One never learns enough ... U.C. On Saturday 06 November 2004 03:13 pm, Uwe C. Schroeder

Re: [GENERAL] SQL question

2004-11-07 Thread Greg Stark
Uwe C. Schroeder [EMAIL PROTECTED] writes: Here's a question for the SQL guru's out there, which I've been trying to solve for the last couple of hours. There's got to be a solution to this, but somehow I can't find it. Tables: table1 ( uid int PK, uname varchar(64) )

Re: [GENERAL] SQL question

2004-11-06 Thread Uwe C. Schroeder
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 If someone knows this it would be great - because I'm still curious how to solve it. However I just remodelled my db structure to eliminate the problem (basically I pulled the several tables into one since each of the table2/table3 tables only has

Re: [GENERAL] SQL question

2004-11-06 Thread Vincent Hikida
I'm afraid, I'm not used to SQL92 join syntax and almost all my experience is in Oracle but how about: SELECT t1.uid , t1.xname , t2.uid , t3.uid FROM table1 t1 INNER JOIN table2 t2 ON t1.uid = t2.uid

Re: [GENERAL] SQL-question: returning the id of an insert querry

2003-11-12 Thread Andrew Sullivan
On Mon, Nov 10, 2003 at 08:56:03AM -0800, Scott Chapman wrote: I talked with the author or SQLObject about this recently and I thnk he's implementing this correctly, by querying the cursor for the last OID?: That won't scale unless you index oid. And your tables will all need oids, which is

Re: [GENERAL] SQL-question: returning the id of an insert querry

2003-11-12 Thread Greg Stark
scott.marlowe [EMAIL PROTECTED] writes: select tablename.fieldname.currval; That syntax would be problematic, it would mean to select all rows from tablename and evaluate fieldname.currval for each one. Actually it's worse, it would be confused with schemas I think. The postgres-ish way to do

Re: [GENERAL] SQL-question: returning the id of an insert querry

2003-11-12 Thread Doug McNaught
Scott Chapman [EMAIL PROTECTED] writes: On Wednesday 12 November 2003 11:29, Doug McNaught wrote: Scott Chapman [EMAIL PROTECTED] writes: It would be nice if PostgreSQL could return the primary key it inserted with but that may not be a fool-proof solution either. Is there a nice way

Re: [GENERAL] SQL-question: returning the id of an insert querry

2003-11-12 Thread Doug McNaught
Scott Chapman [EMAIL PROTECTED] writes: It would be nice if PostgreSQL could return the primary key it inserted with but that may not be a fool-proof solution either. Is there a nice way to handle this situation? Write a database function that inserts the record and returns the primary key

Re: [GENERAL] SQL-question: returning the id of an insert querry

2003-11-10 Thread Alvaro Herrera
On Mon, Nov 10, 2003 at 08:09:29AM -0800, Scott Chapman wrote: Chronological events here: X inserts a new record into A. Y inserts a new record into A. X fetches currval of the SA. What value does X get in this case, the one from X's insert or Y's? X's. -- Alvaro Herrera

Re: [GENERAL] SQL-question: returning the id of an insert querry

2003-11-10 Thread Scott Chapman
On Monday 10 November 2003 08:23, David Green wrote: Are X Y two different connections? If you execute 2 statements on the same connection and then get currval() it will give the last generated id. Ex. On 1 connection: INSERT INTO A (fld) VALUES (val); -- id generated = 1 INSERT INTO A

Re: [GENERAL] SQL-question: returning the id of an insert querry

2003-11-10 Thread Kathy Zhu
I saw this method of Statement class in jdbc. Will the return int contain the autogenerated key value ?? public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException thanks, kathy Scott Chapman wrote: On Monday 10 November 2003

Re: [GENERAL] SQL-question: returning the id of an insert querry

2003-11-09 Thread Andreas Fromm
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Martijn van Oosterhout wrote: After you've done the insert on the address table, you can use currval('address_id_seq') (or equivalent) to get the ID. Ofcourse you have to have used nextval() for the original insert. Hope this helps, ..going to

Re: [GENERAL] SQL-question: returning the id of an insert querry

2003-11-09 Thread Scott Chapman
On Sunday 09 November 2003 03:13, Martijn van Oosterhout wrote: After you've done the insert on the address table, you can use currval('address_id_seq') (or equivalent) to get the ID. Ofcourse you have to have used nextval() for the original insert. What if someone else inserts another address

Re: [GENERAL] SQL-question: returning the id of an insert querry

2003-11-09 Thread Doug McNaught
Scott Chapman [EMAIL PROTECTED] writes: On Sunday 09 November 2003 03:13, Martijn van Oosterhout wrote: After you've done the insert on the address table, you can use currval('address_id_seq') (or equivalent) to get the ID. Ofcourse you have to have used nextval() for the original insert.

Re: [GENERAL] SQL-question: returning the id of an insert querry

2003-11-09 Thread Alvaro Herrera
On Sun, Nov 09, 2003 at 10:26:51AM -0800, Scott Chapman wrote: On Sunday 09 November 2003 03:13, Martijn van Oosterhout wrote: After you've done the insert on the address table, you can use currval('address_id_seq') (or equivalent) to get the ID. Ofcourse you have to have used nextval() for

Re: [GENERAL] SQL question - problem with INTERSECT

2000-11-02 Thread hubert depesz lubaczewski
On Thu, Nov 02, 2000 at 06:50:07AM -0500, Keith L. Musser wrote: upper(a.word) = 'JIM' or upper(a.word) = 'JONES' upper(s.word) = 'HELLO' or upper(s.word) = 'THERE' Interesting possibility. Unfortunately, the outer select ends up being a sequential scan over the entire messages table,

Re: [GENERAL] SQL question - problem with INTERSECT

2000-11-01 Thread Keith L. Musser
eally like to know is why INTERSECT does not allow this. If I understand that, maybe I can figure out how to get what I need. -Original Message- From: Igor Roboul [EMAIL PROTECTED] To: PGSQL-General [EMAIL PROTECTED] Date: Wednesday, November 01, 2000 12:03 AM Subject: Re: [GENERAL] SQL question

Re: [GENERAL] SQL question - problem with INTERSECT

2000-10-31 Thread Igor Roboul
On Mon, Oct 30, 2000 at 07:59:06PM -0500, Keith L. Musser wrote: "(SELECT messages.msgid FROM messages, subject_index WHERE ((subject_index.word='Hello' or subject_index.word='There') and (subject_index.msgid = messages.msgid)) GROUP BY messages.msgid HAVING count(messages.msgid)=2)

Re: [GENERAL] sql question

2000-07-13 Thread Travis Bauer
Thanks, Now that you mention it, I even remember reading it in the book! Travis Bauer | CS Grad Student | IU |www.cs.indiana.edu/~trbauer On Wed, 12 Jul 2000, Ross

Re: [GENERAL] sql question

2000-07-12 Thread Ross J. Reedstrom
On Wed, Jul 12, 2000 at 05:33:09PM -0500, Travis Bauer wrote: Let's say I have a table t1 with two fields, x and y. How do I write an sql statement like: select x if y1 else 0 from t1; SELECT CASE WHEN y1 THEN x ELSE 0 END FROM t1; From page 33 of Bruce's book, at:

RE: [GENERAL] SQL Question

1999-03-10 Thread Michael Davis
Yes, the "having" clause is supported in 6.4.2 with some restrictions as outlined in the documentation on the web site. -Original Message- From: Matthew [SMTP:[EMAIL PROTECTED]] Sent: Tuesday, March 09, 1999 10:46 PM To: [EMAIL PROTECTED]