Sucre
Sucre

Reputation: 3

how to write a query that returns the second quarter of a year .....example total profit of second quarter from 2011 to 2016

SELECT SUM(profit),
DATE_PART('quarter',2,sales_date) AS Quarter,sales_year
FROM sales_data
GROUP BY sales_year,sales_date

this is what i have tried

Upvotes: 0

Views: 543

Answers (1)

user330315
user330315

Reputation:

You need a WHERE clause that only selects dates from the second quarter, then you need to GROUP BY the quarter, not the full date.

select sales_year, 
       date_part('quarter', sales_date) as quarter,
       sum(profit)
from sales_data
where date_part('quarter', sales_date) = 2 -- get only dates from second quarter
   and sales_date >= date '2011-01-01'
   and sales_date  < date '2017-01-01' -- only from 2011 to 2016
group by sales_year, date_part('quarter', sales_date)

Upvotes: 1

Related Questions