Re: Comparing dates in DDL

2019-01-04 Thread Rich Shepard
On Fri, 4 Jan 2019, Andreas Kretschmer wrote:   Only if all projects have a known end_date; some don't. that's not a problem: test=*# create table projects(duration daterange default daterange(current_date,null) check(lower(duration) is not null)); Andreas, Thank you. That's a data type

Re: Comparing dates in DDL

2019-01-04 Thread Rich Shepard
On Fri, 4 Jan 2019, Rich Shepard wrote: Thank you. That's a data type I've not before used. Andreas, Thinking more about duration perhaps I'm seeing a problem that really does not exist: it's a single column for both dates in the table while the UI needs separate dat

Re: Comparing dates in DDL

2019-01-04 Thread Rich Shepard
On Fri, 4 Jan 2019, David G. Johnston wrote: That would be the decision to make - does your toolkit support (or can be made to support) the type and are you willing to choose a sub-optimal database model because one or more applications happen to do things differently? IMO the daterange datatyp

Re: Comparing dates in DDL

2019-01-04 Thread Rich Shepard
On Fri, 4 Jan 2019, Jeremy Finzel wrote: Another suggestion which hasn’t been mentioned is using ‘infinity’ as the end date. I like this because it IMO indicates that the record is clearly the current valid record more than null. Jeremy, I believe that infinity was mentioned in this thread.

insert into: NULL in date column

2019-01-11 Thread Rich Shepard
A table has this column definition: next_contact date DEFAULT '2020-11-06' CONSTRAINT valid_next_date CHECK (next_contact >= CURRENT_DATE), (and I don't know that it needs a default). In an input statement that column is left empty ('') when there's no defined date

Re: insert into: NULL in date column

2019-01-11 Thread Rich Shepard
On Fri, 11 Jan 2019, Rob Sargent wrote: psql:activities.sql:2: ERROR:  invalid input syntax for type date: "" LINE 2: ...reaction they''ve experienced environmental issues.','','');     ^ Miss-matched single quotes in activities.sql?

Re: insert into: NULL in date column

2019-01-11 Thread Rich Shepard
On Fri, 11 Jan 2019, Rob Sargent wrote: Something syntactically askew I think. Rob, I agree that's the problem. Why there's a problem is what I need to learn. Thanks, Rich

Re: insert into: NULL in date column

2019-01-11 Thread Rich Shepard
On Fri, 11 Jan 2019, Ken Tanzer wrote: I think the problem is actually that you're trying to represent your NULL dates with '', which PG doesn't like. Ken, That's certainly how I saw the error message. cat test.csv my_text,my_date,my_int 'Some Text','1/1/18',3 'More Text,,2 'Enough','',1

Re: insert into: NULL in date column

2019-01-12 Thread Rich Shepard
On Fri, 11 Jan 2019, David G. Johnston wrote: VALUES (1, null, 3) is valid, VALUES (1,,3) is not. David, Using null occurred to me when I saw that an empty space still failed. Thanks for clarifying and confirming. Best regards, Rich

Re: insert into: NULL in date column

2019-01-12 Thread Rich Shepard
On Fri, 11 Jan 2019, David G. Johnston wrote: The default does seem a bit arbitrary and pointless... David, That answers my question about it. Thanks again. Best regards, Rich

Re: insert into: NULL in date column

2019-01-12 Thread Rich Shepard
On Sat, 12 Jan 2019, Ricardo Martin Gomez wrote: Hi, one question. Do you put explicit "NULL" in the column value? Other option is. You have to delete the column "next_contact" in your INSERT clause. So, if the column has a default value, this value Will be inserted. Else Null value will be inse

Re: insert into: NULL in date column

2019-01-12 Thread Rich Shepard
On Sat, 12 Jan 2019, David G. Johnston wrote: Actually, you didn't ask about the check constraint, which is actually horribly broken since current_date is not an immutable function. David, I know that nulls cannot be validly used in comparisons which makes the check constraint FUBAR. Thank

Re: insert into: NULL in date column

2019-01-12 Thread Rich Shepard
On Sat, 12 Jan 2019, David G. Johnston wrote: NULL isn't the problem - a check constraint can resolve to unknown in which case it behaves the same as if it resolved as true (i.e., its basically a IS NOT FALSE test in the backend). This is actually a nice feature of check constraints since for n

Re: insert into: NULL in date column

2019-01-12 Thread Rich Shepard
On Sat, 12 Jan 2019, David G. Johnston wrote: Inserting new data into a table qualifies as "data change" in my mind... David, Then it's certainly good enough for me. ;-) Looking at the text file which the application will replace there are occasions when there's more than one contact on

Refining query statement

2019-01-15 Thread Rich Shepard
Working with my sales/client management system using psql I have a select statement to identify contacts to be made. This statement works: select (C.contact_id, C.lname, C.fname, C.direct_phone, O.org_name, A.next_contact) from Contacts as C, Organizations as O, Activities as A where C.org_id

Re: Refining query statement

2019-01-15 Thread Rich Shepard
On Tue, 15 Jan 2019, Adrian Klaver wrote: For the above I could see using a datepicker widget that allows for multidate select. The specifics would depend on the software you are using to write the UI. Adrian, I'm using wxPython4, and I will use a calendar with that. But, is there a way to

Re: Refining query statement

2019-01-15 Thread Rich Shepard
On Tue, 15 Jan 2019, Thomas Kellerer wrote: With regards to "cleaner": the first thing to do is to remove the parentheses around the column list. In Postgres "(a,b,c)" creates a single column with an anonymous record type (that contains three fields), rather than selecting three columns. In othe

Re: Refining query statement

2019-01-15 Thread Rich Shepard
On Tue, 15 Jan 2019, Ron wrote: And I've never liked this method (though I'm old and crotchety) Ron, I'm older but not crotchety (most days), and I'm taking my SQL knowledge beyone what I've used in the past. I would appreciate you're explaining why you don't like the explicit JOINs pre

Re: Refining query statement

2019-01-15 Thread Rich Shepard
On Tue, 15 Jan 2019, Adrian Klaver wrote: Use BETWEEN?: https://www.postgresql.org/docs/10/functions-comparison.html a BETWEEN x AND y between So: next_contact BETWEEN '01/01/2019'::date AND '01/15/2019'::date Or a range function: https://www.postgresql.org/docs/10/functions-range.html

Identifying comments

2019-01-17 Thread Rich Shepard
I know that "--" has been the single-line comment identifier since SQL was implemented by IBM on mainframes using Hollerith cards. Today, some RDBMSs also allow the C comment indentifier pair, /* ... */. The postgres10 manual when searched for 'comments' presents the double hyphens and, in Sectio

Re: Identifying comments [ANSWERED]

2019-01-17 Thread Rich Shepard
On Thu, 17 Jan 2019, Rich Shepard wrote: I want only to confirm that I can use /* ... */ for multiline comments in my DDL and DML scripts. Oops! Section 4.1.5 tells me that I can. Apologies to all, Rich

Re: Refining query statement

2019-01-17 Thread Rich Shepard
On Tue, 15 Jan 2019, Thomas Kellerer wrote: select distinct on (C.contact_id) C.contact_id, C.lname, C.fname, C.direct_phone, O.org_name, A.next_contact from Contacts as C join Organizations as O on C.org_id = O.org_id join Activities as A on C.contact_id = A.contact_id where

Re: Refining query statement

2019-01-17 Thread Rich Shepard
On Thu, 17 Jan 2019, David G. Johnston wrote: Why is next_contact allowed to be null? David, There are a number of reasons. The prospect might have retired, told me to pound sand, or has put off a decision. Your concept of "most current row" is strictly based upon next_contact so if next_co

Re: Refining query statement

2019-01-17 Thread Rich Shepard
On Thu, 17 Jan 2019, Adrian Klaver wrote: Got to thinking more and realized the answer depends on what you want the query to produce. Can you let us know what is you are trying to pull out with the query? Adrian, Certainly. Over breakfast I realized the same thing: the existing SELECT query i

Re: Refining query statement

2019-01-17 Thread Rich Shepard
On Thu, 17 Jan 2019, David G. Johnston wrote: Yes...though now it just sounds like a flawed data model. David, This is what I thought. How stuck are you in that regard? Those "future" contacts should have their own records and not be derived via an optional field on an existing record.

Re: Refining query statement

2019-01-17 Thread Rich Shepard
On Thu, 17 Jan 2019, Adrian Klaver wrote: To be clear the next-activity date = next_contact in the database, correct? Adrian, Yes. I've renamed the Activities table to Contacts and the Contacts table to People. NULL basically means unknown, so having it stand for something is a bit of a s

Re: Refining query statement

2019-01-17 Thread Rich Shepard
On Thu, 17 Jan 2019, Adrian Klaver wrote: Seems to me a boolean field of name active to denote contacts you need to keep up with is in order. Then make the next_contact field NOT NULL and replace the current NULL values with 'infinity': WHERE COALESCE(next_contact, 'infinity') BETWEEN '01/01/

Re: Refining query statement

2019-01-17 Thread Rich Shepard
On Thu, 17 Jan 2019, David G. Johnston wrote: Off the top of my head (and this is a model I am quite familiar with even if I'm doing this email at speed): I'd suggest an actual activity table: David, Adrian's suggestion of a Contacts table column called 'active' having a boolean data type

Re: Refining query statement

2019-01-17 Thread Rich Shepard
On Thu, 17 Jan 2019, Adrian Klaver wrote: It would produce results, so yes it would work. The question are they the results you want? Adrian, To which the answer is no as I just discovered. The above would return anything with a next_contact less then today. That could extend backwards to

Re: Refining query statement

2019-01-17 Thread Rich Shepard
On Thu, 17 Jan 2019, Adrian Klaver wrote: Got to thinking more and realized the answer depends on what you want the query to produce. Can you let us know what is you are trying to pull out with the query? Adrian, et al., Took your advice and re-thought what I need the query to return. This al

Re: Refining query statement

2019-01-17 Thread Rich Shepard
On Thu, 17 Jan 2019, Adrian Klaver wrote: I would think the active would be on People or Organizations. Then you can eliminate then from the query results before you ever got to the contact history. Adrian, Excellent point. I don't know why I put active in the contacts table as it does make m

Re: Refining query statement

2019-01-17 Thread Rich Shepard
On Thu, 17 Jan 2019, David G. Johnston wrote: I would advise changing Contacts to "Activities" as the former can readily be interpreted (and is in the wild) as both "an instance of contacting a person" and "the person at the organization who is being contacted" (i.e., your People class). David

Re: Refining query statement [DONE]

2019-01-18 Thread Rich Shepard
On Tue, 15 Jan 2019, Rich Shepard wrote: Working with my sales/client management system using psql I have a select statement to identify contacts to be made. This statement works: With much patient advice from Adrian, David, Thomas, and Ron I fixed the schema and the query statement. To close

Blank, nullable date column rejected by psql

2019-02-11 Thread Rich Shepard
Running postgresql-10.5 on Slackware-14.2. A table has a column defined as Column | Type | Collation | Nullable | Default next_contact | date | | | In a .sql file to insert rows in this table psql has a problem when there's no value for the next_contact column: $ psq

Re: Blank, nullable date column rejected by psql

2019-02-11 Thread Rich Shepard
On Mon, 11 Feb 2019, Tom Lane wrote: An empty string is not a null. Tom, I understand this yet thought that empty strings and numeric fields were accepted. Guess I need to review this. Thanks, Rich

Re: Blank, nullable date column rejected by psql

2019-02-11 Thread Rich Shepard
On Mon, 11 Feb 2019, Ron wrote: NULL is nothing. Blank isn't nothing; blank is a zero-length string.  Thus, you need to tell Pg "nothing", not "blank string". Ron, All of you who responded drove home my need to explicitly enter null when there are no data for a column. Thanks, Rich

Re: Blank, nullable date column rejected by psql

2019-02-11 Thread Rich Shepard
On Mon, 11 Feb 2019, Rich Shepard wrote: All of you who responded drove home my need to explicitly enter null when there are no data for a column. Correction: when there are no date data for a column. Rich

Re: Blank, nullable date column rejected by psql

2019-02-11 Thread Rich Shepard
On Mon, 11 Feb 2019, Ron wrote: You've got ADOS (All Databases are Oracle Syndrome). Interesting as I've never bought, used, or seen anything from Oracle. Guess it's transmitted by errent bits. Regards, Rich

Re: Blank, nullable date column rejected by psql

2019-02-11 Thread Rich Shepard
On Mon, 11 Feb 2019, Ken Tanzer wrote: Just in case you miss this little nuance, you don't necessarily _have_ to specify a NULL for that column, depending how you're doing your inserts. You haven't show us your table or what INSERT you're using, but all of these examples will work, and don't spe

Re: Blank, nullable date column rejected by psql

2019-02-12 Thread Rich Shepard
On Tue, 12 Feb 2019, Laurenz Albe wrote: If you insert a string into a "date" column, PostgreSQL will try to convert the string to a date with the type input function. The type input function fails on an empty string, since it cannot parse it into a valid "date" value. This also applies to the e

Subquery to select max(date) value

2019-02-12 Thread Rich Shepard
The query is to return the latest next_contact date for each person. Using the max() aggregate function and modeling the example of lo_temp on page 13 of the rel. 10 manual I wrote this statement: select (P.person_id, P.lname, P.fname, P.direct_phone, O.org_name, A.next_contact) from People as P

Re: Subquery to select max(date) value [RESOLVED]

2019-02-12 Thread Rich Shepard
On Tue, 12 Feb 2019, David G. Johnston wrote: You put the open parenthesis after the word select instead of before. A.next_contact = (SELECT max(A.next_contact) FROM A) David. Color me suitably embarrassed. Thank you, Rich

Re: Subquery to select max(date) value

2019-02-12 Thread Rich Shepard
On Tue, 12 Feb 2019, Jeff Ross wrote: Try (select (max(A.next_contact) from A) Thanks, Jeff. The syntax accepted by psql is A.next_contact = (select (max(A.next_contact)) from Activities as A) but the date is not included in the output. The revised statement is now: select (P.person_id, P.ln

Re: Subquery to select max(date) value

2019-02-12 Thread Rich Shepard
On Tue, 12 Feb 2019, Rich Shepard wrote: A.next_contact = (select (max(A.next_contact)) from Activities as A) Errata: The parentheses around the max aggregate are not necessary. A.next_contact now displays at the end of each returned row as 'infinity'. Rich

Re: Subquery to select max(date) value

2019-02-12 Thread Rich Shepard
On Tue, 12 Feb 2019, Adrian Klaver wrote: 'infinity' is the max date, so this is what you want? Adrian, Nope. When I went to make a cup of coffee I realized that I need the other date constraints (such as IS NOT NULL), too. I'm re-wording the statement to put everything in the correct order.

Re: Subquery to select max(date) value

2019-02-12 Thread Rich Shepard
On Tue, 12 Feb 2019, Ken Tanzer wrote: select (P.person_id, P.lname, P.fname, P.direct_phone, O.org_name, (select max(A.next_contact) from Activities as A WHERE p.person_id=A.person_id) FROM ... Ken, Yes, cheers indeed. A bit of thinking and re-organizing resulted in a working statement that

Re: Subquery to select max(date) value

2019-02-12 Thread Rich Shepard
On Tue, 12 Feb 2019, Ken Tanzer wrote: If that's getting you what you want, then great and more power to you. It looks like you'll only get people who have a next_contact in your target window there. You might also consider something like this... Ken, I'll work with your example. This loo

Re: Subquery to select max(date) value

2019-02-13 Thread Rich Shepard
On Wed, 13 Feb 2019, Andrew Gierth wrote: Rich> Will probably try DISTINCT ON, too, if that makes it simpler or Rich> faster. You want LATERAL. Andrew, That's new to me so I'll read about it. Thanks, Rich

Re: Subquery to select max(date) value

2019-02-13 Thread Rich Shepard
On Tue, 12 Feb 2019, Ken Tanzer wrote: If that's getting you what you want, then great and more power to you. It looks like you'll only get people who have a next_contact in your target window there. You might also consider something like this... select p.person_id, p.lname, p.fname,

Re: Subquery to select max(date) value

2019-02-13 Thread Rich Shepard
On Wed, 13 Feb 2019, Adrian Klaver wrote: The LEFT JOIN. There are rows in people for which there no records coming from the sub-select on activities, so the row is 'padded' with NULL values for the missing data. Adrian, I assume it's the inner left join. I'll trace what's happening at each s

Re: Subquery to select max(date) value

2019-02-13 Thread Rich Shepard
On Wed, 13 Feb 2019, Adrian Klaver wrote: AFAIK there is no inner left join: https://www.postgresql.org/docs/10/sql-select.html#SQL-FROM Sigh. There are two LEFT JOINS in the statement. I referred to the SECOND one as INNER. I correct my message to refer to the second of the two left joins. R

Re: Subquery to select max(date) value

2019-02-13 Thread Rich Shepard
On Wed, 13 Feb 2019, Adrian Klaver wrote: Should have been clearer in my previous post, you can get rid of the nulls by filtering out the entire row. Adrian, Thank you. I'm rebuilding the statement from the inside out (which helps me learn more SQL in the process). For example, select * from

Re: Subquery to select max(date) value

2019-02-13 Thread Rich Shepard
On Wed, 13 Feb 2019, Adrian Klaver wrote: It will work if you use it to filter after the joins are done. I'll work on finding the proper syntax to do this. Need to do more reading and trial-and-error testing. Regards, Rich

Re: Subquery to select max(date) value

2019-02-13 Thread Rich Shepard
On Wed, 13 Feb 2019, Andrew Gierth wrote: Adrian> Close to your last posted query. person_id 2 and 3 have NULL Adrian> values for activities data as there is no record for 2 and 3 is Adrian> out of the date range.: DISTINCT ON with no matching ORDER BY at the _same_ query level is non-determi

Re: Subquery to select max(date) value

2019-02-13 Thread Rich Shepard
On Wed, 13 Feb 2019, Adrian Klaver wrote: Given a sufficiently large date range that may not be true as you may have contacted a given person multiple times during that range and generated multiple activities records. Adrian, This is true as it has occurred. I want only the most recent activi

Re: Subquery to select max(date) value

2019-02-13 Thread Rich Shepard
On Wed, 13 Feb 2019, Brent Wood wrote: I have not really followed this thread but would not a query along the lines of select * from activity where person_id = n and timestamp = (select max(timestamp) from activity where person_id = n); give the required answer ie, always return the latest resul

Re: Subquery to select max(date) value

2019-02-15 Thread Rich Shepard
On Wed, 13 Feb 2019, Andrew Gierth wrote: You want LATERAL. Andrew, et al,: I found a couple of web pages describing the lateral join yet have not correctly applied them. The manual's page did not help me get the correct syntax, either. Think I'm close, however: select p.person_id, p.lname,

Re: Subquery to select max(date) value

2019-02-15 Thread Rich Shepard
On Fri, 15 Feb 2019, Andrew Gierth wrote: LATERAL (SELECT ...) is syntactically like (SELECT ...) in that it comes _after_ a "," in the from-clause or after a [LEFT] JOIN keyword. Don't think of LATERAL as being a type of join, think of it as qualifying the (SELECT ...) that follows. Andrew,

Re: Subquery to select max(date) value

2019-02-15 Thread Rich Shepard
On Fri, 15 Feb 2019, Andrew Gierth wrote: LATERAL (SELECT ...) is syntactically like (SELECT ...) in that it comes _after_ a "," in the from-clause or after a [LEFT] JOIN keyword. Andrew, Yes, the missing ',' made a big difference. You'd want a condition here that references the "people"

Re: Subquery to select max(date) value

2019-02-15 Thread Rich Shepard
On Fri, 15 Feb 2019, Andrew Gierth wrote: Rich> I've not before run 'explain' on a query. Would that be Rich> appropriate here? Yes. Andrew, I'll learn how to use it. The problem here is that you have no join conditions at all, so the result set of this query is massive. And you've duplica

Re: Subquery to select max(date) value

2019-03-28 Thread Rich Shepard
On Fri, 15 Feb 2019, Andrew Gierth wrote: select p.person_id, p.lname, p.fname, p.direct_phone, o.org_name, sq.* from people as p join organizations as o on p.organization_id=o.id -- OR WHATEVER cross join lateral (select a.next_contact from activities a

Re: Subquery to select max(date) value

2019-03-28 Thread Rich Shepard
On Thu, 28 Mar 2019, Ken Tanzer wrote: You need the ORDER BY in the outer join. Ken, I thought so. But, ... (And I don't think the one inside the lateral join is doing you any good). Try: ... a.next_contact is not null limit 1) sq order by sq.next_contact

Re: Subquery to select max(date) value

2019-03-28 Thread Rich Shepard
On Thu, 28 Mar 2019, David G. Johnston wrote: Yes, if you join the result on an ordered subquery to anything you no longer have a guaranteed order for the combined relation. David, This makes sense to me. select ... from ... join ... cross join lateral ... -- now add an order by for the top

Re: Subquery to select max(date) value

2019-03-29 Thread Rich Shepard
On Thu, 28 Mar 2019, David G. Johnston wrote: It returns something at least. If you put the output you get into a spreadsheet are you able to manually sort it the way you desire? David, Probably, but that's not practical for the application. Thanks, Rich

Re: Subquery to select max(date) value

2019-03-29 Thread Rich Shepard
On Thu, 28 Mar 2019, Ken Tanzer wrote: Really? Is your next_contact field a date field and not a text field? What order does it come out in? (And I assume you saw David J's correction to the misinformation I offered.) Ken, Mea culpa. the next_contact column is a date, I did apply David's cor

Re: Subquery to select max(date) value [RESOLVED]

2019-03-29 Thread Rich Shepard
On Fri, 29 Mar 2019, Andrew Gierth wrote: That query seems correct assuming you want the result in descending order of next_contact. How did the actual result differ from your expectation? Andrew, User error: I had a couple of date typos (2018 rather than 2019) and was thinking of generating

Running psql in emacs shell generates key conflict error: why?

2019-04-02 Thread Rich Shepard
An unexpected result this morning that I'd like to understand. In emacs I loaded the file organizations.sql in a buffer to insert new rows in that table. Rather than running psql from a virtual console, I opened an emacs shell and entered the command, psql -f organizations.sql -d bustrac and saw

Re: Running psql in emacs shell generates key conflict error: why? [FIXED]

2019-04-02 Thread Rich Shepard
On Tue, 2 Apr 2019, Adrian Klaver wrote: What was the exact message? What was in the last line? Difference in shell escaping? Adrian, Cannot answer the first two because that work is long gone. So, I just tried it again with only one row to insert and psql inserted that row without complaint.

How serial primary key numbers are assigned

2019-04-04 Thread Rich Shepard
Just out of curiosity, how does postgres assign serial primary key identifiers when rows are inserted in multiple, separate working sessions? I'm populating tables using INSERT INTO statements in separate working sessions and see that the assigned id numbers are sequential in each session, but ha

Re: How serial primary key numbers are assigned

2019-04-04 Thread Rich Shepard
On Thu, 4 Apr 2019, Adrian Klaver wrote: See: https://www.postgresql.org/docs/11/sql-createsequence.html Thanks, Adrian. My web searches did not find this URL, only results on how to set up automatic serial id generation. Regards, Rich

Primary key data type: integer vs identity

2019-04-19 Thread Rich Shepard
When I created the database I set primary key data types as integer; for example: Column| Type | Collation | Nullable | Default --+---+---+--+- org_id | integer | | not null | nextval('organizations_o

Re: Primary key data type: integer vs identity

2019-04-19 Thread Rich Shepard
On Fri, 19 Apr 2019, Michel Pelletier wrote: https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-SERIAL "The data types smallserial, serial and bigserial are not true types, but merely a notational convenience for creating unique identifier columns" so really there's no differ

Re: Primary key data type: integer vs identity

2019-04-19 Thread Rich Shepard
On Fri, 19 Apr 2019, Adrian Klaver wrote: If it is working for you now I see no reason to switch. Adrian, It does work. I just learned about the SQL identity type and want to learn when it's most appropriate to use. The databases I develop all work with integers as primary keys and reading ab

Re: Primary key data type: integer vs identity

2019-04-19 Thread Rich Shepard
On Fri, 19 Apr 2019, Adrian Klaver wrote: Mainly for folks that want cross database SQL compliance. It is not a type so much as a way of specifying an auto-increment column. Thank you, Adrian. I saw that it was a column specification and your explanation of its application is really helpful.

Re: Primary key data type: integer vs identity

2019-04-23 Thread Rich Shepard
On Tue, 23 Apr 2019, Peter Devoy wrote: I think you may have serial already there (indicated by sequence in the the default value). Peter, As serial is an alias for integer (the actual data type) using integer and specifying it as the primary key works. If you wish to change to identity col

Table update: restore or replace?

2019-05-14 Thread Rich Shepard
I just updated a column in a table (postgresql-10.5) and accidently applied it to all 185 rows rather than just one. There is no active transaction to roll back. Each night I do a backup and just started working on the database a few minutes ago. If I restore all contents of /var/lib/pgsql/10/dat

Re: Table update: restore or replace?

2019-05-14 Thread Rich Shepard
On Tue, 14 May 2019, Adrian Klaver wrote: A file level backup or database dump? Adrian, File level. April 20th is my most recent database dump because I forgot to run it last Friday afternoon. Rich

Re: Table update: restore or replace?

2019-05-14 Thread Rich Shepard
On Tue, 14 May 2019, Adrian Klaver wrote: Does the table you overwrote the data change much? Adrian, Yes. It's in my business tracking database so it's updated almost every day. If not it might be safer to just fetch it from the April 20th dump and then apply the changes since then. The c

Re: Table update: restore or replace?

2019-05-14 Thread Rich Shepard
On Tue, 14 May 2019, Tom Lane wrote: Yeah. You can't just selectively copy files, because the data files are dependent on the contents of the pg_xact transaction log; it's all or nothing. Tom, That's why I thought of copying the entire data/ directory. Also, I don't know what method you've

Re: Re: Table update: restore or replace?

2019-05-14 Thread Rich Shepard
On Tue, 14 May 2019, Brad Nicholson wrote: Might I suggest you setup proper backups with continuous archiving instead? If you had those, you would be able to restore this database back to the point right before you issued the bad update statement. I'd highly recommend pgBackRest for the task.

Re: Table update: restore or replace?

2019-05-14 Thread Rich Shepard
On Tue, 14 May 2019, Adrian Klaver wrote: Or just fetch them from the table data you have saved in the dump file. Adrian, Sigh. I should have thought of that. A great time saver. Thanks, Rich

Re: Table update: restore or replace? [RESOLVED]

2019-05-14 Thread Rich Shepard
On Tue, 14 May 2019, Adrian Klaver wrote: Or just fetch them from the table data you have saved in the dump file. Adrian, This did save a lot of time! Thanks for the pointer. And I think I'll set up a cron job to do a database dump each day with the date appended to the file name in the bas

Bulk inserts into two (related) tables

2019-05-21 Thread Rich Shepard
I'm cleaning and formatting a 800-line data file to be inserted into a database. Some of the input file fields will be inserted into an 'organizations' table letting postgres assign sequential org_id numbers. Other fields will be inserted into a separate 'people' table associated with each organiz

Re: Bulk inserts into two (related) tables

2019-05-21 Thread Rich Shepard
On Tue, 21 May 2019, Adrian Klaver wrote: Well you are not going to know the org_id until the organization table is loaded, which means something like: 1) First run through file load the organizations table. 2) Build a mapping of org_id to organization. 3) Run through data file again and loa

Re: Bulk inserts into two (related) tables

2019-05-21 Thread Rich Shepard
On Tue, 21 May 2019, Francisco Olarte wrote: From how you say it, I assume you have some data in your original dumps which can relate boths, lets assume it's org_name, but may be an org-code. If you do not have it it means you cannot match people to orgs in your data, all is lost. Francisco,

Re: Bulk inserts into two (related) tables

2019-05-21 Thread Rich Shepard
On Tue, 21 May 2019, Michael Lewis wrote: For each row- Insert into organizations table if the record does not exist, returning ID. Insert into people using that ID. Michael, The org_id will not exist until I run the insert script. Else, load all the data with empty ID column on person tabl

Re: Bulk inserts into two (related) tables

2019-05-21 Thread Rich Shepard
On Tue, 21 May 2019, Adrian Klaver wrote: The other way is to create the org_id for each organization ahead of time and put it into the data file. Either way you have create the org_id for the FK relationship, it is just a matter of where and when. Adrian, I had thought of that but overlooked

Re: Bulk inserts into two (related) tables

2019-05-21 Thread Rich Shepard
On Tue, 21 May 2019, Will Hartung wrote: You can either use a script for the entire process, or, convert the people table to INSERT statements that have a SELECT for the foreign key as part of the insert. INSERT INTO PEOPLE (id, name, org_id) VALUES (1, ‘Bob’, (SELECT org_id FROM org WHERE org_

Re: Bulk inserts into two (related) tables

2019-05-22 Thread Rich Shepard
On Wed, 22 May 2019, Francisco Olarte wrote: I'm curious, what org_id do you put (manually) to the people? you must have some way to match it ( like, say, "I have an organization line, followed by lines for people in this organization" Francisco, The first data entered was in small chunks so

Re: Bulk inserts into two (related) tables

2019-05-22 Thread Rich Shepard
On Wed, 22 May 2019, Jeremy Finzel wrote: There's absolutely no need to use anything beyond SQL here, though you could if you want to. Jeremy, This is a new experience for me so I didn't think of a SQL solution. I really wonder how much we are just talking past each other simply because we

Re: Bulk inserts into two (related) tables

2019-05-22 Thread Rich Shepard
On Wed, 22 May 2019, Adrian Klaver wrote: So does the people data have an organization attribute? Adrian, Yes. It's the FK to the organization table. If so why not just assign the org_id while cleaning up the data? That's what I thought to do based on your suggestion yesterday. It would m

Re: Bulk inserts into two (related) tables

2019-05-22 Thread Rich Shepard
On Wed, 22 May 2019, Francisco Olarte wrote: You are not reading what we write to you. Note YOU AND ONLY YOU are the one speaking of PK. We are speaking of "unique identifier" ( that would be, IIRC, "candidate keys", you can peek any as your PK, or even introduce a new synthetic one with a seque

Re: Bulk inserts into two (related) tables

2019-05-22 Thread Rich Shepard
On Wed, 22 May 2019, Francisco Olarte wrote: Also, when I speak of "unique identifier" I'm not speaking of the one if your FINAL tables, I assume you would have at least the *_id field as PKEY, so nothing else needed, but the one in your SOURCE data set (it can be anything, like the row number i

Re: Bulk inserts into two (related) tables

2019-05-22 Thread Rich Shepard
On Wed, 22 May 2019, Jeremy Finzel wrote: Are you saying your database already has an organizations table, and this data file is appending to it with all-brand-new organizations and people? Jeremy, The database has both organizations and people tables (among others) which are already populate

Re: Bulk inserts into two (related) tables

2019-05-22 Thread Rich Shepard
On Wed, 22 May 2019, Adrian Klaver wrote: A sample of the data you are cleaning up. Adrian, et al.: I have it working properly now. Both org_id and person_id numbers are prepended to each row in the appropriate table and they are unique because each series begins one greater than the max(*_id

Re: Data entry / data editing tools (more end-user focus).

2019-05-23 Thread Rich Shepard
On Thu, 23 May 2019, Frank Alberto Rodriguez wrote: If you have the database modeled, the most quickly think I can thinks is with python framework Django. Configure the connection to the DB and make reverse engineer with Django, this create the entities class, then activate the administration fo

Re: Converting yes or no to one letter strings.

2019-06-04 Thread Rich Shepard
On Tue, 4 Jun 2019, Lou wrote: To start off, I just need to convert true to 's'. false will have to be manually changed to 'f' or 'p' or 'e' as appropriate. Lou, I'm far from an expert so take my comments with a bag of salt. First, 'false' is displayed in a column as 'f' when you look at a ta

Re: Converting yes or no to one letter strings.

2019-06-04 Thread Rich Shepard
On Tue, 4 Jun 2019, David G. Johnston wrote: Actually, given that "f" is becoming multiple different values it seems like the OP is improving upon their data model. That should very much be done at the table level at not relegated to views, let alone a UI layer. It is not unusual to want to con

Re: Need a DB layout gui

2019-06-24 Thread Rich Shepard
On Mon, 24 Jun 2019, David Gauthier wrote: I've been poking around https://wiki.postgresql.org/wiki/Community_Guide_to_PostgreSQL_GUI_Tools to see if there is anything that can read PG metadata in and display it graphically in a gui. You know, the kind of picture that is 1 block per table with 1

Left join syntax error

2024-05-18 Thread Rich Shepard
It's been a _very_ long time since I wrote a SQL script and, despite looking at my SQL books and web pages, I don't know how to fix the error. The three line script is: - SELECT p.lname, p.fname, p.job_title, p.company_nbr, p.email, c.company_name FROM people as p, companies as c LEFT JOIN

<    1   2   3   4   5   6   >