Oracle分组不能使用as 后别名
SELECT
min( t.batch_id ) as minBatchNo,
to_char ( to_date ( t.tdate, 'yyyymmdd' ), 'yyyymm' ) as yymm
FROM
table1 left join (select * from table2 where file_type != '0') f0 on t.batch_id = f0.batch_no
WHERE 1=1
GROUP BY
-- 分组,不能直接使用上面的as yymm,要用实际的字段做聚合
to_char ( to_date ( t.tdate, 'yyyymmdd' ), 'yyyymm' )
-- having 过滤作用,区别于where,可以放在group by后面做数据过滤。
-- to_char(trunc(sysdate,'MONTH'),'yyyymmdd') 获取当月的第一天,并转换成字符串格式
having min(t.batch_id) < (select to_char(trunc(sysdate,'MONTH'),'yyyymmdd') First_DayOfMonth from dual)
-- 取当月最后一天,并转成字符串
to_char(last_day(trunc(sysdate,'MONTH')),'yyyy-mm-dd')