Identical values in one column and unique value pairs - mysql

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

Related

MySql fill up table with values depending on other table

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.

Group and calculate sum with joined table

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?

Performing JOIN between two tables

I have two tables like this:
table_1
id user_id item_id item_number
15 1 1 7
16 1 2 12
17 1 3 1
18 1 4 0
19 1 5 11
20 5 1 2
21 5 2 2
22 5 3 5
23 5 4 7
24 5 5 1
table_2
id user_id item_id attribute
41 5 1 1
42 1 1 1
43 7 5 1
44 1 4 1
45 1 4 0
I would like to select user_id, item_id and item_number from table_1 and number of rows from table_2 where table_1.user_id = table_2.user_id and table_1.item_id = table_2.item_id.
I have this:
SELECT item_id, item_number, COUNT(attribute) AS number
FROM table_1
LEFT OUTER JOIN table_2 ON table_1.user_id = table_2.user_id
WHERE table_1.user_id='1'
Can you help me?
Expected result:
user_id item_id item_number number
1 1 7 1
1 2 12 0
1 3 1 0
1 4 0 2
1 5 11 0
Try this:
SELECT t1.user_id, t1.item_id, t1.item_number, COUNT(attribute) AS number
FROM table_1 t1
LEFT OUTER JOIN table_2 t2 ON t1.user_id = t2.user_id AND t1.item_id = t2.item_id
WHERE t1.user_id = '1'
GROUP BY t1.user_id, t1.item_id;
you have to use alias as same column names confuses the server to identify which column have to fetch..so you can use this..
SELECT t1.user_id,t1.item_id, t1.item_number, COUNT(attribute) AS number
FROM table_1 t1
LEFT OUTER JOIN table_2 t2 ON t1.user_id = t2.user_id
WHERE t1.user_id='1' GROUP BY t1.user_id, t1.item_id;
Since you don't really need any column values from table_2, I'd go with a sub-query to get the count:
select user_id, item_id, item_number,
(select count(*) from table_2
where table_1.user_id = table_2.user_id
and table_1.item_id = table_2.item_id)
from table_1
WHERE table_1.user_id='1'

Unioning and Joining in 1 Query?

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

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;