O
O
okuznetsov12016-06-14 12:11:06
MySQL
okuznetsov1, 2016-06-14 12:11:06

There is a request, it is impossible to present it in CDbCriteria?

Need help, query itself:
SELECT DISTINCT
agencies.id,
agencies.name as nameAgency,
CONCAT('/site.ru/office/agencies/',agencies.id) AS urlAgency,
IF( agencies.enabled=1,"No" ,null) as 'blockedAgency',
IF( suppliersVisaSupport.support=2,"Yes",null) as 'supportVisa',
count(DISTINCT IF( t.status=4,t.orderId,null)) as 'numberOrdersCancelled',
count(DISTINCT IF( t.status=3,t.orderId,null)) as 'numberOrdersRejected',
count(DISTINCT IF( t.status=8,t.orderId,null)) as 'numberOrdersConfirmed',
count(DISTINCT IF ( t.status=6,t.orderId,null)) as 'numberOrdersResponse',
count(DISTINCT t.orderId) as 'numberOrdersTotal',
count(DISTINCT IF( t.status=4,t.orderId,null)) / count(DISTINCT t.orderId) * 100 as 'procentCancelled'
FROM `bookings` `t`
INNER JOIN agencies ON t.agencyId = agencies.id
INNER JOIN suppliersVisaSupport ON suppliersVisaSupport.supplierId = t.supplierId
INNER JOIN dicStatuses ON t.status = dicStatuses.id
WHERE
agencies.enabled = 1 AND
suppliersVisaSupport.support = 2 AND
FROM_UNIXTIME(t.createTime) BETWEEN '2016-02-01' and '2016-02-02'
GROUP BY agencies.name
HAVING numberOrdersTotal > 0
What I did:
// create an instance of the CDbCriteria class
$criteria = new CDbCriteria;
// select all columns from all tables participating in the query
$criteria->select = '
agencies.id,,
agencies.name as nameAgency,
CONCAT(\'/site.ru/office/agencies/\',agencies.id) AS urlAgency,
IF( agencies.enabled=1,"No",null) as \'blockedAgency\',
IF( suppliersVisaSupport.support=2,"Yes",null) as \'supportVisa\',
count(DISTINCT IF( t.status=4,t.orderId,null)) as \'numberOrdersCancelled\',
count(DISTINCT IF( t.status=3,t.orderId,null)) as \'numberOrdersRejected\',
count(DISTINCT IF( t.status=8,t.orderId,null)) as \'numberOrdersConfirmed\',
count(DISTINCT IF( t.status=6,t.orderId,null)) as \'numberOrdersResponse\',
count(DISTINCT t.orderId) as \'numberOrdersTotal\',
count(DISTINCT IF( t.status=4, t.orderId,null)) / count(DISTINCT t.orderId) * 100 as \'procentCancelled\'
';
// select only non-repeating data rows
$criteria->distinct = true;
$criteria->join = 'INNER JOIN agencies ON t.agencyId = agencies.id '
. 'INNER JOIN suppliersVisaSupport ON suppliersVisaSupport.supplierId = t.supplierId '
. 'INNER JOIN dicStatuses ON t.status = dicStatuses.id';
$criteria->condition = 'agencies.enabled = 1 AND
suppliersVisaSupport.support = 2 AND
FROM_UNIXTIME(t.createTime) BETWEEN \'2016-02-01\' and \'2016-02-02\'
';
// grouping
$criteria->group = 'agencies.name';
$criteria->having = 'numberOrdersTotal > 0';
// sort query results
$criteria->order = 'procentCancelled DESC, agencies.name ASC';
$criteria->limit = 1;
But as I understand it, I wrote it down incorrectly, tk. yii builds the query like this:
SELECT COUNT(*) FROM (SELECT DISTINCT
agencies.id,
agencies.name as nameAgency,
CONCAT('/site.ru/office/agencies/',agencies.id) AS urlAgency,
IF( agencies .enabled=1,"No",null) as 'blockedAgency',
IF( suppliersVisaSupport.support=2,"Yes",null) as 'supportVisa',
count(DISTINCT IF( t.status=4,t.orderId, null)) as 'numberOrdersCancelled',
count(DISTINCT IF( t.status=3,t.orderId,null)) as 'numberOrdersRejected',
count(DISTINCT IF( t.status=8,t.orderId,null)) as 'numberOrdersConfirmed',
count(DISTINCT IF ( t.status=6,t.orderId,null)) as 'numberOrdersResponse',
count(DISTINCT t.orderId) as 'numberOrdersTotal',
count(DISTINCT IF( t.status=4,t.orderId,null)) / count(DISTINCT t.orderId) * 100 as 'procentCancelled'
FROM `bookings` `t` INNER JOIN agencies ON t.agencyId = agencies.id INNER JOIN suppliersVisaSupport ON suppliersVisaSupport.supplierId = t.supplierId INNER JOIN dicStatuses ON t.status = dicStatuses.id WHERE agencies.enabled = 1 AND
suppliersVisaSupport.support = 2 AND
FROM_UNIXTIME(t.createTime) BETWEEN '2016-02-01' and '2016-02-02'
GROUP BY agencies.name HAVING numberOrdersTotal > 0) sq

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question