Click here to Skip to main content
15,905,587 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Guy I wish to combine some rows from same table,so that a print result that would have been like this
[("1","rick","manager","Paris"), ("2","Dane","accounting","Paris")]
Would be like this
[("1","rick","manager","Paris","2","Dane","accounting","Paris")]

What I have tried:

def pick(place):
conn= sqlite3.connect('my.db')
cursor = conn.cursor()
cursor m.execute("SELECT * FROM table WHERE location=?UNION ALL",(place))
row=cursor.fetchall()
conn.commit()
conn.close()
pick("Paris")
Posted
Updated 31-Jan-20 3:58am

You can only do that if the two rows have common information - otherwise the system has no idea which row to "combine" with the other - and without that, it can't guarantee to produce the same information twice in a row.

In your example the only common data is "Paris", so just use a JOIN:
SQL
SELECT a.*, b.* 
FROM Table1 a
JOIN Table2 b
   ON a.ColumnWithParisIn = b.OtherColumnThatHasParisIn
 
Share this answer
 
v2
Two things:

First, UNION ALL allows duplicates whilst UNION does not.

You need a way to compare data that my only differ by user-created nuance.
A way I was able to handle your problem, in most cases, is as follows:

1- Collect all the data from your request and after casting all to char format append them into one sting.
2 - filter sting so that only alphanumeric characters remain. That means removing all spacing and punctuation. Convert all to same case if if necessary in your database type (viz-a-viz, testing for duplicates).
3 - compare this string to others in your list made in this same manner.
4 - remember that alphanumeric differences, like spelling errors, will not be discovered.

Now - what's left out of this? You'll need a way to distinctly select the sources from these (i.e, those that match) and block them.

One strategy: dump the result of your query into a tmp table. Do a query on it to find those with matching aggregate strings and delete all but one from the table. Use what's left in the tmp table, except you no longer need the aggregate string, to select for your query.

How you make them distinct and remove dupes (based on aggregate) from your tmp table is up to you . . .
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900