简体   繁体   English

Oracle Sql,如何将日值分组为一个月摘要

[英]Oracle Sql, How do I group day values into a months summary

wondering if someone can assist.想知道是否有人可以提供帮助。

I'm trying to take the values in a date column, the day values and grouping the days into a month to get a summary of total pallets received for the month.我正在尝试获取日期列中的值、日期值并将天分组为一个月,以获取当月收到的托盘总数的摘要。

I'm getting an error, 'ORA-01722: invalid number' when adding to_char(conf_date, 'MM-YYYY') to my select/group statement.将 to_char(conf_date, 'MM-YYYY') 添加到我的 select/group 语句时出现错误,'ORA-01722: invalid number'。

Thanks in advance!提前致谢!

My Current SQL我的当前 SQL

with pallets_received as (
select a.comp_code, 
       a.cust_code, 
       a.rcpt_num, 
       to_char(a.rcpt_conf_date,'mm-dd-yyyy')conf_date,
       sum(b.rcpt_loc_qty)/(c.item_qty_bkd_qty) pallets
from e_rcpt_h a
left join e_rcpt_d5D1 b
       on a.comp_code=b.comp_code 
       and a.rcpt_num=b.rcpt_num
left join m_item_d1 c
       on b.comp_code=c.comp_code 
       and b.cust_code=c.cust_code 
       and b.invt_lev1=c.item_code
where a.comp_code='T3'
       and c.item_qty_bkd_lev_num=1
       and a.cust_code='101029'
       and a.flow_pros_code='CORE'
       and trunc(a.rcpt_conf_date) >= to_date('02-01-2020','mm-dd-yyyy')
group by a.comp_code, 
         a.cust_code, 
         a.rcpt_num, 
         a.rcpt_conf_date, 
         c.item_qty_bkd_qty
order by conf_date
)
select comp_code, 
       cust_code, 
       conf_dateas month, 
       sum(ceil(pallets)) pallets
from pallets_received
group by comp_code, 
         cust_code, 
         conf_date
order by conf_date

Table桌子

comp_code | cust_code | conf_date  | pallets
---------------------------------------------
T3          101029      03-06-2020   2
T3          101029      03-09-2020   8
C4          101029      04-05-2020   2
C4          101029      04-05-2020   8

Output I'm trying to achieve Output 我正在努力实现

comp_code | cust_code | month   | pallets
---------------------------------------------
T3          101029      03-2020   10
C4          101029      04-2020   10

CONF_DATE is just an alias for the string result of 'to_char(a.rcpt_conf_date,'mm-dd-yyyy')' Therefore CONF_DATE is effectively just a string. CONF_DATE 只是 'to_char(a.rcpt_conf_date,'mm-dd-yyyy')' 的字符串结果的别名,因此 CONF_DATE 实际上只是一个字符串。 Yet, in your GROUP BY, you are feeding it to TO_CHAR.然而,在您的 GROUP BY 中,您将其提供给 TO_CHAR。 This forces oracle to first do an implied TO_DATE, using the controlling value of NLS_DATE_FORMAT.这会强制 oracle 首先使用 NLS_DATE_FORMAT 的控制值执行隐含的 TO_DATE。 That is almost guaranteed to return an error.这几乎可以保证返回错误。

group by comp_code, 
         cust_code, 
         trunc(rcpt_conf_date,'MON')

(yes, you need to learn to format your code.. for your own sanity as well as that of others who have to read it.) (是的,你需要学习格式化你的代码......为了你自己以及其他必须阅读它的人的理智。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Oracle SQL 19 中使用基于组的所有行填写 null 值? - How do I fill in null values with all rows based on group in Oracle SQL 19? 如何获取组中最新日期对应的值以填写 Oracle SQL 中的 null 值? - How do I get value corresponding to latest date in group to fill in null values in Oracle SQL? 如何获得 SQL 中周期性月份的最后一天? - How can I get the last day of periodic months in SQL? 如何在 select oracle 中的单行上对值进行分组 - How do I group values ​on a single line in select oracle 如何在 oracle sql 中获得 group by 的总和? - How do I get the sum of group by in oracle sql? 如何在包含GROUP BY的SQL查询中将月份从数字转换为字符串形式? - How do I convert Months from Numeral to String Form in SQL Query Containing GROUP BY? 当 SQL 中有多个继续 Null 值时,如何根据前几个月的值填充 Null 值 - How do I populate the Null Values based on Previous months value, when there are multiple continues Null values in SQL ORACLE SQL - 如何在教师辞职前2个月每天查找每位教师的救济人数? - ORACLE SQL - How to find the number of reliefs each teacher has, each day, 2 months before the teacher resigned? 如何返回 Oracle SQL 中确定时间的 2 个月前的第 14 天? - How to return 14th day from month which was 2 months ago with determinated time in Oracle SQL? 我无法按以下方式进行Oracle SQL分组 - Oracle SQL I cannot do group by
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM