Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

mysql - Selecting the SUM of a group as well as values of that groups largest ID

Example table below:

id locid val1 val2 val3
1 123 631 337 0
2 123 102 235 0
3 123 99 7 0
4 456 471 74 0
5 456 65 532 0
6 456 21 17 0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In case you don't have window function.and use a mysql 5.x version

SELECT 
 `locid`
, SUM(val1)
, (SELECT val2 FROM table1 WHERE id = MAX(t1.`id`)) val2
, (SELECT val3 FROm table1 WHERE id = MAX(t1.`id`)) val3
FROM table1 t1
GROUP By  `locid`

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...