如果在同一查询的另一结果行中满足条件,是否有一种方法可以减少一个结果行中的值

时间:2019-04-17 09:58:59

标签: mysql

我有一张桌子(在现实生活中,这是一个复杂的查询,可为我提供此结果)。每行代表供应商不同的期限承诺:

--------------------------------------------------------------------------
| supplier | days_left | prod_type | prods_to_deliver | prods_in_transit |
--------------------------------------------------------------------------
|      1   |    20     |     1     |         9        |         7        |
--------------------------------------------------------------------------
|      1   |    20     |     2     |         5        |         3        |
--------------------------------------------------------------------------
|      1   |     5     |   NULL    |         7        |         4        |
--------------------------------------------------------------------------
|      1   |    20     |   NULL    |         9        |         6        |
--------------------------------------------------------------------------

棘手的是,每个供应商最多可以有两行, prod_type = NULL ,每行具有不同的 days_left 值。对于 prods_in_transit ,最高行也总是包含最低行的值,我必须对其进行更正。

(H)标记最高 days_left 值行,并用(L)标记最低值行。

我正在寻找一个SELECT,它将以以下方式改变我的结果: 如果同时存在(H)(L)行,则对于(H) prods_in_transit(H)= prods_in_transit (H)-prods_in_transit(L)

输出应如下所示:

--------------------------------------------------------------------------
| supplier | days_left | prod_type | prods_to_deliver | prods_in_transit |
--------------------------------------------------------------------------
|      1   |    20     |     1     |         9        |         7        |
--------------------------------------------------------------------------
|      1   |    20     |     2     |         5        |         3        |
--------------------------------------------------------------------------
|      1   |     5     |   NULL    |         7        |         4        |
--------------------------------------------------------------------------
|      1   |    20     |   NULL    |         9        |       >>2<<      |
--------------------------------------------------------------------------

对于最后一行的 prods_in_transit 值,应计算为 6-4 = 2

这应该是一个SQL查询,因为它将在大量不同的供应商上运行。我的示例仅说明了一个片段。用PHP foreach遍历每个供应商的结果也不是一种选择,因为这将花费很多时间。

理想情况下,我已经给了我一些代码,但实际上,我不知道从哪里开始。

谢谢。

1 个答案:

答案 0 :(得分:-1)

DROP TABLE IF EXISTS T;
CREATE TABLE T (SUPPLIERID INT, PRODS_TYPE INT, days_left int, PRODS_IN_TRANSIT INT);
INSERT INTO T VALUES
(1,1,1,2),(1,2,2,7),(1,NULL,5,4),(1,NULL,20,6),
(2,1,4,1),(2,NULL,7,10),
(3,1,6,3)
; 

SELECT SUPPLIERID,PRODS_TYPE,days_left,PRODS_IN_TRANSIT FROM T WHERE PRODS_TYPE IS NOT NULL
UNION ALL
SELECT SUPPLIERID,PRODS_TYPE,days_left,prods_in_transit 
        FROM T 
        WHERE PRODS_TYPE IS NULL 
        and prods_in_transit = 
        (select min(prods_in_transit) from t t1 where t1.supplierid = t.supplierid and t1.prods_type is null)
UNION ALL
(
select t.supplierid,t.prods_type,t.days_left,p
from t join
(
SELECT SUPPLIERID,PRODS_TYPE,max(prods_in_transit) maxint,MAX(PRODS_IN_TRANSIT) - MIN(PRODS_IN_TRANSIT) P 
        FROM T
        WHERE PRODS_TYPE IS NULL 
        GROUP BY SUPPLIERID,PRODS_TYPE HAVING COUNT(*) > 1 
) t1 on t1.SUPPLIERID = t.SUPPLIERID and t1.maxint = t.prods_in_transit
) 

ORDER BY SUPPLIERID;


+------------+------------+-----------+------------------+
| SUPPLIERID | PRODS_TYPE | days_left | PRODS_IN_TRANSIT |
+------------+------------+-----------+------------------+
|          1 |          1 |         1 |                2 |
|          1 |          2 |         2 |                7 |
|          1 |       NULL |         5 |                4 |
|          1 |       NULL |        20 |                2 |
|          2 |          1 |         4 |                1 |
|          2 |       NULL |         7 |               10 |
|          3 |          1 |         6 |                3 |
+------------+------------+-----------+------------------+
7 rows in set (0.00 sec)