如何找到用户在特定年份的旅行距离?

btqmn9zl  于 2021-06-17  发布在  Mysql
关注(0)|答案(3)|浏览(288)
UserId, SRC, DST, Mode, Dist, Year
1,CHN,IND,airplane,200,1990
2,IND,CHN,airplane,200,1991
3,IND,CHN,airplane,200,1992
4,RUS,IND,airplane,200,1990
5,CHN,RUS,airplane,200,1992
6,AUS,PAK,airplane,200,1991

我想找出一个用户在特定年份的总旅行距离。

Ex: <Id> <Distance> <year>
     1  200 1991
     1  600 1993
     2  400 1991
     2  200 1992

如何为此编写查询?

6gpjuf90

6gpjuf901#

你需要加上每年的假期 group by 条款

select holidays.id, sum(holidays.distance) as totaldistance, holidays.year 
from holidays 
group by holidays.id ,holidays.year
order by holidays.id ASC
jvidinwx

jvidinwx2#

SELECT USERID, YEAR, SUM (DIST)
FROM TABLE_USER_DISTANCE
GROUP BY USERID, YEAR;
aiazj4mn

aiazj4mn3#

您的分组是userid和year,因此:

SELECT UserID, SUM(Dist), Year
FROM table
GROUP BY UserID,Year

相关问题