How to sync two column code? - mysql

I want to synchronize the two columns (one table, two columns).
Example:
A-1 references B-2, 3, 4 but A-2 references only B-1.
How to add B-1 reference B-3 and B-4 with mysql query?
A B
-------
1 2
1 3
1 4
2 1
3 1
4 1
5 6
5 7
6 5
7 5

I think you want this:
insert into t(a, b)
select b, a
from t
where not exists (select 1 from t t2 where t2.a = t.b and t2.b = t.a);
This will put all pairs into the table, in both directions.

Related

Make a query to select a value from one column that is combined with all the values from another column

Imagine I have the following tables:
Numbers PK
1
2
3
4
5
6
7
8
9
10
Numbers FK 1
Numbers FK 2
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
1
10
1
8
8
10
4
7
3
4
9
1
6
3
9
4
6
5
6
I have the following tables: "Numbers PK" as Primary key and another 2 tables that are related one with each other that are foreign keys of Numbers.
I am trying to make a query to select the number(s) from the table "NumbersFK2" that are related with all the numbers of "Numbers PK".
As you can see in this example the solution would be 1 as 1 is related with 1-10 in the tables "Numbers FK1" and "Numbers FK2"
I have tried to solve and after some days I need some help as I don't know how could I do it. I appreciate the help. Thanks
We use dense_rank() to count the Numbers_PK in case they're not consecutive. Then we left join, group by and count(distinct Numbers_PK).
with t3 as (
select Numbers_PK
,dense_rank() over(order by Numbers_PK) as dns_rnk
from t
)
select Numbers_FK_2
from t3 left join t2 on t2.Numbers_FK_1 = t3.Numbers_PK
group by Numbers_FK_2
having count(distinct Numbers_PK) = max(dns_rnk)
Numbers_FK_2
1
Fiddle

group every 3 count rows with mysql query

can anyone help me to group every 3 counts rows???
I have data like this
num
score
1
3
1
3
1
3
1
3
1
3
1
3
4
3
4
3
4
3
2
3
2
3
2
3
and i want result like this
num
count(num)
1
3
1
3
2
3
4
3
You want to group every 3 rows. But without giving supplementary conditions, we can only take assumptions on how you would like to group. Supposing each row in the 3-rows group has the same content and the score value from the base table has no influence on grouping, I guess you just want to get the num once for every 3 rows as the 3 rows have the same num. Besides, we use a constant value 3 for count(num) column. If all my assumptions come true, try this:
select num, 3 as 'count(num)'
from (select num,#row_id:=#row_id+1 as row_id
from test,(select #row_id:=0) t1 ) t2
where row_id mod 3 =0
order by num
;

How to insert rows for each of the TYPE column?

How to insert rows for each of the type column? If there is two type, type 1 and type 2, then I need to insert two rows and also need to change the order and id value for whole table.
current status:
CHOICE Table
id choice type order
1 AA 1 1
2 BB 1 2
3 CC 1 3
4 AAA 2 4
5 BBB 2 5
6 CCC 2 6
7 DDD 2 7
Required updated table:
Now i wan to insert choice "000" for each type. The updated table will be look like bellow. How can I achieve this?
updated CHOICE Table
id choice type order
1 000 1 1
2 AA 1 2
3 BB 1 3
4 CC 1 4
5 000 2 5
6 AAA 2 6
7 BBB 2 7
8 CCC 2 8
9 DDD 2 9
here, id and order column serialized again.
The actual table is too big, so I cannot insert by edit. Please help for this complex query. I have no clue to solve this.
Use insert . . . select to insert the rows:
insert into choice (choice, type)
select distinct '000', type
from choice;
This assumes that id is automatically assigned (and it will be different from your example).
However, it looks like you want to update the order as well. For this, I would suggest an update:
update choice c join
(select c2.*,
row_number() over (partition by choice order by (order is null) desc, order) as new_order
from choice c2
) c2
on c.id = c2.id
set c.order = c2.new_order;
As an editorial comment, order is a very bad choice for a column name because it is a SQL keyword.

Update/Delete using views in parent tables

I have a table A with columns c1,c2,c3 and c4. I have another table B with the same columns(c1,c2,c3,c4).
The contents of both the tables are almost same. The only difference is that the table A contains some duplicate rows since no unique index in A.
I have a view C joining A and B. How can I find the rows that are updated/deleted in A, so that the same can be updated/deleted from B as well, using view C.
Here is sample data.
A
c1 c2 c3 c4
1 2 3 4
1 2 3 4
1 3 3 4
2 5 6 7
B
c1 c2 c3 c4
1 2 3 4
1 3 3 4
2 5 6 7
So, B= A - (duplicate entries)
In B, (c1,c2) is the unique key.
I have a view C with joins to A and B.
Goal : If a row is deleted from A, say (1,3,3,4), the same row should be deleted from B also.
Question : How can I use the view C to achieve the above mentioned Goal

What is the SQL equivalent to pandas 'transform'?

Suppose you have the following SQL table:
A B C
2 1 4
3 4 5
3 1 1
1 4 0
5 0 1
And you want to add/show a column containing the mean (or any other aggregate function) of column A for each distinct value of column B. You want to keep all columns. So the result would look like this:
A B C avg(A)|B
2 1 4 2.5
3 4 5 2.0
3 1 1 2.5
1 4 0 2.0
5 0 1 5.0
The best way to do it in pandas, as far as I know, would be:
>>> df['avg(A)|B'] = df.groupby('B')['A'].transform('mean')
>>> df
A B C avg(A)|B
0 2 1 4 2.5
1 3 4 5 2.0
2 3 1 1 2.5
3 1 4 0 2.0
4 5 0 1 5.0
How would you do it in SQL? Can one avoid using a JOIN?
You can join to a derived table that contains the aggregate value for each grouping of b
select * from mytable t1
join (
select avg(a), b
from mytable
group by b
) t2 on t2.b = t1.b
or using a subquery
select *, (select avg(a) from mytable t2 where t2.b = t1.b)
from mytable t1
the question is tagged both mysql and psql, so I'm not sure which db you're using. But on postgres you can use window functions
select *, avg(a) over (partition by b)
from mytable