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

Categories

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

sql - How to SUM from MySQL for every n record

I have a following result from query:

+---------------+------+------+------+------+------+------+------+-------+
| order_main_id | S36  | S37  | S38  | S39  | S40  | S41  | S42  | total |
+---------------+------+------+------+------+------+------+------+-------+
|            26 |  127 |  247 |  335 |  333 |  223 |  111 |   18 |  1394 | 
|            26 |  323 |  606 |  772 |  765 |  573 |  312 |  154 |  3505 | 
|            38 |   25 |   35 |   35 |   35 |   20 | NULL | NULL |   150 | 
|            38 |   25 |   35 |   35 |   35 |   20 | NULL | NULL |   150 | 
|            39 |   65 |   86 |   86 |   42 |   21 | NULL | NULL |   300 | 
|            39 |   42 |   58 |   58 |   28 |   14 | NULL | NULL |   200 | 
|            35 |   11 |   20 |   21 |   18 |    9 |    2 | NULL |    81 | 
|            35 |   10 |   25 |   30 |   23 |   12 |    1 | NULL |   101 | 
+---------------+------+------+------+------+------+------+------+-------+

I would like to insert a SUM before enter different order_main_id, it would be like this result:

+---------------+------+------+------+------+------+------+------+-------+
| order_main_id | S36  | S37  | S38  | S39  | S40  | S41  | S42  | total |
+---------------+------+------+------+------+------+------+------+-------+
|            26 |  127 |  247 |  335 |  333 |  223 |  111 |   18 |  1394 | 
|            26 |  323 |  606 |  772 |  765 |  573 |  312 |  154 |  3505 |
|               |  450 |  853 | 1107 | 1098 |  796 |  423 |  172 |  4899 | 
|            38 |   25 |   35 |   35 |   35 |   20 | NULL | NULL |   150 | 
|            38 |   25 |   35 |   35 |   35 |   20 | NULL | NULL |   150 |
|               |   50 |   70 |   70 |   70 |   40 | NULL | NULL |   300 | 
|            39 |   65 |   86 |   86 |   42 |   21 | NULL | NULL |   300 | 
|            39 |   42 |   58 |   58 |   28 |   14 | NULL | NULL |   200 |
|               |  107 |  144 |  144 |   70 |   35 | NULL | NULL |   500 | 
|            35 |   11 |   20 |   21 |   18 |    9 |    2 | NULL |    81 | 
|            35 |   10 |   25 |   30 |   23 |   12 |    1 | NULL |   101 |
|               |   21 |   45 |   51 |   41 |   21 |    3 | NULL |   182 | 
+---------------+------+------+------+------+------+------+------+-------+

How to make this possible ?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You'll need to write a second Query which makes use of GROUP BY order_main_id.

Something like:

SELECT sum(S41+...) FROM yourTable GROUP BY orderMainId

K


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...