如何合并第一和第二选择SUM语句

时间:2018-08-23 04:27:05

标签: mysql sql

第一个select语句在计划中检索汇总项数据,第二个select语句在实际执行时检索汇总项数据。如何运行第二条SQL语句而又不影响第一条SQL语句的结果

这是SQL提琴:Fiddle

第一个SQL选择

SELECT
    p.Content_Number AS PartNumber,
    p.Content_Name AS PartName,
    SUM(IF(p.ID_Packing IN(
                SELECT
                    pck0.ID_Packing
                FROM
                    tb_packing pck0
                WHERE
                    pck0.ID_Project_Item = 1
            ), p.Quantity, 0)) AS Quantity
FROM tb_packing_plan p
WHERE
    p.ID_Packing IN(
        SELECT pck.ID_Packing
        FROM tb_packing pck
        WHERE pck.ID_Project_Item = 1
    )
GROUP BY p.Content_Number
ORDER BY p.Content_Number;

第二个SQL选择

SELECT DISTINCT
    p.Content_Number AS PartNumber,
    p.Content_Name AS PartName,
    SUM(IF(r.ID_Packing IN(
                SELECT pck1.ID_Packing
                FROM tb_packing pck1
                WHERE pck1.ID_Project_Item = 1
            ), r.Quantity, 0)) AS Quantity2
FROM
    tb_packing_plan p
    LEFT JOIN tb_packing_real r
    ON  r.Content_Number = p.Content_Number
WHERE
    p.ID_Packing IN(
        SELECT pck.ID_Packing
        FROM tb_packing pck
        WHERE pck.ID_Project_Item = 1
    )
GROUP BY p.ID_Packing,
    p.Content_Number
ORDER BY p.Content_Number;

预期结果

|       PartNumber       |            PartName           | Quantity | Quantity2 |
|       1212121212       | Lorem Ipsum Dolor Sit Amet 12 |    12    |     6     |
|       1313131313       | Lorem Ipsum Dolor Sit Amet 13 |    2     |     2     |
|       1414141414       | Lorem Ipsum Dolor Sit Amet 14 |    6     |     5     |
|       1515151515       | Lorem Ipsum Dolor Sit Amet 15 |    2     |     0     |
|       2626262626       | Lorem Ipsum Dolor Sit Amet 26 |    9     |     6     |
|       2727272727       | Lorem Ipsum Dolor Sit Amet 27 |    12    |     11    |
|       3131313131       | Lorem Ipsum Dolor Sit Amet 31 |    5     |     0     |
|       3232323232       | Lorem Ipsum Dolor Sit Amet 32 |    4     |     4     |
|       3535353535       | Lorem Ipsum Dolor Sit Amet 35 |    5     |     5     |
|       3636363636       | Lorem Ipsum Dolor Sit Amet 36 |    2     |     0     |
|       3737373737       | Lorem Ipsum Dolor Sit Amet 37 |    5     |     0     |
|       3838383838       | Lorem Ipsum Dolor Sit Amet 38 |    8     |     3     |
|       4141414141       | Lorem Ipsum Dolor Sit Amet 41 |    7     |     0     |
|       4343434343       | Lorem Ipsum Dolor Sit Amet 43 |    3     |     0     |
|       4545454545       | Lorem Ipsum Dolor Sit Amet 45 |    3     |     0     |
|       4646464646       | Lorem Ipsum Dolor Sit Amet 46 |    3     |     0     |
| KND02S-D0001-0907-A7072| Manufacturing Part KND02S     |    5     |     5     |

1 个答案:

答案 0 :(得分:0)

只需加入两个查询

SELECT A.PartNumber,A.Partname,A.Quantity,B.Quantity2 FROM 
(
   SELECT p.Content_Number AS PartNumber, p.Content_Name AS PartName, SUM(IF(p.ID_Packing IN (SELECT pck0.ID_Packing FROM tb_packing pck0 WHERE pck0.ID_Project_Item = 1), p.Quantity, 0)) AS Quantity 
   FROM tb_packing_plan p
   WHERE p.ID_Packing IN (SELECT pck.ID_Packing FROM tb_packing pck WHERE pck.ID_Project_Item = 1)
   GROUP BY p.Content_Number
   ORDER BY p.Content_Number
)A JOIN 
(
    SELECT DISTINCT p.Content_Number AS PartNumber, p.Content_Name AS PartName, SUM(IF(r.ID_Packing IN (SELECT pck1.ID_Packing FROM tb_packing pck1 WHERE pck1.ID_Project_Item = 1), r.Quantity, 0)) AS Quantity2 
   FROM tb_packing_plan p LEFT JOIN tb_packing_real r ON r.Content_Number = p.Content_Number 
   WHERE p.ID_Packing IN (SELECT pck.ID_Packing FROM tb_packing pck WHERE pck.ID_Project_Item = 1) 
   GROUP BY p.ID_Packing ,p.Content_Number
   ORDER BY p.Content_Number
) B ON A.PartNumber = B.PartNumber

SQL Fiddle