/    Sign up×
Community /Pin to ProfileBookmark

YEARWEEK() skips the first week of the year

GOAL: I am trying to retrieve the sum of data on a weekly basis between a start and end-date (provided by the user).

Issue: **When retrieving data between a date range involving 2019-2020, the first week of 2020 is skipped.** It goes straight to 202002. Why is this? How can I fix it?

“`
//Retrieve count of attendance, no shows and cancellations per user selected category and sort by week number
$q = “SELECT YEARWEEK(`start_time`, 0) AS weekno,
SUM(`is_no_show` = 0 AND `is_cancelled` = 0) as attended,
SUM(`is_no_show` = 1) AS no_shows,
SUM(`is_cancelled` = 1)AS cancelled
FROM `myTable`
WHERE (`start_time` > :start_date AND `start_time` < :end_date)
AND category LIKE :cohort_type
GROUP BY weekno”;

try {
$stmt = $dbh->prepare($q);
$stmt->bindParam(‘:start_date’, $start_date);
$stmt->bindParam(‘:end_date’, $end_date);
$stmt->bindParam(‘:cohort_type’, $cohort_type, PDO::PARAM_STR);
$stmt->execute();
$q_result = $stmt->fetchAll(PDO::FETCH_ASSOC);

if($q_result) {
if(count($q_result) > 1) {
// good records
}else{
// no records
}

}else{
// query failed.
}

echo json_encode(array(‘success’ => 1,’msg’ => $q_result));
}

catch(PDOException $e) {
echo json_encode(array(‘success’=> 1, ‘msg’=>$e->getMessage()));
exit;
}

“`

General DB Structure:
`+————+———–+———–+
| start_time | no_shows | cancelled |
+————+———–+———–+
| 2019-12-20 | 1 | 0 |
| 2019-12-21 | 0 | 0 |
| 2019-12-22 | 0 | 1 | `

Thank you!!

to post a comment

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 23.2020 — Maybe remove the second argument to YEARWEEK(), so that it does not require specific days or number of days to exist in that week. (It uses the same 2nd parameter as does the WEEK() function, as described at https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_week .)
Copy linkTweet thisAlerts:
@heartstringsauthorJan 23.2020 — @NogDog#1613514

Would it also skip a week if there was no particular data for that week?
Copy linkTweet thisAlerts:
@NogDogJan 24.2020 — > @heartstrings#1613521 Would it also skip a week if there was no particular data for that week?

Yeah, most likely. A simple way to add in the missing weeks is not jumping out at me right away -- can the code that consumes the result handle it without the "empty" weeks, or do you really need it in that JSON result?
Copy linkTweet thisAlerts:
@heartstringsauthorJan 24.2020 — @NogDog#1613531 I need it in a json result to send it back to the site for graphing. Is there a way to convert the YEARWEEK() output to a date in php?

Like say the output is '201940'. How would I convert this to '2019/09/30'?
Copy linkTweet thisAlerts:
@NogDogJan 24.2020 — Does this look right?
<i>
</i>$ /usr/bin/php -a
Interactive shell

php &gt; $week = '201940';
php &gt; list($year, $weekNbr) = str_split($week, 4);
php &gt; echo $year;
2019
php &gt; echo $weekNbr;
40
php &gt; $date = date('Y/m/d', strtotime("+$weekNbr weeks", strtotime("$year/01/01")));
php &gt; echo $date;
2019/10/08

Hmm...looks like it's in the ballpark of what you expected, but a bit over. :
×

Success!

Help @heartstrings spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...