如何从每个n记录的MySQL中求和

时间:2009-08-28 10:17:01

标签: sql mysql

我的查询结果如下:

+---------------+------+------+------+------+------+------+------+-------+
| 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 | 
+---------------+------+------+------+------+------+------+------+-------+

我想在输入不同的order_main_id之前插入SUM,就像这样的结果:

+---------------+------+------+------+------+------+------+------+-------+
| 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 | 
+---------------+------+------+------+------+------+------+------+-------+

如何实现这一目标?

3 个答案:

答案 0 :(得分:4)

您需要编写第二个Query,它使用GROUP BY order_main_id。

类似的东西:

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

ķ

答案 1 :(得分:2)

您实际上可以在一个查询中执行此操作,但使用union all(实际上是两个查询,但结果集合在一起以生成一个非常棒的结果集):

select
    order_main_id,
    S36,
    S37,
    S38,
    S39,
    S40,
    S41,
    S42,
    S36 + S37 + S38 + S39 + S40 + S41 + S42 as total,
    'Detail' as rowtype
from
    tblA
union all
select
    order_main_id,
    sum(S36),
    sum(S37),
    sum(S38),
    sum(S39),
    sum(S40),
    sum(S41),
    sum(S42),
    sum(S36 + S37 + S38 + S39 + S40 + S41 + S42),
    'Summary' as rowtype
from
    tblA
group by
    order_main_id
order by
    order_main_id, RowType

请记住,order by会影响union all的整体,而不仅仅是最后一个查询。所以,你的结果集看起来像这样:

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

这样,您就知道什么是什么,什么不是详细信息或摘要行,以及它的order_main_id。您可以随时(也可能应该)在表示层中隐藏此列。

答案 2 :(得分:0)

对于类似这样的事情,我认为您应该使用报告库(例如Crystal Reports),这样可以省去很多麻烦,请查看JasperReports and similar projects on osalt