pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Find duplicates in a column then print Information to a file
6+ messages / 5 participants
[nested] [flat]

* Find duplicates in a column then print Information to a file
@ 2017-12-31 18:19 Sherman Willden <operasopranos@gmail.com>
  2017-12-31 18:23 ` Re: Find duplicates in a column then print Information to a file Martin Moore <martin.moore@avbrief.com>
  2017-12-31 18:53 ` Re: Find duplicates in a column then print Information to a file Francisco Olarte <folarte@peoplecall.com>
  0 siblings, 2 replies; 6+ messages in thread

From: Sherman Willden @ 2017-12-31 18:19 UTC (permalink / raw)
  To: pgsql-general

Development Platform: Ubuntu 17.10 mainly command line work

Tools: perl 5.26 and postgresql 9.6

Goal: Display duplicate aria titles on screen and to a local file

Database name: arias

Table name: aria_precis

csv delimiter: the # symbol

arias=# \d aria_precis
   Table "public.aria_precis"
   Column    | Type | Modifiers
-------------+------+-----------
 id          | text | not null
 aria        | text |
 artist      | text |
 a_artist    | text |
 album_title | text |
Indexes:
    "aria_precis_pkey" PRIMARY KEY, btree (id)

Problems:
  1. Can't connect aria title to id
  2. Can't write discovered information to file

I know about this link but I probably didn't understand what I read there.
https://www.postgresql.org/docs/current/static/sql-select.html#SQL-WITH

What I know displays the 46 duplicate aria titles of 413 entries but not
the id or artist:
SELECT aria FROM aria_precis WHERE aria IN (SELECT aria FROM aria_precis
GROUP BY aria  HAVING COUNT(aria)>1);

When I perform the following I get (0 rows):
SELECT aria FROM aria_precis WHERE aria IN (SELECT aria FROM aria_precis
GROUP BY id, aria  HAVING COUNT(aria)>1);
 aria
------
(0 rows)

After I get the above information how do I write that information to a
file? The only thing I know writes the entire database to a file:
 \COPY aria_precis TO '/home/sherman/aria_precis_2.csv' WITH DELIMITER '#,'
CSV HEADER;

Thank you;

Sherman

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Find duplicates in a column then print Information to a file
  2017-12-31 18:19 Find duplicates in a column then print Information to a file Sherman Willden <operasopranos@gmail.com>
@ 2017-12-31 18:23 ` Martin Moore <martin.moore@avbrief.com>
  1 sibling, 0 replies; 6+ messages in thread

From: Martin Moore @ 2017-12-31 18:23 UTC (permalink / raw)
  To: Sherman Willden <operasopranos@gmail.com>; pgsql-general

 

From: Sherman Willden <operasopranos@gmail.com>
Date: Sunday, 31 December 2017 at 18:19
To: <pgsql-general@postgresql.org>
Subject: Find duplicates in a column then print Information to a file

 

Development Platform: Ubuntu 17.10 mainly command line work

Tools: perl 5.26 and postgresql 9.6

Goal: Display duplicate aria titles on screen and to a local file

Database name: arias

Table name: aria_precis

csv delimiter: the # symbol

arias=# \d aria_precis
   Table "public.aria_precis"
   Column    | Type | Modifiers
-------------+------+-----------
 id          | text | not null
 aria        | text |
 artist      | text |
 a_artist    | text |
 album_title | text |
Indexes:
    "aria_precis_pkey" PRIMARY KEY, btree (id)

Problems:
  1. Can't connect aria title to id
  2. Can't write discovered information to file

I know about this link but I probably didn't understand what I read there.
https://www.postgresql.org/docs/current/static/sql-select.html#SQL-WITH

What I know displays the 46 duplicate aria titles of 413 entries but not the id or artist:
SELECT aria FROM aria_precis WHERE aria IN (SELECT aria FROM aria_precis GROUP BY aria  HAVING COUNT(aria)>1);

When I perform the following I get (0 rows):
SELECT aria FROM aria_precis WHERE aria IN (SELECT aria FROM aria_precis GROUP BY id, aria  HAVING COUNT(aria)>1);
 aria
------
(0 rows)

After I get the above information how do I write that information to a file? The only thing I know writes the entire database to a file:
 \COPY aria_precis TO '/home/sherman/aria_precis_2.csv' WITH DELIMITER '#,' CSV HEADER;

Thank you;

Sherman

========================================

 

 

On the cmdline just do:

 

psql arias <pwd> -c ‘SELECT aria FROM aria_precis WHERE aria IN (SELECT aria FROM aria_precis GROUP BY aria  HAVING COUNT(aria)>1)‘   > outfile.txt

 

 

Martin.

 

 

 

 

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Find duplicates in a column then print Information to a file
  2017-12-31 18:19 Find duplicates in a column then print Information to a file Sherman Willden <operasopranos@gmail.com>
@ 2017-12-31 18:53 ` Francisco Olarte <folarte@peoplecall.com>
  2017-12-31 19:04   ` Re: Find duplicates in a column then print Information to a file Sherman Willden <operasopranos@gmail.com>
  1 sibling, 1 reply; 6+ messages in thread

From: Francisco Olarte @ 2017-12-31 18:53 UTC (permalink / raw)
  To: Sherman Willden <operasopranos@gmail.com>; +Cc: pgsql-general

Mmmmm, I notice a faint homework smell here ;-> , but the question is
nicely asked so:

On Sun, Dec 31, 2017 at 7:19 PM, Sherman Willden
<operasopranos@gmail.com> wrote:
...
> SELECT aria FROM aria_precis WHERE aria IN (SELECT aria FROM aria_precis
> GROUP BY aria  HAVING COUNT(aria)>1);

The outer select is fully redundant. Just use the inner one. In fact,
not redundnat, harmful, as it will spit many copies of aria for the
duplicated ones. If you add id (and/or artist) to the outer query then
it is useful again.


> When I perform the following I get (0 rows):
> SELECT aria FROM aria_precis WHERE aria IN (SELECT aria FROM aria_precis
> GROUP BY id, aria  HAVING COUNT(aria)>1);

As expected. See the inner query, you are grouping by ID which is the
primary key, by PK definition all counts are going to be one, so no
results, nothing goes to the outer query.

Use the first query, but adding id and artist to the OUTER level.

> After I get the above information how do I write that information to a file?
> The only thing I know writes the entire database to a file:
>  \COPY aria_precis TO '/home/sherman/aria_precis_2.csv' WITH DELIMITER '#,'
> CSV HEADER;

If copy format is ok to you, knowing \copy look at the docs, looking
at https://www.postgresql.org/docs/9.6/static/app-psql.html you will
read:

>>>
\copy { table [ ( column_list ) ] | ( query ) } { from | to } {
'filename' | program 'command' | stdin | stdout | pstdin | pstdout } [
[ with ] ( option [, ...] ) ]

Performs a frontend (client) copy. This is an operation that runs an
SQL COPY command, but instead of the server reading or writing the
specified file, psql reads or writes the file and routes the data
between the server and the local file system. This means that file
accessibility and privileges are those of the local user, not the
server, and no SQL superuser privileges are required.
<<<

Notice the (query) option? Your copy is using the table+optional
column list format, (aria_precis), just change it to the "( query )"
format ( NOTICE THE MANDATORY PARENS, this is how copy knows what to
do. "t" => table, "t(c1)"=>table+columns, "(xx)" => query ( nothing
before the opening parens ). )

Happy new year.

Francisco Olarte.




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Find duplicates in a column then print Information to a file
  2017-12-31 18:19 Find duplicates in a column then print Information to a file Sherman Willden <operasopranos@gmail.com>
  2017-12-31 18:53 ` Re: Find duplicates in a column then print Information to a file Francisco Olarte <folarte@peoplecall.com>
@ 2017-12-31 19:04   ` Sherman Willden <operasopranos@gmail.com>
  2017-12-31 19:23     ` Re: Find duplicates in a column then print Information to a file Rob Sargent <robjsargent@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Sherman Willden @ 2017-12-31 19:04 UTC (permalink / raw)
  To: Francisco Olarte <folarte@peoplecall.com>; martin.moore@avbrief.com; pgsql-general

Thank you for the replies. I will start working on them now. Not a student
but since I now have the time I may look into it. I am 71 retired working
at Home Depot. I have a collection of CDs by various artists and I have the
time to create and maintain my own database concerning these subjects. I
retired from USAF, SUN Microsystems, and HP where I worked with the UNIX
systems. As I said my main question right now is which duplicate songs do I
have and who performs them. I really appreciate the support I get here.

Sherman

On Sun, Dec 31, 2017 at 11:53 AM, Francisco Olarte <folarte@peoplecall.com>
wrote:

> Mmmmm, I notice a faint homework smell here ;-> , but the question is
> nicely asked so:
>
> On Sun, Dec 31, 2017 at 7:19 PM, Sherman Willden
> <operasopranos@gmail.com> wrote:
> ...
> > SELECT aria FROM aria_precis WHERE aria IN (SELECT aria FROM aria_precis
> > GROUP BY aria  HAVING COUNT(aria)>1);
>
> The outer select is fully redundant. Just use the inner one. In fact,
> not redundnat, harmful, as it will spit many copies of aria for the
> duplicated ones. If you add id (and/or artist) to the outer query then
> it is useful again.
>
>
> > When I perform the following I get (0 rows):
> > SELECT aria FROM aria_precis WHERE aria IN (SELECT aria FROM aria_precis
> > GROUP BY id, aria  HAVING COUNT(aria)>1);
>
> As expected. See the inner query, you are grouping by ID which is the
> primary key, by PK definition all counts are going to be one, so no
> results, nothing goes to the outer query.
>
> Use the first query, but adding id and artist to the OUTER level.
>
> > After I get the above information how do I write that information to a
> file?
> > The only thing I know writes the entire database to a file:
> >  \COPY aria_precis TO '/home/sherman/aria_precis_2.csv' WITH DELIMITER
> '#,'
> > CSV HEADER;
>
> If copy format is ok to you, knowing \copy look at the docs, looking
> at https://www.postgresql.org/docs/9.6/static/app-psql.html you will
> read:
>
> >>>
> \copy { table [ ( column_list ) ] | ( query ) } { from | to } {
> 'filename' | program 'command' | stdin | stdout | pstdin | pstdout } [
> [ with ] ( option [, ...] ) ]
>
> Performs a frontend (client) copy. This is an operation that runs an
> SQL COPY command, but instead of the server reading or writing the
> specified file, psql reads or writes the file and routes the data
> between the server and the local file system. This means that file
> accessibility and privileges are those of the local user, not the
> server, and no SQL superuser privileges are required.
> <<<
>
> Notice the (query) option? Your copy is using the table+optional
> column list format, (aria_precis), just change it to the "( query )"
> format ( NOTICE THE MANDATORY PARENS, this is how copy knows what to
> do. "t" => table, "t(c1)"=>table+columns, "(xx)" => query ( nothing
> before the opening parens ). )
>
> Happy new year.
>
> Francisco Olarte.
>

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Find duplicates in a column then print Information to a file
  2017-12-31 18:19 Find duplicates in a column then print Information to a file Sherman Willden <operasopranos@gmail.com>
  2017-12-31 18:53 ` Re: Find duplicates in a column then print Information to a file Francisco Olarte <folarte@peoplecall.com>
  2017-12-31 19:04   ` Re: Find duplicates in a column then print Information to a file Sherman Willden <operasopranos@gmail.com>
@ 2017-12-31 19:23     ` Rob Sargent <robjsargent@gmail.com>
  2017-12-31 20:08       ` Re: Find duplicates in a column then print Information to a file Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 6+ messages in thread

From: Rob Sargent @ 2017-12-31 19:23 UTC (permalink / raw)
  To: Sherman Willden <operasopranos@gmail.com>; +Cc: Francisco Olarte <folarte@peoplecall.com>; martin.moore@avbrief.com; pgsql-general

Keep in mind there is a quick write-to-file in psql with ‘\o <filename>’. And don’t forget to turn it off with ‘\o’

> On Dec 31, 2017, at 12:04 PM, Sherman Willden <operasopranos@gmail.com> wrote:
> 
> Thank you for the replies. I will start working on them now. Not a student but since I now have the time I may look into it. I am 71 retired working at Home Depot. I have a collection of CDs by various artists and I have the time to create and maintain my own database concerning these subjects. I retired from USAF, SUN Microsystems, and HP where I worked with the UNIX systems. As I said my main question right now is which duplicate songs do I have and who performs them. I really appreciate the support I get here.
> 
> Sherman
> 
>> On Sun, Dec 31, 2017 at 11:53 AM, Francisco Olarte <folarte@peoplecall.com> wrote:
>> Mmmmm, I notice a faint homework smell here ;-> , but the question is
>> nicely asked so:
>> 
>> On Sun, Dec 31, 2017 at 7:19 PM, Sherman Willden
>> <operasopranos@gmail.com> wrote:
>> ...
>> > SELECT aria FROM aria_precis WHERE aria IN (SELECT aria FROM aria_precis
>> > GROUP BY aria  HAVING COUNT(aria)>1);
>> 
>> The outer select is fully redundant. Just use the inner one. In fact,
>> not redundnat, harmful, as it will spit many copies of aria for the
>> duplicated ones. If you add id (and/or artist) to the outer query then
>> it is useful again.
>> 
>> 
>> > When I perform the following I get (0 rows):
>> > SELECT aria FROM aria_precis WHERE aria IN (SELECT aria FROM aria_precis
>> > GROUP BY id, aria  HAVING COUNT(aria)>1);
>> 
>> As expected. See the inner query, you are grouping by ID which is the
>> primary key, by PK definition all counts are going to be one, so no
>> results, nothing goes to the outer query.
>> 
>> Use the first query, but adding id and artist to the OUTER level.
>> 
>> > After I get the above information how do I write that information to a file?
>> > The only thing I know writes the entire database to a file:
>> >  \COPY aria_precis TO '/home/sherman/aria_precis_2.csv' WITH DELIMITER '#,'
>> > CSV HEADER;
>> 
>> If copy format is ok to you, knowing \copy look at the docs, looking
>> at https://www.postgresql.org/docs/9.6/static/app-psql.html you will
>> read:
>> 
>> >>>
>> \copy { table [ ( column_list ) ] | ( query ) } { from | to } {
>> 'filename' | program 'command' | stdin | stdout | pstdin | pstdout } [
>> [ with ] ( option [, ...] ) ]
>> 
>> Performs a frontend (client) copy. This is an operation that runs an
>> SQL COPY command, but instead of the server reading or writing the
>> specified file, psql reads or writes the file and routes the data
>> between the server and the local file system. This means that file
>> accessibility and privileges are those of the local user, not the
>> server, and no SQL superuser privileges are required.
>> <<<
>> 
>> Notice the (query) option? Your copy is using the table+optional
>> column list format, (aria_precis), just change it to the "( query )"
>> format ( NOTICE THE MANDATORY PARENS, this is how copy knows what to
>> do. "t" => table, "t(c1)"=>table+columns, "(xx)" => query ( nothing
>> before the opening parens ). )
>> 
>> Happy new year.
>> 
>> Francisco Olarte.
> 

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Find duplicates in a column then print Information to a file
  2017-12-31 18:19 Find duplicates in a column then print Information to a file Sherman Willden <operasopranos@gmail.com>
  2017-12-31 18:53 ` Re: Find duplicates in a column then print Information to a file Francisco Olarte <folarte@peoplecall.com>
  2017-12-31 19:04   ` Re: Find duplicates in a column then print Information to a file Sherman Willden <operasopranos@gmail.com>
  2017-12-31 19:23     ` Re: Find duplicates in a column then print Information to a file Rob Sargent <robjsargent@gmail.com>
@ 2017-12-31 20:08       ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 6+ messages in thread

From: Tom Lane @ 2017-12-31 20:08 UTC (permalink / raw)
  To: Rob Sargent <robjsargent@gmail.com>; +Cc: Sherman Willden <operasopranos@gmail.com>; Francisco Olarte <folarte@peoplecall.com>; martin.moore@avbrief.com; pgsql-general

Rob Sargent <robjsargent@gmail.com> writes:
> Keep in mind there is a quick write-to-file in psql with ‘\o <filename>’. And don’t forget to turn it off with ‘\o’

See also "\g filename", for one-shot output.

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2017-12-31 20:08 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-12-31 18:19 Find duplicates in a column then print Information to a file Sherman Willden <operasopranos@gmail.com>
2017-12-31 18:23 ` Martin Moore <martin.moore@avbrief.com>
2017-12-31 18:53 ` Francisco Olarte <folarte@peoplecall.com>
2017-12-31 19:04   ` Sherman Willden <operasopranos@gmail.com>
2017-12-31 19:23     ` Rob Sargent <robjsargent@gmail.com>
2017-12-31 20:08       ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox