Unioning and Joining in 1 Query? - mysql

I have the following tables:
TABLE1:
A B C
1 2 3
2 4 6
3 6 9
TABLE2:
A B C
4 8 12
5 10 15
6 12 18
TABLE3:
A D
2 X
4 Y
6 Z
I need one query that gives:
A B C D
1 2 3
2 4 6 X
3 6 9
4 8 12 Y
5 10 15
6 12 18 Z
Is that possible?
I can do it in 2 queries, but the person I'm doing it for wants it in 1.
Thanks!

Try this (example on sqlfiddle):
SELECT x.a, x.b, x.c, d
FROM (
SELECT a, b, c FROM table1
UNION ALL
SELECT a, b, c FROM table2
) x
LEFT JOIN table3 ON ( table3.a = x.a )

Sure:
select v1.*, table3.d
from
(select table1.a, table1.b, table1.c
from table1
union all
select table2.a, table2.b, table2.c
from table2
) v1
left join table3 on v1.a = table3.a

Related

mysql select max group by id

good evening,
i have a table:
A B C
45 1 1
22 2 1
40 3 1
43 1 2
21 2 2
61 3 2
49 4 2
60 5 2
76 1 3
41 2 3
57 3 3
i find max(A) from max(B) group by C. The result should be 60 - max number in A from last row in B from each group (C)
Thank you for your help
If i understand correctly your question you could use an inner join on select max(b):
select max(A)
from my_table m
inner join (
select C,
max(B) act_B
from my_table
group by C
) t on t.act_B = m.B and t.c = m.c

Identical values in one column and unique value pairs

I have 2 columns
A B
1 2
2 2
1 2
3 2
5 2
0 2
4 2
11 4
12 4
11 4
I want the SQL query to return the pairs (A,B) where:
B has appeared 3 or more times
AND (A,B) is unique
The resulting table would be:
A B
1 2
2 2
3 2
5 2
0 2
4 2
You could use a join with a selected table grouped by B having count = 3
select distinct A, B
from my_table as t1
inner join (
select b
from my_table
group by b
having count(*)= 3
) t2 on t2.b = t1.b
and for 3 or more
select distinct A, B
from my_table as t1
inner join (
select b
from my_table
group by b
having count(*) >= 3
) t2 on t2.b = t1.b

MySQL - Select distinct detecting changes on different ordered rows

I have a table like this:
T A B C ID
2015-07-19 a b c 1
2015-07-16 a y z 2
2015-07-21 a b c 1
2015-07-17 a y c 2
2015-07-18 a y c 1
2015-07-20 a b c 1
2015-07-17 a y c 1
2015-07-19 a b c 2
2015-07-16 a y z 1
2015-07-20 a b c 2
2015-07-15 a y z 1
2015-07-22 x b c 1
2015-07-21 a b c 2
2015-07-18 a y c 2
2015-07-15 a y z 2
2015-07-22 a y c 2
2015-07-14 x b c 1
I need to get an ordered result by datetime column T, but I need that the query detects and avoid repeated rows in columns A, B and C. And all this ordered and separated by ID.
It could be a stored procedure. It's important to be fast, because is a huge log table. With millions of rows.
The result should be like this:
T A B C ID
2015-07-22 x b c 1
2015-07-19 a b c 1
2015-07-17 a y c 1
2015-07-15 a y z 1
2015-07-14 x b c 1
2015-07-22 a y c 2
2015-07-19 a b c 2
2015-07-17 a y c 2
2015-07-15 a y z 2
Any ideas?
This query gives the expected result (tested):
SELECT t1.* FROM mytable t1
LEFT JOIN mytable t2 ON t1.t = t2.t + INTERVAL 1 DAY AND t1.A = t2.A AND t1.B = t2.B AND t1.C = t2.C AND t1.ID = t2.ID
WHERE t2.T IS NULL
ORDER BY t1.ID, t1.T DESC
Try this query:
SELECT max(t),a,b,c,id FROM table GROUP BY A,B,C,id ORDER BY ID, max(T)
Try this query:
SELECT * FROM table GROUP BY A,B,C,T ORDER BY ID, T
Edit: added T to group by

Min Max Value in coloumn but with a certain condition

I have a MySql table with 3 coluomn : Nip, Bidang and Total.
I want to count min and max value of total with the same bidang. but i don't want to count min and max value for all coloumns.
Sample data:
NIP Bidang Total
1 A 10
2 A 5
3 A 1
4 B 4
5 B 7
6 C 8
7 C 9
And the result column:
MIN
1
1
1
4
4
8
8
Simply do this:
SELECT * FROM
(
(SELECT Bidang FROM TableName) T1 LEFT OUTER JOIN
(SELECT bidang, MIN(Total) MinVal, MAX(Total) MaxVal
FROM TableName
GROUP BY Bidang) T2 ON T1.Bidang=T2.Bidang
)
Result:
BIDANG MINVAL MAXVAL
A 1 10
A 1 10
A 1 10
B 4 7
B 4 7
C 8 9
C 8 9
See result in SQL Fiddle.
EDIT
To see the Total column also, add Total to first query.
SELECT * FROM
(
(SELECT NIP,Bidang,Total FROM TableName) T1 LEFT OUTER JOIN
(SELECT bidang, MIN(total) MinVal, MAX(Total) MaxVal
FROM TableName
GROUP BY Bidang) T2 ON T1.Bidang=T2.Bidang
)
Result:
NIP BIDANG TOTAL MINVAL MAXVAL
1 A 10 1 10
2 A 5 1 10
3 A 1 1 10
4 B 4 4 7
5 B 7 4 7
6 C 8 8 9
7 C 9 8 9
See result in SQL Fiddle.
Select bidang, min(total) MyMin, Max(total) myMax
From tableName
group by bidang
NIP bidang total
1 A 10
2 A 5
3 A 1
4 B 4
5 B 7
6 C 8
7 C 9
should return
A 1 10
B 4 7
C 8 9

MySql duplicate SUM, one to many relation ship

I have following 3 tables where B, C have many to one relation ship with A, and i need to generate the following Result table with MySql
Table A Table B Table C
id name id A.id qty id A.id
1 a 1 1 5 1 1
2 b 2 1 10 2 1
3 c 3 2 4 3 2
4 3 6 4 2
5 2 7
Expected Result Table
C.id A.id sum(B.qty Group by A.id)
1 1 15
2 1 15
3 2 11
4 2 11
select c.id, a.id, sum(b.qty)
from c
inner join a on c.a_id = a.id
inner join b on b.a_id = a.id
group by c.id, a.id
or in your case, you just don't need table a
select c.id as cid, c.a_id as aid , sum(b.qty) as totalqty
from c
inner join b on b.a_id =c.a_id
group by c.id, c.a_id;