Good day everyone.
Please i have a small problem and easy for the genuis in this group. I want to PIVOT MYSQL data base on number of days in a month choosen
sample data
|
attendance |
date |
| primary school | 1-1-2020 |
| primary school | 1-1-2020 |
| secondary school | 1-1-2020 |
| secondary school | 1-1-2020 |
| secondary school | 2-1-2020 |
| primary school | 2-1-2020 |
| primary school | 3-1-2020 |
SQL SERVER has a PIVOT statement which is not available on MYSQL. my problem is i want to group the data base on the current month i chose, i dont want to use CASE statement because i cant i achieve what i want
my expected outcome, let say i chose january as a month, i want the outcome to look like this
| attendance | 1-1-2020 | 2-1-2020 | 3-1-2020 | 4-1-2020 | 5-1-2020 | 6-1-2020 | 7-1-2020 |
| primary school | 2 | 1 | 1 | ||||
| secondary school | 2 | 1 |
i want the outcome to give me complete month days irrespective of choosen month, i can get the sum of data but to get the complete month days is what i need help from. if any one has a better alternative can suggest to me please.
thank you all.
Nitin SontakkePosted Jan 12, 2022, 6:34 PM
Muhammad Imran AnsariPosted Jan 12, 2022, 12:11 PM
Try the following query for result. For date range you can generate a string for respective date.
select *
from
(
select attendance, [date]
from AttendanceRecord
) src
pivot
(
COUNT([date])
for date in ([2020-01-01], [2020-02-01], [2020-03-01], [2020-04-01])
) piv
Abdu AbdulPosted Jan 12, 2022, 11:31 AM
Sachin SinghPosted Jan 12, 2022, 11:07 AM