Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Cursometr\Facts\CourseStructure;
- use App\Cursometr\Facts\UsingQuery;
- use Tinderbox\ClickhouseBuilder\Integrations\Laravel\Builder;
- use Tinderbox\ClickhouseBuilder\Query\Identifier;
- class CourseStructureLearnersTotalFact extends AbstractCourseStructureFact implements UsingQuery
- {
- protected $format = 'integer';
- protected $defaultValue = 0;
- public static function getName(): string
- {
- return 'learners-total';
- }
- public function getQuery(\Closure $queryCallback = null): Builder
- {
- $learnersQuery = $this->getCleanQuery()
- ->table('learners')
- ->arrayJoin('entitiesAttempts')
- ->select('learnerId', 'entitiesAttempts.entityIri as entityIri')
- ->applyWherePeriod('entitiesAttempts.startedAt', $this->getPeriod())
- ->final();
- if (!is_null($queryCallback)) {
- $learnersQuery = $queryCallback($learnersQuery, $this);
- }
- $attemptsQuery = $this->getCleanQuery()
- ->from($learnersQuery)
- ->select('learnerId', 'entityIri as childEntityIri')
- ->where('learnerId', '>', 0);
- /*
- * Запрос на распаковывание таблицы с IRI
- */
- $longTableQuery = $this->getCleanQuery()
- ->table(new Identifier($this->getTempTableName()))
- ->select('entityIri', 'childEntityIri')
- ->arrayJoin('childs as childEntityIri');
- $summaryJoinQuery = $this->getCleanQuery()
- ->from($longTableQuery)
- ->select('entityIri', 'learnerId')
- ->allLeftJoin($attemptsQuery, ['childEntityIri']);
- $finalQuery = $this->getCleanQuery()
- ->from($summaryJoinQuery)
- ->select(raw('uniq(learnerId) as '.static::getIdentifier()), 'entityIri')
- ->where('learnerId', '>', 0)
- ->groupBy('entityIri');
- return $finalQuery;
- }
- public function getJoinColumns(): array
- {
- return ['entityIri'];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement