Search code examples
sqloracleoracle-sqldeveloper

Is there way to display record month by month?


So.. I am trying to make record display by month by month.

For example,

    select X, Y, Z, (X + Y + Z) as total
from (
  select
    (select count(x) from table1 a, table2 b where date between '2020-01-01' and '2020-05-01') as X,
    (select count(y) from table3 a, table4 b where date between '2020-01-01' and '2020-05-01') as Y,
    (select count(z) from table5 a, table6 b where date between '2020-01-01' and '2020-05-01') as Z
  from dual
);

This will display count of x between those date range but if I display it like 2 columns, like first column display month, second column display X and display it month by month. So result would be like below. Is this doable in sql?

Month    ||     X      ||     Y     ||     Z     ||   Total
January  ||    125     ||    133    ||    155    ||    413
February ||    150     ||    123    ||    129    ||    402
March    ||    170     ||    177    ||    155    ||    502
....
....

Solution

  • You just need to group by month

    Select count(X), TO_CHAR(PDATE , 'MONTH') AS Month_name
    
    From TableA
    
    Where PDATE between '2020-01-01' to '2020-01-31'
    group by TO_CHAR(PDATE , 'MONTH')