MYSQL查询 - 使用来自另一个表

时间:2016-10-13 18:45:46

标签: mysql sql aggregate-functions

我指的是2张桌子。我能够从item_transactions表中为每个item_id汇总qty_sold。在一个单独的列中,我想从i_multiple_int_attributes表中查找该项的所有先前版本的qty_sold(有些最多有4个以前的版本)。有人能指出我正确的方向吗?

当前查询:

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 |
+-------+-------+------------+-------------------+-------+

1 个答案:

答案 0 :(得分: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