描述: 数据库字段
pk_group pk_org pk_order orderstate ordertype money
现在 需要
************************************************************************************************
当 orderstate =3 并且 ordertype !=4 时,统计所有 money 之和 即 sum(money )
同时
当 ordertype !=4 时,统计所有 money 之和 即 sum(money )
并且根据 pk_group pk_org pk_order 分组
*************************************************************************************************
我写的:
select t.pk_group as pk_group,
t.pk_org as pk_org,
t.pk_currtype as corigcurrencyid,
t.pk_order as pk_order
case
WHEN orderstate =3 and ordertype !=4 THEN sum(money ) as A
WHEN ordertype !=4 THEN sum(money ) as B
from table t
group by t.pk_order,
t.pk_group,
t.pk_org
求各位大神帮忙 修改一下 多谢
------解决思路----------------------
select t.pk_group as pk_group,
t.pk_org as pk_org,
t.pk_currtype as corigcurrencyid,
t.pk_order as pk_order,
SUM(case
WHEN orderstate=3 and ordertype!=4 THEN money
WHEN ordertype!=4 THEN money END) AS A
from table t
group by t.pk_order,
t.pk_group,
t.pk_org
------解决思路----------------------
恩,同意楼上意见,也可以使用max
select t.pk_group as pk_group,
t.pk_org as pk_org,
max(t.pk_currtype) as corigcurrencyid,
t.pk_order as pk_order,
SUM(case
WHEN orderstate=3 and ordertype!=4 THEN money
WHEN ordertype!=4 THEN money END) AS A
from table t
group by t.pk_order,
t.pk_group,
t.pk_org
------解决思路----------------------
select t.pk_group as pk_group,
t.pk_org as pk_org,
t.pk_order as pk_order,
SUM(case
WHEN orderstate=3 and ordertype!=4 THEN money
WHEN ordertype=4 THEN money END) AS CNT
from table t
group by t.pk_order,
t.pk_group,
t.pk_org
------解决思路----------------------
同意