I have the following two Queries that I would like to combine into one with the date being the first column. The problem I'm having is that the conditions of the subquery are different and the second query might not have a value for every month. I have been running them on different pages hence why the AS num_custs is the same on both.
I have tried a couple of different things with SQL but thus far have failed. My initial thoughts were to use UNION in the SQL but that didn't work. I think I am on the right tracks trying to do it in SQL rather than PHP.
First question would be which SQL command should I be using to achieve this?
Hope it all makes sense.
Query 1:
SELECT * , COUNT( entity_id ) AS num_custs
FROM (
SELECT e. * , e.created_at AS abc123, o.status, o.total_invoiced, o.shipping_description, o.subtotal_incl_tax, MAX( o.created_at ) AS last_order_date
FROM mg_customer_entity AS e
LEFT JOIN mg_sales_flat_order AS o ON o.customer_id = e.entity_id
WHERE e.entity_type_id = '1'
AND o.status NOT LIKE 'canceled'
AND o.status NOT LIKE 'closed'
AND o.status NOT LIKE 'fraud'
AND o.status NOT LIKE 'holded'
AND o.status NOT LIKE 'paypal_canceled_reversal'
AND e.store_id
BETWEEN 1
AND 2
AND o.total_invoiced IS NOT NULL
AND o.subtotal_incl_tax IS NOT NULL
GROUP BY e.entity_id
HAVING last_order_date IS NOT NULL
)sub_query
GROUP BY YEAR( abc123 ) , MONTH( abc123 )
ORDER BY abc123 DESC
Query 2:
SELECT * , COUNT( entity_id ) AS num_custs
FROM (
SELECT e. * , e.created_at AS abc123, o.status, o.total_invoiced, o.shipping_description, o.subtotal_incl_tax, MAX( o.created_at ) AS last_order_date
FROM mg_customer_entity AS e
LEFT JOIN mg_sales_flat_order AS o ON o.customer_id = e.entity_id
WHERE e.entity_type_id = '1'
AND o.status NOT LIKE 'canceled'
AND o.status NOT LIKE 'closed'
AND o.status NOT LIKE 'fraud'
AND o.status NOT LIKE 'holded'
AND o.status NOT LIKE 'paypal_canceled_reversal'
AND e.store_id
BETWEEN 1
AND 2
AND o.total_invoiced IS NOT NULL
AND o.subtotal_incl_tax IS NOT NULL
GROUP BY e.entity_id
HAVING last_order_date >= DATE_SUB( CURDATE( ) , INTERVAL 91 DAY )
AND last_order_date IS NOT NULL
)sub_query
GROUP BY YEAR( abc123 ) , MONTH( abc123 )
ORDER BY abc123 DESC
You mention that you wanted the date as first result, you can either specify all fields or just add it in front.
If you build the query in PHP you can easily inject the extra condition.
The condition will be inserted at the "%s" position in the sprintf function .
// set $limit_order_date depending on the page
$having = '';
if ( true === $limit_order_date ) {
$having = 'AND last_order_date >= DATE_SUB( CURDATE( ) , INTERVAL 91 DAY )';
}
$sql = sprintf("
SELECT abc123,
* ,
COUNT( entity_id ) AS num_custs
FROM (
SELECT e. *,
e.created_at AS abc123,
o.status,
o.total_invoiced,
o.shipping_description,
o.subtotal_incl_tax,
MAX( o.created_at ) AS last_order_date
FROM mg_customer_entity AS e
LEFT JOIN mg_sales_flat_order AS o
ON o.customer_id = e.entity_id
WHERE e.entity_type_id = '1'
AND o.status NOT LIKE 'canceled'
AND o.status NOT LIKE 'closed'
AND o.status NOT LIKE 'fraud'
AND o.status NOT LIKE 'holded'
AND o.status NOT LIKE 'paypal_canceled_reversal'
AND e.store_id BETWEEN 1 AND 2
AND o.total_invoiced IS NOT NULL
AND o.subtotal_incl_tax IS NOT NULL
GROUP BY e.entity_id
HAVING last_order_date IS NOT NULL %s
)sub_query
GROUP BY YEAR( abc123 ),
MONTH( abc123 )
ORDER BY abc123 DESC",
$having
);
I hope I interpreted your question correctly.
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.