1

我正在尝试从数据库中获取有关按日期分组的订单的信息。我有表 sales_flat_order,其中有它的 id、订单创建日期、total_paid for order 和 order item count。我有表 sales_flat_order_item 订单商品的价格在哪里。

我创建了脚本来按天获取订单信息:

SELECT 
    DATE( sales_flat_order.created_at ) AS date,
    SUM( sales_flat_order.total_paid ) AS sales,
    SUM( sales_flat_order.total_item_count ) AS items
FROM 
    sales_flat_order, 
    sales_flat_order_payment
WHERE 
    sales_flat_order.status = 'complete'
    AND sales_flat_order.entity_id = sales_flat_order_payment.parent_id
    AND sales_flat_order_payment.method = 'checkmo'
GROUP BY DATE( sales_flat_order.created_at )
WITH ROLLUP

我得到:

DATE       SALES ITEMS
2013-03-05 72    3
2013-03-06 100   5

我有脚本来计算中位数价格:

    SELECT 
    avg(t1.price) as median_val 
FROM 
    (
        SELECT 
            @rownum:=@rownum+1 as `row_number`, 
            d.price
            FROM 
                sales_flat_order_item d,  
                (SELECT @rownum:=0) r
            WHERE 1
            ORDER BY d.price
    ) as t1, 
    (   
        SELECT 
            count(*) as total_rows
        FROM 
            sales_flat_order_item d
        WHERE 1
    ) as t2
WHERE 1
    AND t1.row_number>=total_rows/2 
    and t1.row_number<=total_rows/2+1;

现在我试图结合这两个脚本来获得:

    DATE       SALES ITEMS median_item_price
    2013-03-05 72    3     19
    2013-03-06 100   5     10.5

组合脚本:

    SELECT 
    DATE( sales_flat_order.created_at ) AS date,
    SUM( sales_flat_order.total_paid ) AS sales,
    SUM( sales_flat_order.total_item_count ) AS items,
    sales_flat_order_item.price as median_item_price
FROM 
    sales_flat_order, 
    sales_flat_order_payment, 
    (
        SELECT 
            avg(t1.price) as median_val 
        FROM 
            (
                SELECT 
                    @rownum:=@rownum+1 as `row_number`, 
                    d.price
                FROM 
                    sales_flat_order_item d,  
                    (SELECT @rownum:=0) r
                WHERE 1
                ORDER BY d.price
            ) as t1, 
            (
                SELECT 
                    count(*) as total_rows
                FROM 
                    sales_flat_order_item d
                WHERE 1
            ) as t2
        WHERE 1
            AND t1.row_number>=total_rows/2 
            and t1.row_number<=total_rows/2+1
    ) as sales_flat_order_item
WHERE 
    sales_flat_order.status = 'complete'
    AND sales_flat_order.entity_id = sales_flat_order_payment.parent_id
    AND sales_flat_order_payment.method = 'checkmo' 
    AND DATE(sales_flat_order_item.created_at) = DATE(sales_flat_order.created_at)
GROUP BY DATE( sales_flat_order.created_at )
WITH ROLLUP

并得到错误:#1248 - 每个派生表都必须有自己的别名

这是数据库:http ://sqlfiddle.com/#!2/7dfec

任何人都可以帮忙吗?

4

1 回答 1

3

解决方案:

SELECT 
    DATE( sales_flat_order.created_at ) AS date,
    SUM( sales_flat_order.total_paid ) AS sales,
    SUM( sales_flat_order.total_item_count ) AS items,
    MAX( median.median_val ) as median_item_price
FROM 
    sales_flat_order, 
    sales_flat_order_payment,
    (
 SELECT DATE(sq.created_at) as median_date, avg(sq.price) as median_val FROM (
SELECT t1.row_number, t1.price, t1.created_at FROM(
SELECT IF(@prev!=d.created_at, @rownum:=1, @rownum:=@rownum+1) as `row_number`, d.price, @prev:=d.created_at AS created_at
FROM sales_flat_order_item d, (SELECT @rownum:=0, @prev:=NULL) r
ORDER BY d.price
) as t1 INNER JOIN  
(
  SELECT count(*) as total_rows, created_at 
  FROM sales_flat_order_item d
  GROUP BY created_at
) as t2
ON t1.created_at = t2.created_at
WHERE 1=1
AND t1.row_number>=t2.total_rows/2 and t1.row_number<=t2.total_rows/2+1
)sq
group by DATE(sq.created_at)
    ) as median
WHERE 
    sales_flat_order.status = 'complete'
    AND sales_flat_order.entity_id = sales_flat_order_payment.parent_id
    AND sales_flat_order_payment.method = 'checkmo'
    AND median.median_date = DATE( sales_flat_order.created_at )
GROUP BY DATE( sales_flat_order.created_at )
WITH ROLLUP
于 2013-03-13T15:28:53.037 回答