Skip to content
Advertisement

Sql Query issue and error regarding groupby cause

I am trying to calculate the total number of Projects in every year. and also how many projects are active, how many of them are canceled.

I tried to group by cause for PRojects dates so we have a total number of project in every year but I am not sure where to start and what to do

+ -------------+--------+-----------+------------+----------+-----------------+
| New Projects | Active | Cancelled | Terminated | Inactive | Carried Forward |
+ -------------+--------+-----------+------------+----------+-----------------+
| 2013         |     32 |       45  |          4 |       11 |              30 |
| 2014         |     45 |       75  |         17 |       14 |              44 |
| 2015         |     46 |       90  |         25 |       21 |              44 |
| 2016         |     30 |       74  |         27 |       10 |              37 |
| 2017         |     82 |      119  |         11 |       26 |              82 |
| 2018         |     86 |      168  |         29 |       24 |             115 |
| 2019         |     23 |      138  |          9 |        4 |             125 |
+ -------------+--------+-----------+------------+----------+-----------------+

Advertisement

Answer

You want one result row per year. So group by year. You get it via YEAR or DATEPART. Then count conditionally:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement