在数据集里怎么写能汇总带有条件的金额,这两种方法都不行

select 

h.CUSTOMER_PO_NUMBER as 客户PO,

h.ORDER_NUMBER as 生产编号,

ooot.ORDER_TYPE_NAME as 订单类型,

v.ACCOUNT_SHORT_NAME as 客户简称,

atts.suit_count as 套数,

h.order_status as 订单状态,

h.order_progress as 订单进展,

to_char(h.creation_date,'yyyy-mm-dd')as 订单创建日期,

to_char(h.BOOKED_DATE,'yyyy-mm-dd') as 订单受理日期,

to_char(h.complete_date,'yyyy-mm-dd') as 订单完工日期,

to_char(l.delivery_date,'yyyy-mm-dd') as 订单发货日期,

tv.meaning1 as 大区,

tv.meaning2 as 省区,

tv.meaning3 as 片区,

tv.MEANING as 城市,

v.ACCOUNT_NUMBER as 客户编号,

atts.STYLE_SERIES as 订单风格,

atts.PROMOTION_HEADER_ID as 促销活动,

--SUM(decode(l.item_class,'MM',nvl(l.UNIT_PRICE,0) ,0)) AS 木门, --木门

count(case when l.item_class='MM' then l.UNIT_PRICE else null end) as 木门

from

APPS.OMS_OE_ORDER_HEADERS h,

APPS.OMS_OE_ORDER_TYPES ooot,

apps.cux_om_order_headers_v v,

APPS.OMS_OE_ORDER_HEADER_ATTS atts,

apps.mdm_cust_territory_solutions_v tv,

APPS.OMS_OE_ORDER_LINES l

where 1=1

${if(len(订单类型)==0,"","and ooot.ORDER_TYPE_NAME='"+订单类型+"'")}

${if(len(订单编号)==0,"","and h.ORDER_NUMBER='"+订单编号+"'")}

${if(len(po)==0,"","and h.CUSTOMER_PO_NUMBER='"+po+"'")}

${if(len(客户简称)==0,"","and v.ACCOUNT_SHORT_NAME='"+客户简称+"'")}

${if(len(sq)==0,"","and tv.meaning2 in ('"+sq+"')")}

${if(len(BookDates)=0,""," and to_char(h.BOOKED_DATE,'yyyy-mm-dd')>='"+BookDates+"'")} 

${if(len(BookDatee)=0,""," and to_char(h.BOOKED_DATE,'yyyy-mm-dd')

${if(len(BookDatee_c)=0,""," and to_char(h.creation_date,'yyyy-mm-dd')<='"+BookDatee_c+"'")} 

and v.order_type_id=ooot.order_type_id and ooot.order_type_id=h.order_type_id

and h.header_id=atts.header_id and atts.header_id=v.header_id and v.header_id=l.header_id 

and h.UNIT_ID=tv.UNIT_ID and tv.UNIT_ID=ooot.UNIT_ID and ooot.UNIT_ID=v.UNIT_ID

and v.UNIT_ID=l.UNIT_ID

and h.ACCOUNT_SITE_ID=v.ACCOUNT_SITE_ID

and h.TERRITORY_ID=v.TERRITORY_ID and v.TERRITORY_ID=tv.TERRITORY_ID

--and ooot.CREATED_BY=atts.CREATED_BY

group by

h.CUSTOMER_PO_NUMBER ,

h.ORDER_NUMBER ,

ooot.ORDER_TYPE_NAME ,

v.ACCOUNT_SHORT_NAME,

atts.suit_count,

h.order_status,

h.order_progress,

h.creation_date,

h.BOOKED_DATE,

h.complete_date,

tv.meaning2,

l.delivery_date,

tv.meaning1,

tv.meaning3,

tv.MEANING,

v.ACCOUNT_NUMBER,

atts.STYLE_SERIES,

atts.PROMOTION_HEADER_ID

image.png

FineReport 艾琳c 发布于 2022-5-17 17:43 (编辑于 2022-5-17 17:58)
1min目标场景问卷 立即参与
回答问题
悬赏:3 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共3回答
最佳回答
0
CD20160914Lv8专家互助
发布于2022-5-17 17:46(编辑于 2022-5-17 18:03)

把你完整的sql。发你你帖子的最上面。。。

select 

t.客户PO,

t.生产编号,

t.订单类型,

t.客户简称,

t.套数,

t.订单状态,

t.订单进展,

t.订单创建日期,

t.订单受理日期,

t.订单完工日期,

t.订单发货日期,

t.大区,

t.省区,

t.片区,

t.城市,

t.客户编号,

t.订单风格,

t.促销活动,

sum(t.数量) as 数量

 from (

select 

h.CUSTOMER_PO_NUMBER as 客户PO,

h.ORDER_NUMBER as 生产编号,

ooot.ORDER_TYPE_NAME as 订单类型,

v.ACCOUNT_SHORT_NAME as 客户简称,

atts.suit_count as 套数,

h.order_status as 订单状态,

h.order_progress as 订单进展,

to_char(h.creation_date,'yyyy-mm-dd') as 订单创建日期,

to_char(h.BOOKED_DATE,'yyyy-mm-dd') as 订单受理日期,

to_char(h.complete_date,'yyyy-mm-dd') as 订单完工日期,

to_char(l.delivery_date,'yyyy-mm-dd') as 订单发货日期,

tv.meaning1 as 大区,

tv.meaning2 as 省区,

tv.meaning3 as 片区,

tv.MEANING as 城市,

v.ACCOUNT_NUMBER as 客户编号,

atts.STYLE_SERIES as 订单风格,

atts.PROMOTION_HEADER_ID as 促销活动,

case when l.item_class='MM' then nvl(l.UNIT_PRICE,0) else 0 end as 数量

from

APPS.OMS_OE_ORDER_HEADERS h,

APPS.OMS_OE_ORDER_TYPES ooot,

apps.cux_om_order_headers_v v,

APPS.OMS_OE_ORDER_HEADER_ATTS atts,

apps.mdm_cust_territory_solutions_v tv,

APPS.OMS_OE_ORDER_LINES l

where 1=1

${if(len(订单类型)==0,"","and ooot.ORDER_TYPE_NAME='"+订单类型+"'")}

${if(len(订单编号)==0,"","and h.ORDER_NUMBER='"+订单编号+"'")}

${if(len(po)==0,"","and h.CUSTOMER_PO_NUMBER='"+po+"'")}

${if(len(客户简称)==0,"","and v.ACCOUNT_SHORT_NAME='"+客户简称+"'")}

${if(len(sq)==0,"","and tv.meaning2 in ('"+sq+"')")}

${if(len(BookDates)=0,""," and to_char(h.BOOKED_DATE,'yyyy-mm-dd')>='"+BookDates+"'")} 

${if(len(BookDatee)=0,""," and to_char(h.BOOKED_DATE,'yyyy-mm-dd')<='"+BookDatee+"'")} 

${if(len(BookDates_c)=0,""," and to_char(h.creation_date,'yyyy-mm-dd')>='"+BookDates_c+"'")} 

${if(len(BookDatee_c)=0,""," and to_char(h.creation_date,'yyyy-mm-dd')<='"+BookDatee_c+"'")} 

and v.order_type_id=ooot.order_type_id and ooot.order_type_id=h.order_type_id

and h.header_id=atts.header_id and atts.header_id=v.header_id and v.header_id=l.header_id 

and h.UNIT_ID=tv.UNIT_ID and tv.UNIT_ID=ooot.UNIT_ID and ooot.UNIT_ID=v.UNIT_ID

and v.UNIT_ID=l.UNIT_ID

and h.ACCOUNT_SITE_ID=v.ACCOUNT_SITE_ID

and h.TERRITORY_ID=v.TERRITORY_ID and v.TERRITORY_ID=tv.TERRITORY_ID

) t

where 1=1

group by  t.客户PO,

t.生产编号,

t.订单类型,

t.客户简称,

t.套数,

t.订单状态,

t.订单进展,

t.订单创建日期,

t.订单受理日期,

t.订单完工日期,

t.订单发货日期,

t.大区,

t.省区,

t.片区,

t.城市,

t.客户编号,

t.订单风格,

t.促销活动

最佳回答
0
好好生活好好学习Lv7资深互助
发布于2022-5-17 17:45(编辑于 2022-5-17 17:46)

image.png

用case when 汇总

最佳回答
0
重庆一棵草Lv4中级互助
发布于2022-5-17 17:46(编辑于 2022-5-17 17:47)

sum(if(xxxx='',金额,0))image.png

  • 艾琳c 艾琳c(提问者) sum(if)会报缺失右括号的错误
    2022-05-17 17:55 
  • 重庆一棵草 重庆一棵草 回复 艾琳c(提问者) 不会啊,。你看好 sum( if(字段=值,求和字段,0 ) )。左右都是2个括号 ,不会缺失的。除非你写错了
    2022-05-17 17:56 
  • 艾琳c 艾琳c(提问者) 回复 重庆一棵草 你看我发的图片
    2022-05-17 17:58 
  • 4关注人数
  • 352浏览人数
  • 最后回答于:2022-5-17 18:03
    请选择关闭问题的原因
    确定 取消
    返回顶部