我正在使用 Postgres 9.4。我有以下查询:
select sum(items) as items, sum(cost) as cost,
presentation_code, processing_date as date
from vw_presentation_summary
where presentation_code LIKE '011%' OR presentation_code LIKE '012%'
GROUP BY date, presentation_code;
Run Code Online (Sandbox Code Playgroud)
这给了我这样的结果:
items | cost | presentation_code | date
-------+------------------+-------------------+------------
8243 | 273445.45 | 0212000AAAAACAC | 2014-03-01
366 | 10511.99 | 0212000AABBABAB | 2013-05-01
461 | 9232.04 | 0212000AABBADAD | 2014-02-01
Run Code Online (Sandbox Code Playgroud)
但是有什么办法可以将所有结果011%组合在一起,并将所有结果012%组合在一起?
您必须将 select 和 group by 元素中的“presentation_code”替换为以下表达式:
substr(presentation_code,1,3)
Run Code Online (Sandbox Code Playgroud)