在我的表 customer_records 中,有一列 StartDate 具有DDMMYYYYHHMMSS
格式值(列数据类型为 VARCHAR)。
我想按YYYYMM
格式从表组中提取数据。
我有以下工作查询:它将以MMYYYY
格式提取数据。
SELECT /*+ PARALLEL(a , 8 ) */
NVL (SUBSTR (StartDate, 3, 6), 'Total') "Year-Month",Started",
TO_CHAR (COUNT (1), '999,999,999') "Customer Count",
TO_CHAR (
SUM (Usage)/60,
'999,999,999')`enter code here`
"Duration (M)"
FROM customer_records a
WHERE type='NEW'
and customer_status=0
GROUP BY ROLLUP (to_date(SUBSTR (StartDate, 3, 6),'MMYYYY'), 'Total')
ORDER BY 1;
但我希望日期格式采用YYYYMM
格式。
我尝试了以下选项。但不工作,
SELECT /*+ PARALLEL(a , 8 ) */
NVL (to_date(SUBSTR (StartDate, 3, 6),'MMYYYY'), 'Total') "Month Started",
TO_CHAR (COUNT (1), '999,999,999') "Customer Count",
TO_CHAR (
SUM (Usage)/60,
'999,999,999')
"Duration (M)"
FROM customer_records a
WHERE type='NEW'
and customer_status=0
GROUP BY ROLLUP (to_date(SUBSTR (StartDate, 3, 6),'MMYYYY'), 'Total')
ORDER BY 1;
有人可以帮我编辑上面的查询以获取YYYYMM
格式的数据吗?
如果给定的 startdate 是日期格式,那么下面的格式会很好用。
更新:正如您在评论中提到的,它不是 datw 格式,然后您首先将其转换为日期格式,然后使用 to_char 函数需要 YYYYMM 格式
SELECT /*+ PARALLEL(a , 8 ) */
NVL ( to_CHAR(to_date(substr(StartDate, 1,8),'DDMMYYYY'),'YYYYMM'),'Total') "Month Started",
TO_CHAR (COUNT (1), '999,999,999') "Customer Count",
TO_CHAR (
SUM (Usage)/60,
'999,999,999')
"Duration (M)"
FROM customer_records a
WHERE type='NEW'
and customer_status=0
GROUP BY ROLLUP (to_CHAR(to_date(substr(StartDate, 1,8),'DDMMYYYY'),'YYYYMM'))
ORDER BY 1;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句