Group and calculate sum with joined table - mysql

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?

Related

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

How to sum a specific row from one table to another in SQL Server

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'

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'

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;

Select FROM 3 Table

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