如何在一年中每个月进行分组

时间:2013-08-27 07:35:30

标签: sql oracle

这是我的oracle查询..我尝试了许多,但它显示了不同的错误 我的条件是显示12个月,如果没有数据显示0值,如果数据只显示值.....

select distinct 
COUNTRY_NAME,                       
TRAN_DATE,
ROUND (nvl(sum(WALKIN_WITHOUT_CGROUP),0)/ COUNT(*),2)APC,
ROUND((nvl(SUM(SALES_VALUE),0)+nvl(sum(total_sales),0)-nvl(sum(net_sales),0)+nvl(sum(discount),0))/nvl(sum(BILLS_WITHOUT_CGROUP),0))ASPB,
ROUND((nvl(SUM(SALES_VALUE),0)+nvl(sum(total_sales),0)-nvl(sum(net_sales),0)+nvl(sum(discount),0))/COUNT(*),2) ASPD,
nvl(sum(BILLS_WITHOUT_CGROUP),0) Bill,
trunc(nvl(SUM(SALES_VALUE),0)+nvl(sum(total_sales),0)-nvl(sum(net_sales),0)+nvl(sum(discount),0)) Sale,
nvl(sum(WALKIN_WITHOUT_CGROUP),0) WALKINS
from OUTLET_PAYMODE_REPORT_FACT A,OUTLET_DETAILS B
WHERE  A.OUTLET_ID=B.OUTLET_ID and SALES_VALUE>0
and rownum <=300
group by COUNTRY_NAME,                      
TRAN_DATE

所需的输出应该是这样的:

MONTH          APC             sale
----- -------           

    01          0      0
    02          0      0
    03          0      0
    04          0      12    
    05          0      14
    06          1       0
    07          1       0
    08          0       0
    09          0       0
    10          2       0
    11          1       0
    12          0       0

2 个答案:

答案 0 :(得分:1)

您必须在select语句的列之间使用逗号

    select '01', to_char(tran_date,'mm') from OUTLET_PAYMODE_REPORT_FACT A,OUTLET_DETAILS B union all
select '02', to_char(tran_date,'mm') from OUTLET_PAYMODE_REPORT_FACT A,OUTLET_DETAILS B union all
select '03', to_char(tran_date,'mm') from OUTLET_PAYMODE_REPORT_FACT A,OUTLET_DETAILS B union all
select '04', to_char(tran_date,'mm') from OUTLET_PAYMODE_REPORT_FACT A,OUTLET_DETAILS B union all
select '05', to_char(tran_date,'mm') from OUTLET_PAYMODE_REPORT_FACT A,OUTLET_DETAILS B union all
select '06', to_char(tran_date,'mm') from OUTLET_PAYMODE_REPORT_FACT A,OUTLET_DETAILS B union all
select '07', to_char(tran_date,'mm') from OUTLET_PAYMODE_REPORT_FACT A,OUTLET_DETAILS B union all
select '08', to_char(tran_date,'mm') from OUTLET_PAYMODE_REPORT_FACT A,OUTLET_DETAILS B union all
select '09', to_char(tran_date,'mm') from OUTLET_PAYMODE_REPORT_FACT A,OUTLET_DETAILS B union all
select '10', to_char(tran_date,'mm') from OUTLET_PAYMODE_REPORT_FACT A,OUTLET_DETAILS B union all
select '11', to_char(tran_date,'mm') from OUTLET_PAYMODE_REPORT_FACT A,OUTLET_DETAILS B union all
select '12', to_char(tran_date,'mm') from OUTLET_PAYMODE_REPORT_FACT

答案 1 :(得分:1)

根据我的经验,10个案例中有8个

ORA-00923: FROM keyword not found where expected

错误是由于缺少逗号