繁体   English   中英

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

[英]SQL query to get minimal sum value and associated column from table

我有以下表格:

t1 - 物品

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

t2 - Item_elements

| 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 |
我需要返回表女巫项目 (t1) 和与具有最小操作数的项目表关联的元素行的 SQL 查询

所需的输出:表应该看起来像

\n | 编号|项目|  Id_el |我们的数量| 客户数量 | 计数操作_编号|\n |  1|  I1 |  2 |  20 |  300 |  100 |\n |  2|  I2 |  4 |  5 |  300 |  10 |\n

结果

我试过那个查询

| 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

试过的结果:

\n | 身份证 | 商品编号 | 我们的数量 |  Client_quantity |SUM(Operations_number)\n |  1 |  1 |  100 |  0 |  150\n |  2 |  1 |  20 |  300 |  100\n |  3 |  2 |  300 |  100 |  200\n |  4 |  2 |  5 |  300 |  10\n

我接下来该怎么做?

现在我有 2 个表:

\n | 元素 ID |  Item_id |元素的总操作数 |\n |  1 |  1 |  150 |\n |  2 |  1 |  100 |\n |  3 |  2 |  200 |\n |  4 |  2 |  10 |\n
\n | 项目 ID | 项目 |\n |  1 |  I1 |\n |  2 |  I2 |\n

我怎样才能加入他们以获得这张桌子?

具有最小求和操作数的项目元素。

\n | 项目 ID | 项目 | 元素_Id | 对元素求和操作数 |\n |  1 |  I1 |  2 |  100 |\n |  2 |  I2 |  4 |  10 |

你可以试试这个..

对于 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

也许如果您在所需的列中使用 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

你想要 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 

输出将是:

![输出格式

试试下面的查询,希望这就是你要问的

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;

您可以使用此获得所需的输出..

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 ;

在这里小提琴

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM