在select中选择 - 每个派生表必须有自己的别名错误

时间:2013-03-12 12:04:30

标签: mysql select correlated-subquery

我正在尝试从按日期分组的订单的数据库信息中获取。 我有表sales_flat_order,我有它的id,订单创建日期,订单的total_paid和订单项目计数。我有表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

有人可以帮忙吗?

1 个答案:

答案 0 :(得分: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