2012-02-21 1 views
0

は、合計売上に関するレポートを生成する次のクエリを作成しました。それは私が期待した結果を生む。私は、MySQLを使用していますが、同様にクエリが見えます:生成される出力がある合計クエリが値を変更して別のテーブルに結合された場合

SELECT detailed_report.title AS title, 
detailed_report.nid AS nid, 
detailed_report.price AS 'price', 
SUM(detailed_report.quantity) AS 'total_quantity', 
SUM(detailed_report.total) AS 'total_revenue' 
FROM (SELECT uc_order_products.nid AS 'nid', 
uc_orders.order_id AS 'order_id', 
uc_order_products.title AS title, 
uc_order_products.price AS price, 
uc_order_products.qty AS 'quantity', 
(uc_order_products.qty * uc_order_products.price) AS 'total' 
FROM uc_orders uc_orders 
LEFT JOIN uc_order_products uc_order_products ON uc_orders.order_id = uc_order_products.order_id 
WHERE uc_orders.order_status IN ('completed')) AS detailed_report 
GROUP BY nid 

enter image description here

私はクエリに日付フィールドを追加すると、その後量の量が正しくありません。私はそれがなぜあるのか分からない。ここで修正されたクエリは次のとおりです。次のように

SELECT event_dates.field_dates_value AS start_date, 
event_dates.field_dates_value2 AS end_date, 
detailed_report.title AS title, 
detailed_report.nid AS nid, 
detailed_report.price AS 'price', 
SUM(detailed_report.quantity) AS 'total_quantity', 
SUM(detailed_report.total) AS 'total_revenue' 
FROM (SELECT uc_order_products.nid AS 'nid', 
uc_orders.order_id AS 'order_id', 
uc_order_products.title AS title, 
uc_order_products.price AS price, 
uc_order_products.qty AS 'quantity', 
(uc_order_products.qty * uc_order_products.price) AS 'total' 
FROM uc_orders uc_orders 
LEFT JOIN uc_order_products uc_order_products ON uc_orders.order_id = uc_order_products.order_id 
WHERE uc_orders.order_status IN ('completed')) AS detailed_report 
LEFT JOIN content_field_dates event_dates ON detailed_report.nid =event_dates.nid 
GROUP BY nid 

結果は次のとおりです。数量の列の値が正しくないことを

result of query

は注意してください。どうしてこれなの?誰かが私が間違ったやり方を説明し、それを修正する方法はありますか?私は完全に困惑しています。

おかげ

答えて

0

私はあなたが粉々に使用しているSQLを分析し、使用しているテーブル式の異なるものがあるかどうかを決定します。

それがOKならば、私は選択から合計を取り除き、新しいテーブルに左の結合との違いがあるかどうかを確認します。

関連する問題