1

日付ごとにグループ化された注文に関するデータベース情報を取得しようとしています。テーブル 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;

今、私はこの2つのスクリプトを組み合わせて取得しようとしています:

    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 に答える