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