使用大小写提取sql,使用零行返回零月份

时间:2017-11-16 21:15:27

标签: sql oracle

尝试列出每个月计算记录的最近12个月,并希望包括未找到记录的月份,在该月份名称旁边显示0。我尝试包括另一个表,它总是至少有一个记录,所有12个月和案例从他们提取月份,但我知道我做错了。我所做的所有研究都没有使用案例提取,如果我可以的话,我希望保留它。如果我不能,那就开放。

当前查询有效且是:

select count(RMA.id) "Number of RMAs", case extract(month from 
RMA.RMA_DATE)when 1 then 'January'
when 2 then 'February'
when 3 then 'March'
when 4 then 'April'
when 5 then 'May'
when 6 then 'June'
when 7 then 'July'
when 8 then 'August'
when 9 then 'September'
when 10 then 'October'
when 11 then 'November'
when 12 then 'December'
else 'error'
end, extract(year from RMA.rma_date)
from RMA,
    RMA_DETAIL
where RMA.ID = RMA_DETAIL.RMA_ID And
       RMA.rma_date >= sysdate - 365 And
       RMA_DETAIL.ID <> 3
group by extract(month from RMA.rma_date), extract(year from RMA.rma_date)
order by extract(year from RMA.rma_date), extract(month from RMA.rma_date)

结果

current query

1 个答案:

答案 0 :(得分:0)

SUM个案

如果您已经确定了查询中的月份,那么将它们总结起来可能是值得的。 计数只能计算表格中的内容。如果您总结了解外观实例,那么您的月份将会显示为0。考虑这样的事情:

SELECT
    SUM(CASE WHEN EXTRACT(month from RMA.RMA_DATE) = '1' THEN 1 ELSE 0) AS January
    ,SUM(CASE WHEN EXTRACT(month from RMA.RMA_DATE) = '2' THEN 1 ELSE 0) AS February
    ,SUM(CASE WHEN EXTRACT(month from RMA.RMA_DATE) = '3' THEN 1 ELSE 0) AS March
    ,SUM(CASE WHEN EXTRACT(month from RMA.RMA_DATE) = '4' THEN 1 ELSE 0) AS April
    ,SUM(CASE WHEN EXTRACT(month from RMA.RMA_DATE) = '5' THEN 1 ELSE 0) AS May
    ,SUM(CASE WHEN EXTRACT(month from RMA.RMA_DATE) = '6' THEN 1 ELSE 0) AS June
    ,SUM(CASE WHEN EXTRACT(month from RMA.RMA_DATE) = '7' THEN 1 ELSE 0) AS July
    ,SUM(CASE WHEN EXTRACT(month from RMA.RMA_DATE) = '8' THEN 1 ELSE 0) AS August
    ,SUM(CASE WHEN EXTRACT(month from RMA.RMA_DATE) = '9' THEN 1 ELSE 0) AS September
    ,SUM(CASE WHEN EXTRACT(month from RMA.RMA_DATE) = '10' THEN 1 ELSE 0) AS October
    ,SUM(CASE WHEN EXTRACT(month from RMA.RMA_DATE) = '11' THEN 1 ELSE 0) AS November
    ,SUM(CASE WHEN EXTRACT(month from RMA.RMA_DATE) = '12' THEN 1 ELSE 0) AS December
...