Replacing row values in multiple columns from cascaded table join - python

I have SQL tables in below format, I was figuring whats the best approach to navigate and join tables to get to the final result. I can do it in Python as well since this seems to require join on multiple columns which would end up duplicating rows.
Any tips?
Table 1:
Table 2 and Table 3 have different number of digits in Account.
Table 2:
Table 3:
Table 1 - With new columns that is needed after navigating from Table 2 and Table 3 to fetch the data into Table1.

You seem to want to combine tables 2 and 3. The logic is not 100% clear, but something like this
with t23 as (
select t2.account, concat(t3.account, ' - ', t3.desc) as desc
from table2 t2 join
table3 t3
on t2.desc = t3.desc
)
select t1.*, t23_1.desc, t23_2.desc, t23_3.desc
from table1 t1 left join
t23 t23_1
on t1.c1 = t23_1.account left join
t23 t23_2
on t1.c2 = t23_2.account left join
t23 t23_3
on t1.c3 = t23_3.account;
I am not sure if t23 is being defined by fiddling with the account column. However, joining on desc seems more obvious.
Also, desc is a very bad name for a column, because it is a SQL keyword (think order by).

Related

How two join multiple tables with multiple conditions in sqlalchemy

select d.field_value as name,a.mobile,c.balance,a.created_at from users as a inner join user_profiles as b on a.id = b.user_id
inner join wallets as c on c.user_profile_id = b.id
left join profile_details as d on d.user_id = a.id where d.field_name = "name" and c.balance > 0 order by a.id desc;
This is my query and i need to write in sqlalchemy, i have model with all mentioned tables and proper columns.
Thanks for yours all efforts and time spent here.
It's pretty easy. I've some links to be referred.
You can refer to the documentation here.
How to join multiple tables here.
How to select only few columns from the query here.
Left and/or right outer join using sqlalchemy here
I think now you may be able to solve your problem. Hope this helps.

How to speed up join query in postgresql

I use psycopg2 to to join two tables together. But for some reason the process is taking way too long. Both tables have index. The tables have around 280 rows and 5-6 columns. But they do have a geometry column with complex boundaries of countries.
This is how the query looks like:
sqlstr = """CREATE TABLE {new_table_name} AS (
SELECT {geom_table_columns}, {added_columns}
FROM {geom_table}
INNER JOIN {temp_table} ON
(g.{id} = {temp_table}.{id})
);
""".format(** {
'new_table_name': new_table,
'geom_table': geom_table_name,
'geom_table_columns': geom_table_columns,
'temp_table': table_name_temp,
'id': geom_table_id,
'added_columns': added_columns
})
Is there any way to make this inner join query faster?

Any faster way to do mysql update query in R? in python?

I tried to run this query:
update table1 A
set number = (select count(distinct(id)) from table2 B where B.col1 = A.col1 or B.col2 = A.col2);
but it takes forever bc table1 has 1,100,000 rows and table2 has 350,000,000 rows.
Is there any faster way to do this query in R? or in python?
I rewrote your query with three subqueries instead of one - with UNION and two INNER JOIN statements:
UPDATE table1 as A
SET number = (SELECT COUNT(DISTINCT(id))
FROM
(SELECT A.id as id
FROM table1 as A
INNER JOIN table2 as B
ON A.col1 = B.col1) -- condition for col1
UNION DISTINCT
(SELECT A.id as id
FROM table1 as A
INNER JOIN table2 as B
ON A.col2 = B.col2) -- condition for col2
)
My notes:
Updating all of the rows in table1 doesn't look like a good idea, because we have to touch 1.1M rows. Probably, another data structure for storing number would have better performance
Try to run part of the query without update of table1 (only part of the query in parenthesis
Take a look into EXPLAIN, if you need more general approach for optimization of SQL queries: https://dev.mysql.com/doc/refman/5.7/en/using-explain.html

Is it possible to store query results to reuse in another query in MySQL?

I have a query to search for data that falls within a certain time period like so:
SELECT id1
FROM table1
WHERE (time > '[time goes here]'
AND time < '[time goes here]')
I am storing and using this data in Python, and then I wish to search for data within the previous result from another table like so:
SELECT id2
FROM table2
WHERE (table2.id1 = '[results from previous query]'.id1
AND '[other conditions go here]')
SELECT id3
FROM table3
WHERE (table3.id2 = '[results from previous query]'.id2
AND '[other conditions go here]')
I would have to do this recursively (an undetermined number of times) so it cannot be done manually. Is it possible to use the results from the previous query in any way or would I have to put the entire first query into the second query, and then put the entire second query into the third query? If it is the latter, is there any way to speed this up, as the first query alone takes several seconds and I can't afford to rerun the query multiple times.
you can use CTE like this
with data1 as
(
select id2 FROM table2 WHERE
),
data2 as
(
select id3 from table 3 where
)
select * from data1,data2
with data1 as
(
select id2 FROM table2 WHERE
),
data2 as
(
select id3 from table3,data1 where <condition>
)
select * from data2
Solution1:
You could use join:
select t2.id2 from table1 t1,table2 t2 where t2.id2=t1.id1 and t1.time>x and t1.time
Solution2:
You could also save the result in a list and then pass it to the next query:
select id2 from table2 where id2 in (list)

sqlite SQL query for unprocessed rows

I'm not quite even sure where / what to search for - so apologies if this is a trivial thing that has been asked before!
I have two tables in sqlite:
table_A = [id, value1, value2]
table_A$foo = [id, foo(value1), foo(value2)]
table_A$bar = [id, bar(value1), bar(value2)]
Where foo() / bar() are arbitrary functions not really relevant here
Now at the moment, I do:
select * from table_A
And use this cursor to compute all the rows for each of the derivative tables.
If something goes wrong (or I add new rows to table_A), i'd like a way to be able to compute (within SQL, rather than in python) which rows are already present in table_A$foo etc. and so just select the remaining (so like a AND NOT)to compute foo() and bar() - i should be able to do this on the ID col, as these remain the same.
Wondering if there is a way to do this in sqlite, which I imagine would be quicker than trying to rig this up in python.
Many thanks!
I don't understand if you consider a match based on value1 columns matching, or a combination of all three columns...
Using EXISTS to find those that are already present:
SELECT *
FROM TABLE_A a
WHERE EXISTS(SELECT NULL
FROM TABLE_A$foo f
WHERE a.id = f.id
AND a.value1 = f.value1
AND a.value2 = f.value2)
Using EXISTS to find those that are not present:
SELECT *
FROM TABLE_A a
WHERE NOT EXISTS(SELECT NULL
FROM TABLE_A$foo f
WHERE a.id = f.id
AND a.value1 = f.value1
AND a.value2 = f.value2)

Categories