SQL - 子选择/分割两列

时间:2016-07-25 11:46:15

标签: mysql sql magento subquery

我对我的magento商店有这个查询:

SELECT date_format(sfo.shipping_arrival_date,"%d.%m.%Y") AS DeliveryDate,
       count(sfo.entity_id) AS NoOfOrders,
       sum(
             (SELECT if(count(s.customer_email)>1,0,1)
              FROM sales_flat_order AS s
              WHERE s.customer_email=sfo.customer_email
                AND (s.status = 'pending'
                     OR s.status = 'processing'))) AS NewCustomers,
       sum(
             (SELECT if(count(s.customer_email)>1,1,0)
              FROM sales_flat_order AS s
              WHERE s.customer_email=sfo.customer_email
                AND (s.status = 'pending'
                     OR s.status = 'processing'))) AS ExistingCustomers,

             (SELECT count(*)
              FROM sales_flat_order AS s
              WHERE s.shipping_method = "freeshipping_freeshipping"
                AND s.shipping_arrival_date = sfo.shipping_arrival_date
                AND (s.status = 'pending'
                     OR s.status = 'processing')) AS NoOfCustomersThatPickUp                
FROM sales_flat_order AS sfo
WHERE sfo.status <> 'canceled'
  AND sfo.shipping_arrival_date <> ''
GROUP BY DeliveryDate
ORDER BY sfo.shipping_arrival_date DESC

它列出了订单数量,新客户,现有客户以及在一张表中提取订单的客户数量。

我现在想要将接收的客户数量除以订单总数,但是当我添加此行时:

NoOfCustomersThatPickUp/NoOfOrders

我收到此错误:

Error running query: Unknown column 'NoOfCustomersThatPickUp' in 'field list'

有人知道如何实现这个目标吗?

谢谢!

0 个答案:

没有答案