While grouped by year:

date_part('year', redireсt_log.created) as year ... group by year 

In the final sample there is both data for 2015, and for 2016. Is it possible to display them together using SQL?

UPD: I get

 COUNT("redirect_log", "id", "clicks"). 
  • Do you mean to sum up in 2 years? - Firepro
  • What does grouping mean? Suppose there is a group 2015-2016 and how then other groups? On what grounds do you want to build groups? - nick_n_a
  • It is possible for even years, for example, by making or. - nick_n_a
  • Firepro, yes, in general for 2 years as a result (so far they are) .. - Timur Musharapov
  • one
    Total quantity in time - no need to group. The selection condition will work much faster, for example, `where date_part ('year', redirect_log.created) in (2015,2016)` - nick_n_a

1 answer 1

If you want to summarize the sample for 2 years, just set the boundaries of dates through WHERE year IN (2015, 2016) or year> 2015 (although in your case you can not set if you have only 2 years sample in the database) and remove the condition GROUP BY, and using aggregate functions (COUNT, SUM, etc.) will allow you to get a result over the entire interval.

  • "Is it possible to display them together using SQL tools": together - it’s in one field to summarize their data, for 2 years, otherwise I get 2 fields :) - Timur Musharapov
  • Do you have a second year field? If you remove the grouping, then you can and date_part field ('year', redirect_log.created) as year - remove) - Firepro
  • Then there will be hundreds of ungrouped data for each day, for 2 years. I need one field, the total amount for all time. - Timur Musharapov
  • Timur, If you call any aggregation function, in your case COUNT (*) in SELECT, then you will have only one line at output, no matter how many lines there are :) - Firepro
  • column "redirect_log.created" must appear in the aggregate function :( - Timur Musharapov