0

MySQL での結合に問題があります。customerorder と customerorderpos の 2 つのテーブルがあります。pos テーブルには注文されたアイテムが含まれます。したがって、customerorder テーブルには注文ごとに常に 1 つのエントリしかありませんが、customerorderpos には同じ customerorderid を持つ複数のエントリが存在する場合があります。

customerorder で合計計算を実行しようとする場合を除いて、すべてがクエリで機能します。たとえば、sum(cart_total_complete)。これは重複した値を返しています (複数の customerorderpos レコードを持つ注文に対して複数回カウントされています)。

結合タイプまたは個別の使用方法のいずれかで、それは基本的なものだと確信していますが、何時間も試してみましたが、うまくいきません...

私が間違っていることはありますか?ご協力いただきありがとうございます!!

select concat(left(dayname(from_unixtime(customerorder.datetime)),3), ' ',
day(from_unixtime(customerorder.datetime))) as day, 
count(distinct customerorder.customerorderid) as count_totalorders, 
count(distinct customerorderpos.itemid) as count_differentitems, 
sum(customerorderpos.quantity_ordered - customerorderpos.quantity_cancelled) as quantity_ordered, 
sum(customerorderpos.itemsubtotal) as item_subtotal, 
sum(customerorderpos.pricechangetotal) as item_pricechangetotal, 
sum(customerorderpos.itemtotal) as item_total, 
sum(customerorderpos.purchase_price * (customerorderpos.quantity_ordered - customerorderpos.quantity_cancelled)) as item_purchasepricetotal, 
sum(cart_discounttotal) as total_discount, 
sum(customerorderpos.itemtotal) - sum(customerorderpos.purchase_price * (customerorderpos.quantity_ordered - customerorderpos.quantity_cancelled)) - sum(cart_discounttotal) as item_earningstotal, 
sum(cart_total_shipping) as total_shipping, 
sum(cart_total_tax) as total_tax, 
sum(cart_total_complete) as total_complete 
from customerorder inner join customerorderpos on customerorderpos.customerorderid = customerorder.customerorderid 
where customerorder.status_cancelled = 0 
group by day(from_unixtime(customerorder.datetime)) order by customerorder.datetime
4

1 に答える 1

0

customerorderpos最初にテーブルを次のように集計する場合customerorderid

SELECT   customerorderid,
         SUM(quantity_ordered - quantity_cancelled) AS quantity_ordered,
         SUM(itemsubtotal) AS item_subtotal,
         SUM(pricechangetotal) AS item_pricechangetotal,
         SUM(itemtotal) AS item_total,
         SUM(purchase_price * (
           quantity_ordered - quantity_cancelled
         )) AS item_purchasepricetotal,
FROM     customerorderpos
GROUP BY customerorderid

customerorder次に、結果をテーブルに結合できます。

SELECT   FROM_UNIXTIME(customerorder.datetime, '%a %e') AS day, 
         COUNT(*)                       AS count_totalorders,
         SUM(i.quantity_ordered)        AS quantity_ordered, 
         SUM(i.item_subtotal)           AS item_subtotal,
         SUM(i.item_pricechangetotal)   AS item_pricechangetotal, 
         SUM(i.item_total)              AS item_total, 
         SUM(i.item_purchasepricetotal) AS item_purchasepricetotal, 
         SUM(o.cart_discounttotal)      AS total_discount, 
         SUM(
           i.item_total
         - i.purchasepricetotal
         - o.cart_discounttotal
         )                              AS item_earningstotal, 
         SUM(o.cart_total_shipping)     AS total_shipping, 
         SUM(o.cart_total_tax)          AS total_tax, 
         SUM(o.cart_total_complete)     AS total_complete 
FROM     customerorder o JOIN (
  SELECT   customerorderid,
           COUNT(DISTINCT itemid) AS count_differentitems,
           SUM(quantity_ordered - quantity_cancelled) AS quantity_ordered,
           SUM(itemsubtotal) AS item_subtotal,
           SUM(pricechangetotal) AS item_pricechangetotal,
           SUM(itemtotal) AS item_total,
           SUM(purchase_price * (
             quantity_ordered - quantity_cancelled
           )) AS item_purchasepricetotal,
  FROM     customerorderpos
  GROUP BY customerorderid
) i USING (customerorderid)
WHERE    o.status_cancelled = 0 
GROUP BY FROM_UNIXTIME(o.datetime, '%e')
ORDER BY o.datetime

各注文で個別のアイテムの数count_differentitemsを集計するか(上記を拡張する単純なケース)、または集計されたすべての注文で個別のアイテムの数を集計するか(追加のテーブル結合が必要になります)。

FROM_UNIXTIME()また、フォーマットされた日付文字列を手動で作成する代わりに、日付フォーマット引数を使用したことにも注意してください。

于 2012-11-06T18:21:35.013 に答える