Hi All,
I have a dataset which has information about students and their registered courses. following is a temp table with a sample structure and data.
DROP TABLE IF EXISTS #StudentData
CREATE TABLE #StudentData (StudentID INT,CourseType VARCHAR(50),CourseID INT,SubscriptionStartDate DATE, SubscriptioneEndDate DATE)
INSERT INTO #StudentData VALUES (1,'Basic',50,'10-15-2016','10-15-2017')
INSERT INTO #StudentData VALUES (1,'Advanced',72,'10-15-2016','2018-12-31')
INSERT INTO #StudentData VALUES (1,'Basic',50,'11-13-2017','12-31-2018')
INSERT INTO #StudentData VALUES (1,'Intermediate',66,'01-10-2017','07-30-2017')
INSERT INTO #StudentData VALUES (1,'Intermediate',68,'08-01-2017','12-30-2017')
INSERT INTO #StudentData VALUES (1,'Advanced',85,'01-10-2019',NULL)
INSERT INTO #StudentData VALUES (1,'Basic',57,'10-15-2020','10-15-2021')
SELECT * FROM #StudentData
ORDER BY CourseType,SubscriptionStartDate
I am trying to create a query that can group the data for different bands(based on the start and end dates), folloiwng is the desired output for the sample provided above.
DROP TABLE IF EXISTS #DesiredOutput
CREATE TABLE #DesiredOutput (StudentID INT,CourseType VARCHAR(50),[16-17] VARCHAR(50),[17-18] VARCHAR(50),[18-19] VARCHAR(50),[19-20] VARCHAR(50),[20-21] VARCHAR(50))
INSERT INTO #DesiredOutput VALUES (1,'Basic',50,50,null,null,57)
INSERT INTO #DesiredOutput VALUES (1,'Intermediate',null,'66,68',null,null,null)
INSERT INTO #DesiredOutput VALUES (1,'Advanced',72,72,null,85,85)
SELECT * FROM #DesiredOutput
I tried Pivot and was trying to use case whens but had no luck and hoping someone can help me. any help is highly appreciated.
Thanks in advance.