具有一定条件的求和

时间:2014-01-20 07:07:21

标签: sql

我有下表: -

enter image description here

我可以得到: -

enter image description here

使用简单的SQL:

Select Grade, sum(Qty) Qty, sum(Length) Length
from TableA
where
Serial like 'U4%'

但我需要的是: -

enter image description here

2E级是基于某些条件,即超过2级的15%津贴。工作如下: -

15% of Grade 1 => 15% x 305 = 46
Therefore, 2E is 190 (236-46).

我很新鲜&需要帮助我怎么做这个分裂?

感谢。

1 个答案:

答案 0 :(得分:0)

试试这个

Select Grade, sum(Qty) Qty, sum(Length) Length
from TableA
where
Serial like 'U4%'

Unoin All

Select * from
(
Select Grade, null , sum(Length) - ((15/100) * sum(qty)) Length
from TableA
where
Serial like 'U4%'
) AS T
where grade=2