sql - How to group by a set of numbers in a column -


i have table below. want group in such way 1-4 weeknums joined , 5-8 weeknums joined together. or in other words want monthly total below fields

table1  weeknum    amount      1          1000      2          1100      3          1200      4          1300      5          1400      6          1500      7          1600      8          1700    

the output need below

output max(weeknum)       sum(amount) 4                   4600 8                   6200 

the below answer did not work actual values below. want start 4 weeks grouping. formula (weeknum-1)/4 returns 3 groups in expected 2

weeknum group expr    expected group expr  1855   463           463      1856   463           463 1857   464           463 1858   464           463 1859   464           464  1860   464           464 1861   465           464  1862   465           464  

need execute query in oracle

try using floor rounds number down in group by clause:

select max(t.weeknum),sum(amount) table1 t group floor((t.weeknum-1)/4) 

this make sure every 4 weeks treated group :

(1-1)/4 -> 0 (2-1)/4 -> 0 ... (5-1)/4 -> 1 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -