查询时结果相减

想要查询时计算两个值相减,代码如下

select t.comp_code,t.khfl1_txt, t.jsl - t.jhl as kcl 

from(

SELECT  comp_code,khfl1_txt,sum(msdjsqty)/count(doc_num) AS jsl,sum(nvl(act_dl_qty,0)) AS jhl from app_yp.yc_kc_orc where jsdat <= '2022-12-20' 

) t

GROUP BY    t.comp_code,t.khfl1_txt

请问为什么会报错呢

FineReport 用户x076327 发布于 2022-12-26 14:52
1min目标场景问卷 立即参与
回答问题
悬赏:3 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共4回答
最佳回答
1
CD20160914Lv8专家互助
发布于2022-12-26 14:54(编辑于 2022-12-26 14:54)

select 

t.comp_code,

t.khfl1_txt, 

t.jsl - t.jhl as kcl 

from(

SELECT  

comp_code,

khfl1_txt,

sum(msdjsqty)/count(doc_num) AS jsl,

sum(nvl(act_dl_qty,0)) AS jhl 

from app_yp.yc_kc_orc where jsdat <= '2022-12-20' 

group by comp_code,khfl1_txt

) t

最佳回答
1
普渡众生Lv6见习互助
发布于2022-12-26 15:02

内层sql用聚合函数怎么不分组呢

最佳回答
1
wryLv4见习互助
发布于2022-12-26 15:16

select t.comp_code,t.khfl1_txt, sum(t.jsl - t.jhl) as kcl  

from(

SELECT  comp_code,khfl1_txt,sum(msdjsqty)/count(doc_num) AS jsl,sum(nvl(act_dl_qty,0)) AS jhl from app_yp.yc_kc_orc where jsdat <= '2022-12-20' 

) t

GROUP BY    t.comp_code,t.khfl1_txt

最佳回答
1
yzmaDt6d6191220Lv2见习互助
发布于2022-12-26 15:25(编辑于 2022-12-26 15:28)

select t.comp_code,t.khfl1_txt, t.jsl - t.jhl as kcl 

from(

SELECT  comp_code,khfl1_txt,sum(msdjsqty)/count(doc_num) AS jsl,sum(nvl(act_dl_qty,0)) AS jhl from app_yp.yc_kc_orc where jsdat <= '2022-12-20' 

GROUP BY    comp_code,khfl1_txt

) t

当有聚合函数出现时 后面需要跟group by,你的group by应该放在括号前

  • 5关注人数
  • 327浏览人数
  • 最后回答于:2022-12-26 15:28
    请选择关闭问题的原因
    确定 取消
    返回顶部