按列

时间:2017-05-15 12:27:48

标签: sql oracle group-by

我正在编写一个SQL查询

SELECT                      
    pj.PROJECT_ID,                      
    SUM(hr.HOURS) as totalhours,                      
    hr.week,    
    hr.week as YEAR_Week,    
    max(rl.IHC_OR_EXTERNAL) as IHC,      
    max(rl.COMPANY_NAME) as companyName,       
    max(pj.PROJECT_SEGMENT) as segment,             
    max(hr.YEAR) as year,                      
    max(pj.PROJECT_STATUS) as PROJECT_STATUS,                      
    max(hr.RESOURCE_ID) as resourceId,                      
    max(hr.HOUR_TYPE) as hourType                    
FROM                      
    HOURS hr
    join PROJECT pj on hr.PROJECT_ID=pj.PROJECT_ID             
    join RESOURCE_LIST rl on hr.RESOURCE_ID=rl.RESOURCE_ID            
GROUP BY
    pj.PROJECT_ID,
    hr.WEEK 

是否可以在中间用连字符显示组。

我正在显示一个基于此的图表。按照201401,201402等数据分组。这是一周。我想要显示一周。这可能。

在选择查询中,我可以作为

   regexp_replace(week, '(\d{4})(\d{2})', '\1-\2') as new_week

但我怎样才能在...中显示组?

1 个答案:

答案 0 :(得分:1)

您不需要在GROUP BY子句中更改任何内容,因为在分组后可以应用添加连字符。

with demo (week, total) as
     ( select '201701', 1 from dual union all
       select '201701', 1 from dual union all
       select '201701', 1 from dual union all
       select '201702', 1 from dual union all
       select '201702', 1 from dual )
select regexp_replace(week, '(\d{4})(\d{2})', '\1-\2') as new_week
     , sum(total)
from   demo
group by week;

NEW_WEEK     SUM(TOTAL)
------------ ----------
2017-01               3
2017-02               2