在线求一条SQL语句 case when 的

兔子侠客 2014-10-27 09:08:23
描述: 数据库字段

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



求各位大神帮忙 修改一下 多谢
...全文
200 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
华而不实 2014-10-30
  • 打赏
  • 举报
回复
引用 4 楼 skyxuyan 的回复:
[quote=引用 1 楼 bw555 的回复:]
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
多谢啦 这样就好了

select t.pk_group as pk_group,
       t.pk_org as pk_org,
       
       t.pk_order as pk_order,
SUM(case  
  WHEN ORDERSTATUS=3 and T.ordertype!=4 THEN money   END)AS AA,
 
SUM( case WHEN ORDERTYPE=4 THEN money  END)AS BB
from  table1  t
group by t.pk_order,
          t.pk_group,
          t.pk_org
[/quote] 同意
L-B-J 2014-10-29
  • 打赏
  • 举报
回复
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
卖水果的net 2014-10-28
  • 打赏
  • 举报
回复
sum 拿到外面。 group by 的列表和 select 中的未使用聚合函数的列,保持一致。
bw555 2014-10-28
  • 打赏
  • 举报
回复
引用 5 楼 yuyeyi 的回复:
ordertype !=4 不是包含了 orderstate =3 并且 ordertype !=4 吗?
是笔误吧 第二个应该是ORDERTYPE=4
yuyeyi 2014-10-28
  • 打赏
  • 举报
回复
ordertype !=4 不是包含了 orderstate =3 并且 ordertype !=4 吗?
兔子侠客 2014-10-27
  • 打赏
  • 举报
回复
引用 1 楼 bw555 的回复:
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
多谢啦 这样就好了

select t.pk_group as pk_group,
       t.pk_org as pk_org,
       
       t.pk_order as pk_order,
SUM(case  
  WHEN ORDERSTATUS=3 and T.ordertype!=4 THEN money   END)AS AA,
 
SUM( case WHEN ORDERTYPE=4 THEN money  END)AS BB
from  table1  t
group by t.pk_order,
          t.pk_group,
          t.pk_org
bw555 2014-10-27
  • 打赏
  • 举报
回复
恩,同意楼上意见,也可以使用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
CT_LXL 2014-10-27
  • 打赏
  • 举报
回复
t.pk_currtype 应该加到group by中,不然要报错
bw555 2014-10-27
  • 打赏
  • 举报
回复
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

17,382

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧