SQL GROUP BY with two tables involved
up vote
0
down vote
favorite
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
mysql sql database-table
add a comment |
up vote
0
down vote
favorite
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
mysql sql database-table
This is a simpleJOIN
+GROUP BY
. What challenges did you face in the SQL build?
– Parfait
Nov 10 at 23:54
sorry for that simple question, the main challenge probably is late night in Europe zone
– MikZ
Nov 10 at 23:57
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
mysql sql database-table
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
mysql sql database-table
mysql sql database-table
asked Nov 10 at 23:39
MikZ
344213
344213
This is a simpleJOIN
+GROUP BY
. What challenges did you face in the SQL build?
– Parfait
Nov 10 at 23:54
sorry for that simple question, the main challenge probably is late night in Europe zone
– MikZ
Nov 10 at 23:57
add a comment |
This is a simpleJOIN
+GROUP BY
. What challenges did you face in the SQL build?
– Parfait
Nov 10 at 23:54
sorry for that simple question, the main challenge probably is late night in Europe zone
– MikZ
Nov 10 at 23:57
This is a simple
JOIN
+ GROUP BY
. What challenges did you face in the SQL build?– Parfait
Nov 10 at 23:54
This is a simple
JOIN
+ GROUP BY
. What challenges did you face in the SQL build?– Parfait
Nov 10 at 23:54
sorry for that simple question, the main challenge probably is late night in Europe zone
– MikZ
Nov 10 at 23:57
sorry for that simple question, the main challenge probably is late night in Europe zone
– MikZ
Nov 10 at 23:57
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
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.
add a comment |
up vote
2
down vote
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
Sorry, TableA.User_ID should be TableA.UserID to match your example fully.
– Bobby Bridgeman
Nov 10 at 23:49
add a comment |
up vote
0
down vote
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
There is no difference of the acccepted answer, besides you forgot a.username in groupby so it will not work
– Eray Balkanli
Nov 11 at 4:29
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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.
add a comment |
up vote
2
down vote
accepted
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.
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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.
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.
answered Nov 10 at 23:43
Cetin Basoz
10.7k11526
10.7k11526
add a comment |
add a comment |
up vote
2
down vote
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
Sorry, TableA.User_ID should be TableA.UserID to match your example fully.
– Bobby Bridgeman
Nov 10 at 23:49
add a comment |
up vote
2
down vote
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
Sorry, TableA.User_ID should be TableA.UserID to match your example fully.
– Bobby Bridgeman
Nov 10 at 23:49
add a comment |
up vote
2
down vote
up vote
2
down vote
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 TableA.Username, TableA.User_ID, SUM(SessionTime) INNER JOIN
TableB ON TableA.User_ID = TableB.User_ID GROUP BY TableA.Username,
TableA.User_ID
edited Nov 10 at 23:48
Brad
9,57932865
9,57932865
answered Nov 10 at 23:46
Bobby Bridgeman
312
312
Sorry, TableA.User_ID should be TableA.UserID to match your example fully.
– Bobby Bridgeman
Nov 10 at 23:49
add a comment |
Sorry, TableA.User_ID should be TableA.UserID to match your example fully.
– Bobby Bridgeman
Nov 10 at 23:49
Sorry, TableA.User_ID should be TableA.UserID to match your example fully.
– Bobby Bridgeman
Nov 10 at 23:49
Sorry, TableA.User_ID should be TableA.UserID to match your example fully.
– Bobby Bridgeman
Nov 10 at 23:49
add a comment |
up vote
0
down vote
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
There is no difference of the acccepted answer, besides you forgot a.username in groupby so it will not work
– Eray Balkanli
Nov 11 at 4:29
add a comment |
up vote
0
down vote
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
There is no difference of the acccepted answer, besides you forgot a.username in groupby so it will not work
– Eray Balkanli
Nov 11 at 4:29
add a comment |
up vote
0
down vote
up vote
0
down vote
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
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
edited Nov 11 at 4:28
Eray Balkanli
3,79541943
3,79541943
answered Nov 11 at 4:22
Shakil Hossain
7919
7919
There is no difference of the acccepted answer, besides you forgot a.username in groupby so it will not work
– Eray Balkanli
Nov 11 at 4:29
add a comment |
There is no difference of the acccepted answer, besides you forgot a.username in groupby so it will not work
– Eray Balkanli
Nov 11 at 4:29
There is no difference of the acccepted answer, besides you forgot a.username in groupby so it will not work
– Eray Balkanli
Nov 11 at 4:29
There is no difference of the acccepted answer, besides you forgot a.username in groupby so it will not work
– Eray Balkanli
Nov 11 at 4:29
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244496%2fsql-group-by-with-two-tables-involved%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This is a simple
JOIN
+GROUP BY
. What challenges did you face in the SQL build?– Parfait
Nov 10 at 23:54
sorry for that simple question, the main challenge probably is late night in Europe zone
– MikZ
Nov 10 at 23:57