Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<pre>Please I have a small problem and easy for the genius in this group. I want to PIVOT MYSQL data base on number of days in a month
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

expected outcome

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 chosen month, I can get the sum of data but to get the complete month days is what I need help from.


What I have tried:

I TRIED SQLSERVER BUT I DONT WANT THE OUTCOME THE WAY SQL SERVER I
Posted
Updated 12-Jan-22 8:43am
Comments
_Asif_ 12-Jan-22 6:45am    
Share your SQL Server query
Maciej Los 12-Jan-22 12:25pm    
Take a look here: Pivot Tables in MySQL[^]

I'd strongly suggest to read these articles:
MySQL Pivot: How To Generate a Pivot Table - Database Star[^]
Build Pivot Tables in MySQL Using User Variables[^]
Pivot Tables in MySQL[^]

Try! When you get stuck, then come back here and ask detailed question.
 
Share this answer
 
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`)
  declare date cursor for `2020-01-01`, `2020-02-01`, `2020-03-01`, `2020-04-01`)
) piv
 
Share this answer
 
v2
Comments
Maciej Los 12-Jan-22 12:24pm    
This does not look like MySql query. This is MS SQL query.
Please, improve it or delete it to avoid down-voting.
CHill60 12-Jan-22 12:36pm    
I just reported it as inaccurate - but mine was the first report
Maciej Los 12-Jan-22 12:44pm    
I did the same :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900