Joining/Merge two keys to one key and removing duplicates - mysql

I want to merge two tables using SQL (Toad for Oracle) merging one key from one table to two keys of the main table while deleting the duplicated entries. I guess an illustration provides you better understanding of my problem:
Dataframe A:
Key1 Key2 ColA
1 2 1993
1 2 1992
1 4 1991
2 4 1990
2 5 1989
2 5 1988
3 5 1987
3 6 1986
3 6 1985
Dataframe B:
Key1&2 ColB
1 Adress1
1 Adress1
1 Adress1
2 Adress2
2 Adress2
3 Adress3
3 Adress3
3 Adress3
4 Adress4
4 Adress4
5 Adress5
6 Adress6
6 Adress6
Desired Dataframe:
Key1 Key2 ColA ColB-1 ColB-2
1 2 1993 Adress1 Adress2
1 2 1992 Adress1 Adress2
1 4 1991 Adress1 Adress4
2 4 1990 Adress2 Adress4
2 5 1989 Adress2 Adress5
2 5 1988 Adress2 Adress5
3 5 1987 Adress3 Adress5
3 6 1986 Adress3 Adress6
3 6 1985 Adress3 Adress6
I tried using following statement so far:
SELECT *
FROM A
LEFT JOIN B ON A.key1=B.key1&2
LEFT JOIN B ON A.key2=B.key1&2
However, as I explained, because dataframe B has duplicated rows in its keys, duplicated rows in my output as well.
Hope its clear.
Thanks,
KS

Is this what you want?
SELECT A.*, B1.ColB, B2.ColB
FROM A LEFT JOIN
(SELECT DISTINCT key1&2, ColB
FROM B
) B1 ON A.key1 = B1.key1&2 LEFT JOIN
(SELECT DISTINCT key1&2, ColB
FROM B
) B2 ON A.key2 = B2.key1&2;

Related

Sum the duplicate values order by date from sql table [duplicate]

This question already has an answer here:
How to sum the duplicate values from mysql table
(1 answer)
Closed 4 years ago.
I have a MySQL table with rows containing duplicate values of Ref_Nr column. So i want to sum the values of Points with respect Ref_Nr, to u_id and r_date columns.
id u_id r_date Points Ref_Nr
1 1 2018-04-11 1 3
2 1 2018-04-11 2 3
3 2 2018-04-11 3 4
4 2 2018-04-11 4 4
5 3 2018-04-11 6 2
6 3 2018-04-11 6 2
7 1 2018-04-10 3 3
8 1 2018-04-10 5 3
9 1 2018-04-10 2 4
10 1 2018-04-10 2 4
11 2 2018-04-10 3 3
12 2 2018-04-10 5 3
13 3 2018-04-10 2 4
14 3 2018-04-10 2 4
Here is the my sql query what i tried, but i am not getting proper output
SELECT u_id, Ref_Nr ,r_date, SUM(Points) AS Points
FROM my_table ORDER BY r_date, Ref_nr,u_id;
Here is the expected output, please help me to solve this problem
u_id r_date Points Ref_Nr
1 2018-04-11 3 3
2 2018-04-11 7 4
3 2018-04-11 12 2
1 2018-04-10 8 3
1 2018-04-10 4 4
2 2018-04-10 8 3
3 2018-04-10 4 4
You need to group tha data. You can't mix normal column selects with aggregate functions like sum()
SELECT r_date, Ref_nr, u_id,
SUM(Points) AS PointSum
FROM my_table
GROUP BY r_date, Ref_nr, u_id
ORDER BY r_date, Ref_nr, u_id;

Use avg() and joins mysql

Say I have a table called students
idStudent Name
1 Billy
2 Mariah
3 Chris
4 Mark
5 Sarah
and another table called tests
idTest score student_idstudent
1 50 1
2 100 1
3 90 2
4 100 3
5 45 4
is it possible to use a combination of a join and avg() to get a result like
idStudent avg_test
1 75
2 90
3 100
4 45
5 0
SELECT s.idStudent,
AVG(COALESCE(t.score, 0)) AS avg_test
FROM students s
LEFT JOIN tests t
ON s.idStudent = t.student_idStudent
GROUP BY s.idStudent

Select record from table with not in other table

I am stuck in query I have a table like this
budget_details
id budget_id expenditure_head_id budget
1 1 1 1233
2 1 2 333
3 1 3 567
4 2 1 343
5 2 2 343
6 2 3 6767
7 2 4 557
expenditure_heads
id name
1 abc
2 xyz
3 qwe
4 uvw
I want to get all the expenditure_heads from budget_details that even
if not in budget_details like here budget_id=1 does not contain expenditure_head_id 4
but I want to select that to with 0 or null displaying
I tried this but it not displaying expenditure_head_id 4
select `expenditure_heads`.`name`, `budget_details`.`budget`, `budget_details`.`id` from
`budget_details`
right join `expenditure_heads` on `budget_details`.`expenditure_head_id` = `expenditure_heads`.`id`
where `budget_details`.`budget_id` = 1"
The where avoid you to get the missing row you need. The left join is done on the ON statement, so this query should work for you:
SELECT EH.name, BD.budget, BD.id FROM expenditure_heads EH
LEFT JOIN budget_details BD
ON (BD.expenditure_head_id = EH.id AND BD.budget_id = 1)

mysql- 3 random records for main departments with multiple level depth of subdepartments

I am trying make a complex query in MySQL i have following two tables
departments employees
Id parent title id name department status
--------------------------- ------------------------------------
1 0 Health 1 abc 3 1
2 0 Sports 2 def 3 1
3 0 Education 3 ghi 5 1
4 1 Physical 4 jkl 10 1
5 1 Mental 5 kkk 6 1
6 2 Football 6 lll 6 1
7 2 Baseball 7 sss 8 1
8 2 Beachball 8 xxx 6 1
9 2 Hockey 9 yyy 6 1
10 4 ENT 10 zzz 7 1
11 0 Finance 11 mnb 11 1
Departments table have four main departments(i-e: parent = 0) with multiple level depths of sub-departments.
Currently i have done it through a PHP function by running queries multiple time to achieve this, but still i want know if this is possible to fetch it with a Query.
Whats the best way OR how to select max 3 employee randomly for each main department with status 1.
The expected result should be something like
id name department maindep
1 abc 3 3
2 def 3 3
3 ghi 5 1
4 jkl 10 1
5 kkk 6 2
7 sss 8 2
10 zzz 7 2
11 mnb 11 11

Mysql left join or much simpler way?

I need to order data according to order index from other table. and order the data with the same 'id' according to entry date.
i cant figure it out how to join data and order using mysql command.
Table1
id name order
1 Ali 1
2 Cenk 3
3 Tan 2
Table 2
id tid m date
1 232 msj1 3
2 434 msj2 2
1 453 msj4 1
3 455 msj5 2
2 541 msj6 4
1 234 msj7 2
3 132 msj8 6
Needed query result
id tid m date
1 453 msj4 1
1 234 msj7 2
1 232 msj1 3
3 455 msj5 2
3 132 msj8 6
2 434 msj2 2
2 541 msj6 4
This should work:
select t2.id, t2.tid, t2.m, t2.date
from t2
left join t1 on t2.id=t1.id
order by t1.order
This orders by the ordering field from table 1.