Well I think it's a simple question but I could not found the solution.
Well I have three Tables:
Table 1
id(AS t1id) Name LASTNAME Value
1 a z 50
2 b e 60
3 c k 70
4 d u 60
Table2
id idTable1 Name(AS t2me) Value(AS t2ve)
1 1 er 50
2 1 zx 150
3 2 zc 300
Table 3
id idTable1 Name(AS t3me) Value(AS t3ve)
1 2 erxc 50
2 2 zvvx 150
3 2 zcz 300
How to get this result with SQL
t1id Name LASTNAME t2me t2ve t3me t3ve
1 a z er 50 erdxc 150
1 a z zx 150
2 b e zc 300 erxc 50
2 b e zvvx 150
2 b e zcz 300
Is that possible? If not what could I do?
SELECT t1.id as t1id, t1.Name, t1.LASTNAME,
t2.Name as t2me, t2.Value as t2ve, t3.Name as t3me,
t3.Value as t3ve from Table1 t1
LEFT JOIN Table2 t2 on t1.id = t2.idTable1
LEFT JOIN Table3 on t3.idTable1 = t1.id
Related
In mySql I have 2 tables.
t1 looks like
id color
------------
1 red
2 green
3 blue
4 purple
t2 looks like
id t1_id ship
------------------------
1 1 a
2 1 b
3 1 c
4 2 a
5 2 b
6 3 b
I need to fill t2 so that for each entry in t1 there are three entries in t2 - one with "a" in the ship row one with "b" in the ship row and one with "c" in it.
I don't want to have multiple entries with the same t1_id <-> ship combo. So after the query t2 should look like this.
t2 after the query I am looking for
id t1_id ship
------------------------
1 1 a
2 1 b
3 1 c
4 2 a
5 2 b
6 3 b
7 2 c
8 3 a
9 3 c
10 4 a
11 4 b
12 4 c
Any ideas on how to achieve that?
If you want to put these in the table, you can use:
insert into t2 (t1_id, ship)
select t1.id, s.ship
from t1 cross join
(select distinct ship from t2) s left join
t2
on t2.t1_id = t1.id and t2.ship = s.ship
where t2.t1_id is null;
This inserts the "remaining" values in table2.
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
I have table1 like this:
ID uid jid type val
1 1 1 1 1
2 1 1 1 10
3 1 2 1 100
4 1 3 1 1000
5 1 4 2 2
And joined table2:
ID uid jid stat time
1 1 1 1 100
2 1 1 1 200
3 1 1 4 300
4 1 2 2 400
I try to get SUM of val from table1, group it by uid and type and join table2 by this query:
SELECT a.uid, a.type, SUM(a.val) as t1, SUM(a.val)*COUNT(distinct(a.id))/COUNT(a.id) as t2, MAX(b.time) as max_time
FROM table1 as a
LEFT JOIN table2 as b on b.uid = a.uid and b.jid = a.jid and b.stat = 1
GROUP BY a.uid, a.type
In result I get this values:
uid type t1 t2 max_time
1 1 1122 748.0000 200
1 2 2 2.0000 NULL
But total for type=1 should be: 1111 (not 1122 and not 748)
Please tell me what I'm doing wrong.
You need use DISTINCT with a.uid in select statement. corrected query is below:
SELECT DISTINCT(a.uid), a.type, SUM(a.val) AS t1, SUM(a.val)*COUNT(DISTINCT(a.id))/COUNT(a.id) AS t2, MAX(b.time) AS max_time
FROM table1 AS a
LEFT JOIN table2 AS b ON b.uid = a.uid AND b.jid = a.jid AND b.stat = 1
GROUP BY a.uid, a.type
can you recheck the sql:
SELECT auid, atype, SUM(aval)
, SUM(aval)*COUNT(DISTINCT(aid))/COUNT(aid) AS t2, MAX(btime) AS max_time
FROM
(
SELECT DISTINCT(a.uid) AS auid, a.type AS atype, a.val AS aval, a.id AS aid, b.time AS btime
FROM table1 AS a
LEFT JOIN table2 AS b ON b.uid = a.uid AND b.jid = a.jid AND b.stat = 1
) AS grouptable
GROUP BY auid, atype
Output I got:
auid atype sum(aval) t2 max_time
1 1 1111 1111.0000 200
1 2 2 2.0000 (NULL)
Is the result you are looking for?
I need to add row 1 from Table2 to row 4 in Table1.
Result for row 4 in Table1 will be: DD1 9 5 7
Table1 Table2
======= =======
Category C1 C2 C3 Category C1 C2 C3
A 8 4 5 DD1 1 1 2
B 1 0 3
C 2 1 0
DD1 8 4 5
Finally table1 becomes...
Table1
=======
Category C1 C2 C3
A 8 4 5
B 1 0 3
C 2 1 0
DD1 9 5 7
Any help would be much appreciated.
Try this:
UPDATE T1
SET
T1.C1 = T1.C1 + T2.C1,
T1.C2 = T1.C2 + T2.C2,
T1.C3 = T1.C3 + T2.C3
FROM Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.Category = T2.Category
WHERE T1.Category = 'DD1'
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