2016-10-13 31 views
1

我有2個表格,如圖所示。我能夠從item_transactions表中爲每個item_id合計qty_sold。在單獨的列中,我希望從i_multiple_int_attributes表中爲該項目的所有先前版本彙總qty_sold(某些版本最多有4個之前的版本)。有人能指引我朝着正確的方向嗎?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

謝謝!在你的第二個子查詢WHERE子句中,我必須將「t.item_id IN」更改爲「a.id IN」),但這正是我所需要的。 –

+0

不客氣。 – Serg

相關問題