SQL查询可从表中获取最小的总和值和关联的列

时间:2019-08-01 11:26:18

标签: mysql sql

我有下表:

t1-物品

| Id | Item   |
|  1 | I1     |
|  2 | I2     |

t2-项目元素

| Id |  Item_id | Element    | Our_quantity  |  Client_quantity |
|  1 |       1  | E11        |          100  |               0  |
|  2 |       1  | E12        |           20  |             300  |
|  3 |       2  | E21        |          300  |             100  |
|  4 |       2  | E22        |            5  |             300  |

t3-元素操作

| Id |  Element_id | Operations_number |
|  1 |           1 |               100 |
|  2 |           1 |                50 |
|  3 |           2 |                50 |
|  4 |           2 |                50 |
|  5 |           3 |                50 |
|  6 |           3 |                50 |
|  7 |           3 |                50 |
|  8 |           3 |                50 |
|  9 |           4 |                10 |
我需要SQL查询,该查询返回表女巫项目(t1)和与具有最小操作数

的项目表关联的元素行

所需的输出: 表格应类似于

| Id|Item| Id_el |Our_quantity| Client_quantity | Count Operations_number|
|  1| I1 |     2 |         20 |             300 |                    100 |
|  2| I2 |     4 |          5 |             300 |                     10 |

result

我尝试了该查询

SELECT t2.Id,Item_id,Our_Quantity,Client_Quantity,SUM(Operations_number) 
FROM t2 LEFT JOIN t3 ON t2.Id=t3.Id_el GROUP BY t2.Id)

尝试结果:

| Id |  Item_id | Our_quantity  |  Client_quantity |SUM(Operations_number)
|  1 |       1  |          100  |               0  |                   150
|  2 |       1  |           20  |             300  |                   100
|  3 |       2  |          300  |             100  |                   200
|  4 |       2  |            5  |             300  |                    10

下一步我该怎么做?

现在我有2张桌子:

| Element Id |  Item_id |Sum operations_number for element  |
|  1         |       1  |                          150      |
|  2         |       1  |                          100      |
|  3         |       2  |                          200      |
|  4         |       2  |                           10      |
| Item Id |     Item  |
|  1      |       I1  |
|  2      |       I2  |

我如何加入他们的行列?

具有最小总和运算数的项目元素。

| Item Id |     Item  | Element_Id | Sum operations_number for element |
|  1      |       I1  | 2          |                          100      |
|  2      |       I2  | 4          |                           10      |

5 个答案:

答案 0 :(得分:0)

您可以尝试这个。.

对于mysql 8 +

     with cte as (
        SELECT t1.id as Item_id, t1.item, t2.id as Ele_Id, t2.Element , t2.Our_quantity , t2.Client_quantity , t3.Operations_number   
        from Items as t1 
        inner join Item_elements as t2 on t1.id=t2.Item_id 
        inner join Item_elements as t3 on t2.id = t3.Element_id 
    )
  , ct as (
        select row_number() over ( partition by t1.id order by t2.Our_quantity ) as Slno, * from cte
    )
    select c.item_id, c.item, c.Ele_id, c.Element, c.our_quantity, c.client_quantity, t.Count_Operations_number 
    from ct as c 
        inner join 
        (
                select distinct Element_id , sum(Operation_number) as Count_Operations_number  
                from Element_operations group by Element_id 
        ) as t 
        on c.Ele_id=t.Element_id
        where c.slno=1

答案 1 :(得分:0)

也许,如果您在所需的列中使用MIN()方法,例如:

SQL

SELECT t2.Id,Item_id, MIN(Our_Quantity), Client_Quantity, SUM(Operations_number) 
FROM t2 LEFT JOIN t3 
ON t2.Id=t3.Id_el 
GROUP BY t2.Id

答案 2 :(得分:0)

是否要以SUM(Operations_number)的最小顺序?

尝试一下

我已经更新了答案,所以这也将获得第一个表格。

    SELECT t1.id, 
       t1.item, 
       id_el, 
       our_quantity, 
       client_quantity, 
       sum 
FROM   t1 
       JOIN (SELECT t2.id              AS Id_el, 
                    t2.item_id, 
                    t2.our_quantity    AS our_quantity, 
                    t2.client_quantity AS client_quantity, 
                    sum 
             FROM   t2 
                    JOIN (SELECT t3.element_id, 
                                 Sum(operations_number) AS sum 
                          FROM   t3 
                          GROUP  BY element_id) AS b 
                      ON t2.id = b.element_id) AS c 
         ON t1.id = c.item_id 
ORDER  BY sum ASC 

输出为:

![Output Format

答案 3 :(得分:0)

尝试以下查询,希望这是您要的

select t1.Id as 'Id',t1.Item as 'Item',
t.Id as 'Id_el', t.Our_quantity as 'Our_quantity',t.Client_quantity as 'Client_quantity',
sum(t3.Operations_number) as 'Count Operations_number'
from t1 join (select * 
from t2 where (Item_id,Our_quantity) in 
( select Item_id, min(Our_quantity) from t2 group by Item_id)) t on t1.Id=t.Item_id
join t3 on t.Id=t3.Element_id
group by t1.Id;

答案 4 :(得分:0)

您可以使用它获得所需的输出。

SELECT 
  t.*,
  MIN(t.opsum) AS `Count Operations_number` 
FROM
  (SELECT 
    a.*,
    b.Id AS `Id_el`,
    b.`Our_quantity`,
    b.`Client_quantity`,
    SUM(c.`Operations_number`) AS opsum 
  FROM
    `t1` AS a 
    LEFT JOIN `t2` AS b 
      ON a.`Id` = b.`Item_id` 
    LEFT JOIN `t3` AS c 
      ON b.`Id` = c.`Element_id` 
  GROUP BY a.Id,
    b.Id 
  ORDER BY a.`Id` ASC,
    opsum ASC) AS t 
GROUP BY t.Id ;

FIDDLE HERE