2016-10-13 8 views
1

表示されているように2つのテーブルがあります。私はitem_transactionsテーブルから各item_idのqty_soldをSUMにすることができます。別の列では、i_multiple_int_attributesテーブル(前回のエディションが4つまであります)からそのアイテムの以前のすべてのエディションのqty_soldをSUMにしたいと思います。誰かが私を正しい方向に向けることができますか?MYSQL他のテーブルの複数のIDを使用したクエリの合計アイテム

現在のクエリ:

SELECT t.item_id, SUM(t.qty_sold) as current_id_sales, SUM(?) as previous_id_sales 
FROM gnpcb.item_transactions t join gnpcb.i_multiple_int_attributes a on t.item_id = a.id 
and a.type = 'items' and a.attribute = 'previous_editions' 
WHERE t.id_type in ('invoice', 'credit') and t.item_id IN ('18117', '17473', '17568') 
and t.transaction_type in ('sale', 'return', 'return_nts') 
GROUP BY t.item_id; 

望ましい結果:

+---------+------------------+-------------------+ 
| item_id | current_id_sales | previous_id_sales | 
+---------+------------------+-------------------+ 
| 17473 |   15743 |    9625 | 
| 17568 |    3893 |    24232 | 
| 18117 |   14430 |    8083 | 
+---------+------------------+-------------------+ 

は表1:item_transactions

+---------+---------+----------+----------------+----------------+ 
| id_type | item_id | qty_sold | price_extended | date_effective | 
+---------+---------+----------+----------------+----------------+ 
| invoice | 18117 |  8 |  13.1600 | 2016-10-01  | 
| invoice | 17473 |  1 |   2.2500 | 2016-10-01  | 
| invoice | 18117 |  1 |   1.0000 | 2016-10-01  | 
| invoice | 18117 |  7 |   2.0000 | 2016-10-01  | 
| invoice | 18117 |  5 |   3.0000 | 2016-10-01  | 
| invoice | 17473 |  3 |   4.0000 | 2016-10-01  | 
| invoice | 17568 |  1 |   4.0000 | 2016-10-01  | 
| invoice | 17568 |  5 |   3.0000 | 2016-10-01  | 
| invoice | 18117 |  8 |   2.0000 | 2016-10-01  | 
| invoice | 17473 |  1 |   1.0000 | 2016-10-01  | 
+---------+---------+----------+----------------+----------------+ 

は表2:i_multiple_int_attributes

+-------+-------+------------+-------------------+-------+ 
| type | id | sort_order | attribute   | value | 
+-------+-------+------------+-------------------+-------+ 
| items | 17473 |   1 | previous_editions | 15743 | 
| items | 17568 |   1 | previous_editions | 3893 | 
| items | 17568 |   2 | previous_editions | 7626 | 
| items | 18117 |   1 | previous_editions | 14430 | 
| items | 18117 |   2 | previous_editions | 17337 | 
| items | 18117 |   3 | previous_editions | 17123 | 
| items | 18117 |   4 | previous_editions | 17614 | 
+-------+-------+------------+-------------------+-------+ 

答えて

0

一つの方法は、グループの現在の、前のグループ、

select curr.item_id, curr.sales, prev.sales 
from (
    SELECT t.item_id, SUM(t.qty_sold) as sales 
    FROM gnpcb.item_transactions t 
    join gnpcb.i_multiple_int_attributes a on t.item_id = a.id 
       and a.type = 'items' and a.attribute = 'previous_editions' 
    WHERE t.id_type in ('invoice', 'credit') and t.item_id IN ('18117', '17473', '17568') and t.transaction_type in ('sale', 'return', 'return_nts') 
    GROUP BY t.item_id) curr 
left join(
    SELECT a.id as item_id, SUM(t.qty_sold) as sales 
    FROM gnpcb.item_transactions t 
    join gnpcb.i_multiple_int_attributes a on t.item_id = a.value 
       and a.type = 'items' and a.attribute = 'previous_editions' 
    WHERE t.id_type in ('invoice', 'credit') and t.item_id IN ('18117', '17473', '17568') and t.transaction_type in ('sale', 'return', 'return_nts') 
    GROUP BY a.id) prev on curr.item_id = prev.item_id 
+0

ありがとう参加です! 2番目のサブクエリのWHERE節では、 "t.item_id IN"を "a.id IN"に変更しなければなりませんでしたが、それが必要なものでした。 –

+0

ようこそ。 – Serg

関連する問題