Ok, this is complicated for me to get out there, but here we go.
I have a family/person attendance table containing the following fields:
| mail_no (FamilyID) | meid (Person ID) | date | status (A/P)|
On the family attendance screen, I need to pull the data and group it in the following format:
meid | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec |
---|---|---|---|---|---|---|---|---|---|---|---|---|
123 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
124 | 2 | 3 | 2 | 0 | 0 | 0 | 0 | 1 | 1 | 2 | 2 | 1 |
Currently, my PHP looks like this:
$year = "2020";
$where_clause = "AND ATDATA.mail_no='".$myrow['mail_no']."' AND ATDATA.status='A'";
SELECT meid,
(SELECT COUNT(*) FROM ATDATA WHERE LEFT(ATDATA.date,7)= '".$year."-01' ".$where_clause.") as month1,
(SELECT COUNT(*) FROM ATDATA WHERE LEFT(ATDATA.date,7)= '".$year."-02' ".$where_clause.") as month2,
(SELECT COUNT(*) FROM ATDATA WHERE LEFT(ATDATA.date,7)= '".$year."-03' ".$where_clause.") as month3,
(SELECT COUNT(*) FROM ATDATA WHERE LEFT(ATDATA.date,7)= '".$year."-04' ".$where_clause.") as month4,
(SELECT COUNT(*) FROM ATDATA WHERE LEFT(ATDATA.date,7)= '".$year."-05' ".$where_clause.") as month5,
(SELECT COUNT(*) FROM ATDATA WHERE LEFT(ATDATA.date,7)= '".$year."-06' ".$where_clause.") as month6,
(SELECT COUNT(*) FROM ATDATA WHERE LEFT(ATDATA.date,7)= '".$year."-07' ".$where_clause.") as month7,
(SELECT COUNT(*) FROM ATDATA WHERE LEFT(ATDATA.date,7)= '".$year."-08' ".$where_clause.") as month8,
(SELECT COUNT(*) FROM ATDATA WHERE LEFT(ATDATA.date,7)= '".$year."-09' ".$where_clause.") as month9,
(SELECT COUNT(*) FROM ATDATA WHERE LEFT(ATDATA.date,7)= '".$year."-10' ".$where_clause.") as month10,
(SELECT COUNT(*) FROM ATDATA WHERE LEFT(ATDATA.date,7)= '".$year."-11' ".$where_clause.") as month11,
(SELECT COUNT(*) FROM ATDATA WHERE LEFT(ATDATA.date,7)= '".$year."-12' ".$where_clause.") as month12
FROM ATDATA
WHERE ATDATA.mail_no='".$myrow['mail_no']."' AND ATDATA.status='A'
GROUP BY meid,month1, month2, month3, month4"
I'm getting the following output, which has the meid's split per row, but the underlying data is grouped on mail_no. How can I get the data grouped by meid instead?
meid | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec |
---|---|---|---|---|---|---|---|---|---|---|---|---|
1752 | 5 | 7 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 6 |
198 | 5 | 7 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 6 |
199 | 5 | 7 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 6 |
hmmm hey
Just group by 'meid', remove the months in the group by... Then do counts where month = Jan... month = Feb... in your select...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.