有点像Oracle SQL newby,我有一个小难题。
我想从“WEIGHT”栏中总结一个月的数据。 START_DATESTAMP最初的格式为'01 / 01/1900 00:00:00',我发现使用'trunc'会删除时间组件。
因此,下面的查询返回每天总计权重 ,但我想返回每月总计权重 。有什么想法吗?
SELECT
CODE, trunc(START_DATESTAMP), sum(WEIGHT) as TOTAL_WEIGHT
FROM TP.LOADING Tbl
where CODE like 'ALM'
and trunc(START_DATESTAMP) >= to_date('01/11/2014', 'DD/MM/YYYY')
and trunc(START_DATESTAMP) < to_date('01/12/2014', 'DD/MM/YYYY')
group by CODE, trunc(START_DATESTAMP)
ORDER BY CODE asc, trunc(START_DATESTAMP) asc
答案 0 :(得分:1)
您可以尝试以下内容:
SELECT CODE, to_char(START_DATESTAMP, 'YYYY/MM') as MONTH, sum(WEIGHT) as TOTAL_WEIGHT
FROM LOADING Tbl
where CODE like 'ALM'
and trunc(START_DATESTAMP) >= to_date('01/01/2014', 'DD/MM/YYYY')
and trunc(START_DATESTAMP) < to_date('01/12/2014', 'DD/MM/YYYY')
group by CODE, to_char(START_DATESTAMP, 'YYYY/MM')
ORDER BY CODE asc, MONTH asc
相应地调整日期检查。您没有说出您在不同年份想要做几个月的事情 - 但您可以转换为纯月份字符串,或者仅使用EXTRACT函数进行数月。
答案 1 :(得分:0)
查看trunc formats的文档。如果你想按月截断:
group by CODE, trunc(START_DATESTAMP, 'MM')