Question

What I'm trying to do is to get an amount for each month.

Would this work?

SELECT @location
       ,sum(x.amount) as Tot_amount
FROM this_table2 y

    INNER JOIN 
       (SELECT 
            SUM(x.amount)  
        FROM 
            this_table x 
        WHERE 
            x.date BETWEEN '1/1/14' AND '1/31/14') January_tot

LEFT JOIN this_table2 y
       on x.no = y.no
WHERE y.desc = 'INCOME' AND x.date between '1/1/14' and 'now 

And if that does work, is there a way to set a parameter for the year? Also, how would I put the where clause after this?

Was it helpful?

Solution

I found out how to do it, the date_part function is essential for this.

select @location
      ,sum(x.amount) as Tot_amount
      ,sum(case when date_part('month', x.date) = 1 and date_part('year', x.date) = 2014 then amount end) as January_Tot
      sum(case when date_part('month', x.date) = 2 and date_part('year', x.date) = 2014 then amount end) as February_Tot

left join this_table2 y
      on x.no = y.no
where y.desc = 'INCOME' and x.date between '1/1/14' and current_date
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top