combine two columns in 1 column mysql - mysql

i have tablea and table b
tablea :
Nama Jumlah
A 66
B 95
C 47
E 57
F 52
tableb:
Nama Jumlah Gaji
A 35 47
B 28 51
C 18 24
D 27 30
E 30 29
G 31 16
how to make query that can combine two tables in one table an to be like this
result :
Nama Jumlah Gaji
A 101 47
B 123 51
C 65 24
D 27 30
E 87 29
F 52 0
G 31 16
it's my query. but i can't get the the result like that.
SELECT a.nama, (a.jumlahtotala+b.jumlahtotalb) AS Jumlah FROM (SELECT nama, SUM(jumlah) AS jumlahtotala FROM tablea GROUP BY nama) a JOIN (SELECT nama, SUM(jumlah) AS jumlahtotalb, SUM(gaji) AS gaji FROM tableb GROUP BY nama) b GROUP BY a.name
thanks for your help
EDITED
Sorry for another question in comment

Try joining the two tables like:
SELECT b.Nama, IFNULL(a.Jumlah, 0) + b.Jumlah, b.Gaji
FROM tablea a RIGHT JOIN tableb b
ON a.Nama = b.Nama

If Nama is not unique in tablea or tableb, or if rows exist in tablea that don't have a matching row in tableb, for example:
tablea:
Nama Jumlah
A 66
B 95
C 47
C 18
tableb:
Nama Jumlah Gaji
A 35 47
C 0 24
D 27 30
Z NULL 99
If an acceptable result is to return a single occurrence of each value of Nama along with the totals of Jumlah and Gaji, then one approach (assuming the datatypes of the Nama and Jumlah columns is compatible), and assuming that there isn't a requirement to return the rows in a particular sequence, one option is to combine the two sets with a UNION ALL operator into a derived table, and then use SUM() aggregate.
For example:
SELECT t.Nama
, SUM(t.Jumlah) AS Jumlah
, SUM(t.Gaji) AS Gaji
FROM ( SELECT b.Nama
, b.Jumlah
, b.Gaji
FROM tableb b
UNION ALL
SELECT a.Nama
, a.Jumlah
, NULL
FROM tablea a
) t
GROUP BY t.Nama
Because of the derived table (i.e. the way that MySQL processes derived tables), this will likely not be the most efficient approach for large sets.

Related

SQL GROUP BY with two tables involved

Could you guys help me to make SELECT query for my simple case:
Table A:
UserID UserName
10 John
11 Mike
12 Matt
Table B:
SessionID UserID SessionTime
124 10 20
123 10 122
42 10 30
324 11 55
534 11 42
344 12 19
653 12 32
I need this result:
Result Table:
UserName UserID TotalTime
John 10 172
Mike 11 97
Matt 12 51
For one Table B this works:
SELECT UserID, SUM(SessionTime) AS "Total Time"
FROM TableB
GROUP BY UserID;
but I need to attach UserName to the final result
thank you
You can do that by using join and group by:
select a.UserId, a.UserName, sum(b.SessionTime) as TotalTime
from tableA a
left join tableB b on a.UserId = b.UserId
group by a.UserId, a.UserName;
Note: This would work for 1-to-many relations as in your case.
SELECT TableA.Username, TableA.User_ID, SUM(SessionTime) INNER JOIN
TableB ON TableA.User_ID = TableB.User_ID GROUP BY TableA.Username,
TableA.User_ID
SELECT a.UserName as "UserName"
,a.UserID as "UserID"
,sum(b.SessionTime) as "TotalTime"
FROM a LEFT JOIN b
ON a.UserID = b.UserID GROUP BY a.UserID
Here. I used TABLE a and Table b

MYSQL Get lowest value in column of a group specified by another column

I have a table that looks like this:
id name yearofstudy mark
1 Alain A 2 75
2 Michael B 3 85
3 Chen C 1 55
4 Caroline D 2 60
5 Mohamed E 2 60
6 Alex F 1 55
7 Sofia O 3 78
8 Samir O 1 85
9 Rob G 2 78
10 Big K 3 55
And I'm trying to get the id, name, year and mark of the students with the lowest (and highest) mark in each year which would give:
id name yearofstudy mark
3 Chen C 1 55
4 Caroline D 2 60
10 Big K 3 55
SQL isn't my strong point and I've been trying using the MIN() function but I haven't managed to get it right yet and would really appreciate some help.
Using a subquery to get the min() and max() for each yearofstudy, and joining it to the original table. (You did say you wanted lowest and highest, right?)
select t.id, t.name, t.yearofstudy, t.mark
from t
inner join (
select
yearofstudy
, min(mark) as minMark
, max(mar) as maxMark
from t
group by yearofstudy
) as m
on t.yearofstudy = m.yearofstudy
and (t.mark = minMark or t.mark = maxMark)
or for just the lowest mark per year:
select t.id, t.name, t.yearofstudy, t.mark
from t
inner join (
select
yearofstudy
, min(mark) as minMark
from t
group by yearofstudy
) as m
on t.yearofstudy = m.yearofstudy
and t.mark = minMark
You could write the query as follows:
SELECT t1.* from your_table t1
INNER JOIN (
SELECT yearofstudy, MIN(marks) as marks
FROM your_table GROUP BY yearofstudy
) t2
ON t1.yearofstudy = t2.yearofstudy
AND t1.marks = t2.marks
GROUP BY t1.yearofstudy
ORDER BY t1.yearofstudy, t1.id;
If all the MIN records for the yearofstudy are required, then you could simply remove GROUP BY t1.yearofstudy
Demo

Remove duplicates in Join statement in mysql and group

I have two tables
table1
Name marks
John 50
Smith 70
Adam 60
Roy 70
table2
Score Grade other
50 C 1.5
60 B 0.7
70 A 0.8
70 A 1.0
I want to get how many people have got A, B, C passes
I want to get an output as
Grade Count
C 1
B 1
A 2
Query I tried was
SELECT table2.Grade,
COUNT(DISTINCT table2.Grade) as count
FROM table1
LEFT JOIN table2
ON table1.Mark = table2.Score
GROUP BY table2.Grade;
But it Gives
Grade Count
C 1
B 1
A 4
So How to remove the duplicates ?
Please help.
At your second table, you got duplicate rows:
Score Grade other
50 C 1.5
60 B 0.7
70 A 0.8
70 A 1.0
Here, there are to A's, and when you joing with first table according to the field "Grade--Score", the whole join is:
Jhon 50 C
Smith 70 A
Smith 70 A --> Second A from second table
Adam 60 B
Roy 70 A
Roy 70 A --> Second A from second table
So group by and count will result 4 for the field grade here:
A 4 --> 2 Smith and 2 Roy
B 1
C 1
So, to get how many single person per grade:
select tb2.Grade GradeMark, count(*) TotalPersons
from table1 as tb1
left join (select tbi2.Score, distinct(tbi2.Grade), tbi2.other, from table2 tbi2) as tb2 on tb2.Grade = tb1.marks
group by tb2.Grade
This query will select distinct values from table2, join with table one and count the results per grade so you should get:
A 2
B 1
C 1
You don't need a JOIN for this. You can try like below using a simple group by and get the count()
select Grade, count(*) as `Count`
from table2
group by Grade;

how can you eliminate permutations between columns in MySQL

i have table columns one (idprocess) point to columns two (idporcess1) and point to columns tree (idprocess2).
id idprocess idporcess1 idprocess2
1 15 16 17 <== A
2 15 16 19 <== B
3 15 20 23
4 14 16 17
6 16 15 80 <== C
7 17 15 49 <== D
8 23 16 20 <== E
I need a SQL query that returns this: row c and row D, so with number idprocess(16) and idprocess(17 )
because row c : idprocess(16) references again ipdprocess1(15)
because row c : idprocess(17 ) references agin ipdprocess1(15)
please help
i want only to eleminate circular referencial in tree
If you are happy to find rows where the first two columns are permutated, this will do the job:
SELECT *
FROM my_tbl t
WHERE EXISTS (SELECT 1 FROM my_tbl t1 WHERE t1.idprocess = t.idprocess1 AND t1.idprocess1 = t.idprocess)
ORDER BY t.id;
Alternative interpretation:
If you want all rows where idprocess1 has been listed in idprocess before (before = smaller id), then you can:
SELECT *
FROM my_tbl t
WHERE EXISTS (SELECT 1 FROM my_tbl t1 WHERE t1.id < t.id AND t1.idprocess = t.idprocess1)
ORDER BY t.id;
You wouldn't call that "permutation", though.
The question is a bit ambiguous but I tried to understand it on my own and prepared the following query:
SELECT *
FROM TEMP
where C2 IN ( Select C2 FROM TEMP group by C2 having count(C2) > 1 )
OR C3 IN ( Select C3 FROM TEMP group by C3 having count(C3) > 1 )

MySQL single table, select value based on multiple rows

From the table below, how would I select all animalIds that have a specific combination of attributeIds e.g. if I supplied attributeIds 455 & 685 I'd expect to get back animalIds 55 & 93
Table name: animalAttributes
id attributeId animalId
1 455 55
2 233 55
3 685 55
4 999 89
5 455 89
6 333 93
7 685 93
8 455 93
I have the following query that seems to work, however, I'm not sure if there is a more robust way?
SELECT animalId
FROM animalAttributes
WHERE attributeId IN (455,685)
GROUP BY animalId
HAVING COUNT(DISTINCT attributeId) = 2;
If you really want accurate results, you could go with a fool-proof method like this:
select distinct base.animalId
from animalAttributes base
join animalAttributes a on base.animalId = a.animalId
and a.attributeId = 455
where base.attributeId = 685
If you later needed 3 matching attributes, you could just add another join:
select distinct base.animalId
from animalAttributes base
join animalAttributes a on base.animalId = a.animalId
and a.attributeId = 455
join animalAttributes b on base.animalId = b.animalId
and b.attributeId = 999
where base.attributeId = 685
SELECT DISTINCT `animalId` FROM `animalAttributes` WHERE `attributeId` = 455
INTERSECT
SELECT DISTINCT `animalId` FROM `animalAttributes` WHERE `attributeId` = 685
SELECT DISTINCT animalId
FROM animalAttributes
WHERE attributeId IN (455,685)
or
SELECT animalId
FROM animalAttributes
WHERE attributeId IN (455,685)
GROUP BY animalId