I have the following dataset:
startDate endDate pointsAchieved
01/07/2013 30/06/2014 0
05/10/2014 30/06/2015 0
01/07/2015 30/06/2016 0
01/07/2016 30/06/2017 1
01/07/2017 30/06/2018 1
30/06/2018 30/06/2019 0
10/12/2019 30/06/2020 0
07/06/2021 30/06/2022 1
01/07/2022 30/06/2023 0
I want to group the data only showing the first date of the points not achieved and last date of points not achieved then doing the same for the pointsAchieved and it must be sorted by startDate, here is an example of the required output:
startDate endDate pointsAchieved
01/07/2013 30/06/2016 0
01/07/2016 30/06/2018 1
01/07/2018 30/06/2020 0
07/06/2021 30/06/2022 1
01/07/2022 30/06/2023 0
Is this possible to do using T-SQL? The purpose of this grouping is to show the user they have a break in their enrolment into a CPD program on a certificate as they haven't achieved their points the previous year.
The resulting text on the certificate will look something like this:
Jul 2013 - Jun 2016 (Points not achieved)
Jul 2016 - Jun 2018 (Points achieved)
Jul 2018 - Jun 2020 (Points not achieved)
Jun 2021 - Jun 2022 (Points achieved)
Jul 2022 - Jun 2023 (Points not achieved)
I have tried the following T-SQL but obviously not correct:
select Min(startDate) as startDate, Max(endDate) as endDate, pointsAchieved
from cpd_enrolments
where userId=XXXX
group by pointsAchieved
order by startDate
As it produces this result:
startDate endDate pointsAchieved
01/07/2013 30/06/2023 0
01/07/2016 30/06/2022 1
Any help would be much appreciated :)
2条答案
按热度按时间vs3odd8k1#
Creating test data as per your example:
You can achieve the desired result with window functions:
Output:
jyztefdp2#
fiddle