|
Question : SQL View to show the data using PIVOT
|
|
I have a data in the format (see the attached file).
I wish to transform the data using a view to display the information in the following format:
WEEKDAY_SCHEDULE, PERIOD_SESSION(ie I Period),PERIOD_SESSION(ie II Period),PERIOD_SESSION(ie III Period), PERIOD_SESSION(ie IV Period),PERIOD_SESSION(ie V Period),PERIOD_SESSION(ie VI Period), PERIOD_SESSION(ie VII Period),PERIOD_SESSION(ie VIII Period)
CLASS_ID and BOARD_ID will be used to retrieve Unque combination.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
|
I have done something like this(not sure if i even know what i am doing here)
SELECT
--ID
WEEKDAY_SCHEDULE,
CLASS,
PERIOD_SESSION,
SUBJECT_NAME,
TEACHER_NAME ,
coalesce([1], '-') as '1',
coalesce([2], '-') as '2',
coalesce([3], '-') as '3',
coalesce([4], '-') as '4',
coalesce([5], '-') as '5',
coalesce([6], '-') as '6',
coalesce([7], '-') as '7',
coalesce([8], '-') as '8',
coalesce([9], '-') as '9',
coalesce([10], '-') as '10',
FROM (
SELECT
--ID
WEEKDAY_SCHEDULE,
PERIOD_,
PERIOD_SESSION,
SUBJECT_NAME,
TEACHER_NAME
FROM
TIMETABLE_MANAGEMENT_VIEW
) o
PIVOT (MAX(SUBJECT_NAME) FOR PERIOD_SESSION_ID IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10])) p
--PIVOT (MAX(ATTENDANCE_STATUS_CODE) FOR DAY_OF_ATTENDANCE IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10])) p
--ORDER BY STUDENT_ID, MONTH_OF_ATTENDANCE
GO
|
|
|
Answer : SQL View to show the data using PIVOT
|
|
What about like this? If not please post your exact query.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
|
SELECT
WEEKDAY_SCHEDULE,
CLASS,
coalesce([1], '-') as '1',
coalesce([2], '-') as '2',
coalesce([3], '-') as '3',
coalesce([4], '-') as '4',
coalesce([5], '-') as '5',
coalesce([6], '-') as '6',
coalesce([7], '-') as '7',
coalesce([8], '-') as '8',
coalesce([9], '-') as '9',
coalesce([10], '-') as '10',
FROM (
SELECT
WEEKDAY_SCHEDULE,
PERIOD_SESSION_ID,
ClASS,
SUBJECT_NAME
FROM
TIMETABLE_MANAGEMENT_VIEW
where Class_id = 1 and board_id = 1 --adjust to your needs
) o
PIVOT (MAX(SUBJECT_NAME) FOR PERIOD_SESSION_ID IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10])) p
|
|
|
|
|