oracle sql,如何将日值分组到月份摘要中

oxf4rvwz  于 2021-08-01  发布在  Java
关注(0)|答案(1)|浏览(297)

想知道是否有人能帮忙。
我试着把日期列中的值,天的值,分组成一个月,得到一个月收到的托盘总数的摘要。
在select/group语句中添加到\u char(conf\u date,'mm yyyy')时,我得到一个错误'ora-01722:无效数字'。
提前谢谢!
我当前的sql

  1. with pallets_received as (
  2. select a.comp_code,
  3. a.cust_code,
  4. a.rcpt_num,
  5. to_char(a.rcpt_conf_date,'mm-dd-yyyy')conf_date,
  6. sum(b.rcpt_loc_qty)/(c.item_qty_bkd_qty) pallets
  7. from e_rcpt_h a
  8. left join e_rcpt_d5D1 b
  9. on a.comp_code=b.comp_code
  10. and a.rcpt_num=b.rcpt_num
  11. left join m_item_d1 c
  12. on b.comp_code=c.comp_code
  13. and b.cust_code=c.cust_code
  14. and b.invt_lev1=c.item_code
  15. where a.comp_code='T3'
  16. and c.item_qty_bkd_lev_num=1
  17. and a.cust_code='101029'
  18. and a.flow_pros_code='CORE'
  19. and trunc(a.rcpt_conf_date) >= to_date('02-01-2020','mm-dd-yyyy')
  20. group by a.comp_code,
  21. a.cust_code,
  22. a.rcpt_num,
  23. a.rcpt_conf_date,
  24. c.item_qty_bkd_qty
  25. order by conf_date
  26. )
  27. select comp_code,
  28. cust_code,
  29. conf_dateas month,
  30. sum(ceil(pallets)) pallets
  31. from pallets_received
  32. group by comp_code,
  33. cust_code,
  34. conf_date
  35. order by conf_date

table

  1. comp_code | cust_code | conf_date | pallets
  2. ---------------------------------------------
  3. T3 101029 03-06-2020 2
  4. T3 101029 03-09-2020 8
  5. C4 101029 04-05-2020 2
  6. C4 101029 04-05-2020 8

我努力实现的产出

  1. comp_code | cust_code | month | pallets
  2. ---------------------------------------------
  3. T3 101029 03-2020 10
  4. C4 101029 04-2020 10
hwamh0ep

hwamh0ep1#

conf\u date只是字符串结果“to\u char(a.rcpt\u conf\u date,'mm-dd-yyyy')的别名,因此conf\u date实际上只是一个字符串。然而,在您的组中,您正在将其喂给\u char。这迫使oracle首先使用nls\ U date\格式的控制值来执行隐含的to\ U date。几乎可以保证返回错误。

  1. group by comp_code,
  2. cust_code,
  3. trunc(rcpt_conf_date,'MON')

(是的,您需要学习格式化代码。。为了你自己的理智,也为了其他人的理智。)

相关问题