分组请问怎么相加

SELECT nvl(sum(t.QUANTITY),0) as QUANTITY,t.INSPSTATUSNAME,nvl(t1.QUANTITYt,0),t.INSPSTATUS,

TO_CHAR(to_date(t.FDATE,'yyyy-mm-dd'),'yyyy-mm') as tdata,

 case t.INSPSTATUS when 'Unqualified' then '2'

                   when 'Qualified' then '1'

when 'Waiting' then '3'

                            else '4'

          end as status,

TO_CHAR(to_date(t.FDATE,'yyyy-mm-dd'),'mm') as month,

TO_CHAR(to_date(t.FDATE,'yyyy-mm-dd'),'yyyy') as year,

SYSDATE,

nvl(t2.folt,0) as folt,nvl(t3.countfolt,0) as countfolt

FROM WMS_STORAGELIST t 

LEFT JOIN (

select   

 TO_CHAR(to_date(FDATE,'yyyy-mm-dd'),'yyyy-mm') as year_mon,  

 sum(QUANTITY) as QUANTITYt,INSPSTATUSNAME

  FROM WMS_STORAGELIST 

GROUP BY 

TO_CHAR(to_date(FDATE,'yyyy-mm-dd'),'yyyy-mm'),INSPSTATUSNAME  

)t1 on t1.INSPSTATUSNAME=t.INSPSTATUSNAME and to_char(add_months(to_date(t.FDATE,'yyyy-mm-dd'),-12),'yyyy-mm')=t1.year_mon

LEFT JOIN (

select COUNT(DISTINCT FACTORYLOTNO || WORKLOTNO) as folt,INSPSTATUSNAME,TO_CHAR(to_date(FDATE,'yyyy-mm-dd'),'yyyy-mm') as year_mon2 from WMS_STORAGELIST GROUP BY INSPSTATUSNAME,TO_CHAR(to_date(FDATE,'yyyy-mm-dd'),'yyyy-mm')

) t2

on t2.INSPSTATUSNAME=t.INSPSTATUSNAME and TO_CHAR(to_date(t.FDATE,'yyyy-mm-dd'),'yyyy-mm')=t2.year_mon2

LEFT JOIN (

select COUNT(DISTINCT FACTORYLOTNO || WORKLOTNO) as countfolt,INSPSTATUSNAME,TO_CHAR(to_date(FDATE,'yyyy-mm-dd'),'yyyy-mm') as year_mon2 from WMS_STORAGELIST GROUP BY INSPSTATUSNAME,TO_CHAR(to_date(FDATE,'yyyy-mm-dd'),'yyyy-mm')

) t3

on t3.INSPSTATUSNAME=t.INSPSTATUSNAME and to_char(add_months(to_date(t.FDATE,'yyyy-mm-dd'),-12),'yyyy-mm')=t3.year_mon2

where 

t.FDATE>='2021'

GROUP BY t.INSPSTATUSNAME,TO_CHAR(to_date(t.FDATE,'yyyy-mm-dd'),'yyyy-mm'),TO_CHAR(to_date(t.FDATE,'yyyy-mm-dd'),'mm'),

TO_CHAR(to_date(t.FDATE,'yyyy-mm-dd'),'yyyy') ,t1.QUANTITYt,t.INSPSTATUS,t2.folt,t3.countfolt

ORDER BY TO_CHAR(to_date(t.FDATE,'yyyy-mm-dd'),'yyyy-mm')

image.png

ZAW 发布于 2021-7-22 16:30
1min目标场景问卷 立即参与
回答问题
悬赏:4 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共3回答
最佳回答
0
liu.yangLv2初级互助
发布于2021-7-22 16:41(编辑于 2021-7-22 17:01)

按照图片得要求,INSPSTATUSNAME,INSPSTATUS 这两个字段都需要处理下,

都需要  case when INSPSTATUSNAME in('合格','试用') then '新值' else INSPSTATUSNAME end,

case when INSPSTATUS in('Qualified','Probational') then '新值' else INSPSTATUS end

最佳回答
0
huahua3211Lv3见习互助
发布于2021-7-22 16:48

case when INSPSTATUSNAME in ('合格','试用') then  '合格' end ,把这个字段处理一下

最佳回答
0
zjc@Lv3初级互助
发布于2021-7-22 18:32

WHERE T.FDATE >= '2021'

看到你的where条件想提醒你, 

'202001011'>'2021',不知道T.FDATE字段是否只是存的年份,如果是那没问题

  • 4关注人数
  • 553浏览人数
  • 最后回答于:2021-7-22 18:32
    请选择关闭问题的原因
    确定 取消
    返回顶部