其他分享
首页 > 其他分享> > 记录Union和Union All一些总结

记录Union和Union All一些总结

作者:互联网

在处理一个SQL的时候,遇到了一些问题,数据部分正确但是有一些怎么也不对。

首先,需要搞明白Union和Union All的区别

Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;

Union All:对两个结果集进行并集操作,包括重复行,不进行排序;

区别可以参照:

Create table Table1  
(  
    id int not null,  
    name varchar(20) not null  
)  
  
Create table Table2  
(  
    id int not null,  
    name varchar(20) not null  
)  

Insert into Table1 values (1,'小明')  
Insert into Table1 values (2,'边兵兵')  
Insert into Table1 values (3,'袁磊')  
  
Insert into Table2 values (1,'小明')  
Insert into Table2 values (2,'柳春平')  
Insert into Table2 values (3,'张永超')  
Insert into Table2 values (4,'刘华健')  

compare
-------------------------------------
select * from Table1  
union   
select * from Table2 
-------------------------------------
and
-------------------------------------
select * from Table1  
union all  
select * from Table2 
-------------------------------------

总结一下这里需要注意的一些坑:

数据字段应与第一个数据集的数据数量一致,并且一定要保证数据字段的顺序对应。

附修改修改的sql:

===============================================================================origin============================================================================
select board_type,float_line,resource,jumbo_plates_id,jumbo_plates_type,sqm,weight,total_weight,price,return_benifit,
        kfl_price,fj_price,heji_price,tax_rate,se_price,fjfws_price,kflws_price,type,case when LL_flag='1' then '纯来料' else LL_flag end as LL_flag,floatLine_name,
        parameter_name,customer_description,order_number,invoice_id,real_invoice_no,customer_id,jumbo_plates_thickness
from (

SELECT   pm.jumbo_plates_thickness, o.product_area AS board_type, ioi.float_line, pm.resource, pm.jumbo_plates_id, pm.jumbo_plates_type, SUM(id.quantity) AS sqm, 
                      SUM(ROUND(id.quantity * pm.jumbo_plates_thickness / 408.76, 3)) AS weight,SUM(id.total_weight) as total_weight, SUM(id.total_price) AS price, SUM(id.discount) AS return_benifit, SUM(id.total_price) 
                      - SUM(id.discount) AS kfl_price, SUM(id.processing_price) AS fj_price, SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price) AS heji_price,case when datediff(day,'2019-03-31',?) <=0 then 0.16 else 0.13 end as tax_rate, 
                      case when datediff(day,'2019-03-31',?) <=0 then ROUND(((SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price)) / (1 + 0.16))*0.16, 2) 
                           else ROUND(((SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price)) / (1 + 0.13))*0.13, 2) end  AS se_price
                      ,case when datediff(day,'2019-03-31',?) <=0 then ROUND(SUM(id.processing_price) / (1 + 0.16), 2) 
                           else  ROUND(SUM(id.processing_price) / (1 + 0.13), 2) end AS fjfws_price, 
                       case when datediff(day,'2019-03-31',?) <=0 then  SUM(id.total_price) - SUM(id.discount) - ROUND((SUM(id.total_price) - SUM(id.discount) / (1 + 0.16))*0.16, 2)
                          else SUM(id.total_price) - SUM(id.discount) - ROUND((SUM(id.total_price) - SUM(id.discount) / (1 + 0.13))*0.13, 2) end AS kflws_price, '1' AS type,'' as LL_flag, fm.floatLine_name, 
                      pam.product_area_name AS parameter_name,o.customer_description,o.processing_jumbo_order_number as order_number,id.invoice_id,rin.real_invoice_no,o.customer_id
FROM         dbo.invoice_order_item_detail AS id 
                    
                     inner join (SELECT     invoice_id, real_invoice_no
                            FROM          dbo.invoice_order
                            WHERE      (undo_flag = '0') AND (status IN ('3', '4')) AND (input_date >= ?) AND (input_date <= ?)) as rin on rin.invoice_id=id.invoice_id


                    INNER JOIN
/*
(select aa.quantity,aa.total_price,aa.discount,aa.processing_price,aa.tax_rate,bb.float_line,aa.jumbo_plates_id,aa.invoice_id,aa.order_id
from invoice_order_item_detail as aa
inner join 
(select float_line,jumbo_plates_stock_out_id
from jumbo_plates_stock_out
group by float_line,jumbo_plates_stock_out_id) as bb on aa.stock_out_id=bb.jumbo_plates_stock_out_id
) as id INNER JOIN
*/
                      dbo.jumbo_plates_main AS pm ON id.jumbo_plates_id = pm.jumbo_plates_id INNER JOIN
                          (SELECT     order_id, float_line, invoice_id,stock_out_id
                            FROM          dbo.invoice_order_item
                            GROUP BY order_id, float_line,invoice_id,stock_out_id) AS ioi ON ioi.order_id = id.order_id and ioi.invoice_id = id.invoice_id and ioi.stock_out_id=id.stock_out_id LEFT OUTER JOIN
                      dbo.floatLine_main AS fm ON ioi.float_line = fm.floatLine_id INNER JOIN
                          (SELECT     pjo.processing_jumbo_order_number, pjo.product_area, cm.customer_description,cm.customer_id
                            FROM          dbo.processing_jumbo_orders as pjo
                            inner join customer_main as cm on cm.customer_id=pjo.customer_id) AS o ON o.processing_jumbo_order_number = id.order_id LEFT OUTER JOIN
                      dbo.product_area_main AS pam ON o.product_area = pam.product_area_id
                     
WHERE      
                      (id.order_id LIKE 'YP%') 
                      --and (o.customer_description like ? or ? is null) and (o.processing_jumbo_order_number like ? or ? is null)
                      --and (pm.jumbo_plates_type like ? or ? is null) and (id.invoice_id like ? or ? is null) and (o.customer_id like ? or ? is null)
GROUP BY pam.product_area_name, fm.floatLine_name, pm.jumbo_plates_id, pm.jumbo_plates_type, pm.jumbo_plates_thickness, pm.resource, ioi.float_line, --id.tax_rate, 
                      o.product_area,o.customer_description,o.processing_jumbo_order_number,id.invoice_id,rin.real_invoice_no,o.customer_id
UNION ALL
SELECT     o_1.product_area AS board_type, id.float_line, '4' AS resource, '' AS jumbo_plates_id, '' AS jumbo_plates_thickness,'' AS jumbo_plates_type, 0 AS sqm, 0 AS weight,0 as total_weight, 0 AS price, 0 AS return_benifit, 
                      0 AS kfl_price, SUM(id.pe_package_price+id.jumbo_package_amount) AS fj_price, SUM(id.pe_package_price+id.jumbo_package_amount) AS heji_price,case when datediff(day,'2019-03-31',?) <=0 then 0.16 else 0.13 end  AS tax_rate,
                      case when datediff(day,'2019-03-31',?) <=0 then round((SUM(id.pe_package_price+id.jumbo_package_amount)/(1+0.16))*0.16,2) 
                          else round((SUM(id.pe_package_price+id.jumbo_package_amount)/(1+0.13))*0.13,2) end  AS se_price, 
                      case when datediff(day,'2019-03-31',?) <=0 then SUM(id.pe_package_price+id.jumbo_package_amount)-round((SUM(id.pe_package_price+id.jumbo_package_amount)/(1+0.16))*0.16,2) 
                          else SUM(id.pe_package_price+id.jumbo_package_amount)-round((SUM(id.pe_package_price+id.jumbo_package_amount)/(1+0.13))*0.13,2) end  AS fjfws_price, 0 AS kflws_price, '1' AS type,'' as LL_flag, fm.floatLine_name, 
                      pam.product_area_name AS parameter_name,o_1.customer_description,o_1.processing_jumbo_order_number as order_number,id.invoice_id,rin.real_invoice_no,o_1.customer_id
FROM         dbo.invoice_order_item AS id 
             inner join (SELECT     invoice_id, real_invoice_no
                            FROM          dbo.invoice_order AS invoice_order_1
                            WHERE      (undo_flag = '0') AND (status IN ('3', '4')) AND (input_date >= ?) AND (input_date <= ?)) as rin on rin.invoice_id=id.invoice_id

                        INNER JOIN
                          (SELECT     pjo.processing_jumbo_order_number, pjo.product_area, cm.customer_description,cm.customer_id
                            FROM          dbo.processing_jumbo_orders as pjo
                            inner join customer_main as cm on cm.customer_id=pjo.customer_id) AS o_1 ON o_1.processing_jumbo_order_number = id.order_id LEFT OUTER JOIN
                      dbo.product_area_main AS pam ON o_1.product_area = pam.product_area_id LEFT OUTER JOIN
                      dbo.floatLine_main AS fm ON id.float_line = fm.floatLine_id
                      left join
                      (select order_id,tax_rate,stock_out_id
                      from invoice_order_item_detail
                      group by order_id,tax_rate,stock_out_id) as dev1 on dev1.order_id=id.order_id and dev1.stock_out_id=id.stock_out_id
                     
WHERE     (ISNULL(id.pe_package_price, 0)+isnull(id.jumbo_package_amount,0) <> 0)  AND 
                      (id.order_id LIKE 'YP%') 
                      --and (o_1.customer_description like ? or ? is null) and (o_1.processing_jumbo_order_number like ? or ? is null)
                      --and ('' like ? or ? is null) and (id.invoice_id like ? or ? is null) and (o_1.customer_id like ? or ? is null)
GROUP BY id.float_line, pam.product_area_name, fm.floatLine_name, o_1.product_area,o_1.customer_description,o_1.processing_jumbo_order_number,id.invoice_id,rin.real_invoice_no,o_1.customer_id
UNION ALL
SELECT     o_2.invoice_area AS board_type, id.processing_machine_line as float_line, case when o_2.order_area='5' then '2' else '1' end AS resource, '' AS jumbo_plates_id, '' AS jumbo_plates_thickness,id.processing_product_desc AS jumbo_plates_type, SUM(id.quantity) AS sqm, 
                      SUM(ROUND(id.quantity * dev2.material_thickness / 408.76, 3)) AS weight,SUM(id.total_weight) as total_weight, SUM(id.total_price) AS price, SUM(id.discount) AS return_benifit, SUM(id.total_price) 
                      - SUM(id.discount) AS kfl_price, SUM(id.processing_price) AS fj_price, SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price) AS heji_price, case when datediff(day,'2019-03-31',?) <=0 then 0.16 else 0.13 end  as tax_rate, 
                      case when datediff(day,'2019-03-31',?) <=0 then ROUND(((SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price)) / (1 + 0.16))*0.16, 2) 
                           else ROUND(((SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price)) / (1 + 0.13))*0.13, 2) end AS se_price, 
                      case when datediff(day,'2019-03-31',?) <=0 then   ROUND(SUM(id.processing_price) / (1 + 0.16), 2) 
                          else  ROUND(SUM(id.processing_price) / (1 + 0.13), 2) end AS fjfws_price,
                       case when datediff(day,'2019-03-31',?) <=0 then SUM(id.total_price) - SUM(id.discount) - ROUND((SUM(id.total_price) - SUM(id.discount) / (1 + 0.16))*0.16, 2)
                          else SUM(id.total_price) - SUM(id.discount) - ROUND((SUM(id.total_price) - SUM(id.discount) / (1 + 0.13))*0.13, 2) end AS kflws_price, '2' AS type,'' as LL_flag, fm.floatLine_name, 
                      pp.parameter_name, o_2.customer_description,o_2.processing_order_number as order_number,id.invoice_id,rin.real_invoice_no,o_2.customer_id
FROM         dbo.invoice_order_item_detail AS id 

                inner join (SELECT     invoice_id,real_invoice_no
                            FROM          dbo.invoice_order AS invoice_order_2
                            WHERE      (undo_flag = '0') AND (status IN ('3', '4')) AND (input_date >= ?) AND (input_date <= ?)) as rin on rin.invoice_id=id.invoice_id


                    INNER JOIN
                          (SELECT     order_id, float_line,stock_out_id
                            FROM          dbo.invoice_order_item AS invoice_order_item_2
                            GROUP BY order_id, float_line,stock_out_id) AS ioi_2 ON ioi_2.order_id = id.order_id and ioi_2.stock_out_id=id.stock_out_id LEFT OUTER JOIN
                      dbo.floatLine_main AS fm ON id.processing_machine_line = fm.floatLine_id INNER JOIN
                          (SELECT     pjo.processing_order_number, pjo.product_desc, pjo.invoice_area, cm.customer_description,cm.customer_id,pjo.order_area
                            FROM          dbo.processing_orders as pjo
                            inner join customer_main as cm on cm.customer_id=pjo.customer_number) AS o_2 ON o_2.processing_order_number = id.order_id INNER JOIN
                          (SELECT     processing_order_number, SUM(material_thickness) AS material_thickness
                            FROM          dbo.processing_order_items AS i
                            WHERE      /*EXISTS
                                                       (SELECT     processing_order_number, MIN(processing_order_item_number) AS min_item_number
                                                         FROM          dbo.processing_product AS p
                                                         WHERE      (processing_order_number = i.processing_order_number)
                                                         GROUP BY processing_order_number
                                                         HAVING      (MIN(processing_order_item_number) = i.processing_order_item_number)) */
                                            processing_order_item_number = '1'
                                            AND (product_type=1) AND (SUBSTRING(material_number, 1, 
                                                   2) <> 'LL')
                            GROUP BY processing_order_number) AS dev2 ON dev2.processing_order_number = id.order_id LEFT OUTER JOIN
                          (SELECT     parameter_id, parameter_name
                            FROM          dbo.pmpi_parameter
                            WHERE      (parameter_type_id = '1019')) AS pp ON pp.parameter_id = o_2.invoice_area
                            
    WHERE     
                      (id.order_id LIKE 'RW%') 
                      --and (o_2.customer_description like ? or ? is null) and (o_2.processing_order_number like ? or ? is null)
                      --and (id.processing_product_desc like ? or ? is null) and (id.invoice_id like ? or ? is null) and (o_2.customer_id like ? or ? is null)
GROUP BY fm.floatLine_name, id.processing_machine_line,  o_2.invoice_area, id.processing_product_desc, pp.parameter_name, o_2.customer_description,o_2.processing_order_number,id.invoice_id,rin.real_invoice_no,o_2.customer_id,o_2.order_area
UNION ALL
SELECT     o_1_1.invoice_area AS board_type, id.processing_machine_line as float_line, case when o_1_1.order_area='5' then '2' else '1' end AS resource, '' AS jumbo_plates_id, '' AS jumbo_plates_thickness,id.processing_product_desc AS jumbo_plates_type, SUM(id.quantity) 
                      AS sqm, SUM(ROUND(id.quantity * dev2_1.material_thickness / 408.76, 3)) AS weight,SUM(id.total_weight) as total_weight, SUM(id.total_price) AS price, SUM(id.discount) AS return_benifit, 
                      SUM(id.total_price) - SUM(id.discount) AS kfl_price, SUM(id.processing_price) AS fj_price, SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price) 
                      AS heji_price, case when datediff(day,'2019-03-31',?) <=0 then 0.16 else 0.13 end  as tax_rate, 
                      case when datediff(day,'2019-03-31',?) <=0 then ROUND(((SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price)) / (1 + 0.16))*0.16, 2) 
                            else ROUND(((SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price)) / (1 + 0.13))*0.13, 2) end AS se_price, 
                      case when datediff(day,'2019-03-31',?) <=0 then ROUND(SUM(id.processing_price) / (1 + 0.16), 2) 
                            else ROUND(SUM(id.processing_price) / (1 + 0.13), 2) end AS fjfws_price, 
                      case when datediff(day,'2019-03-31',?) <=0 then  SUM(id.total_price) - SUM(id.discount) - ROUND((SUM(id.total_price) - SUM(id.discount) / (1 + 0.16))*0.16, 2) 
                            else SUM(id.total_price) - SUM(id.discount) - ROUND((SUM(id.total_price) - SUM(id.discount) / (1 + 0.13))*0.13, 2) end AS kflws_price, '2' AS type,'1' as LL_flag, 
                      fm.floatLine_name, pp_1.parameter_name, o_1_1.customer_description,o_1_1.processing_order_number as order_number,id.invoice_id,rin.real_invoice_no,o_1_1.customer_id
FROM         dbo.invoice_order_item_detail AS id 
                    inner join (SELECT     invoice_id, real_invoice_no
                            FROM          dbo.invoice_order AS invoice_order_1
                            WHERE      (undo_flag = '0') AND (status IN ('3', '4')) AND (input_date >= ?) AND (input_date <= ?)) as rin on rin.invoice_id=id.invoice_id


                        INNER JOIN
                          (SELECT     order_id, float_line,stock_out_id
                            FROM          dbo.invoice_order_item AS invoice_order_item_1
                            GROUP BY order_id, float_line,stock_out_id) AS ioi_1 ON ioi_1.order_id = id.order_id and ioi_1.stock_out_id=id.stock_out_id LEFT OUTER JOIN
                      dbo.floatLine_main AS fm ON id.processing_machine_line = fm.floatLine_id INNER JOIN
                          (SELECT     pjo.processing_order_number, pjo.product_desc, pjo.invoice_area, cm.customer_description,cm.customer_id,pjo.order_area
                            FROM          dbo.processing_orders as pjo
                            inner join customer_main as cm on cm.customer_id=pjo.customer_number) AS o_1_1 ON o_1_1.processing_order_number = id.order_id 
                          
                          
                          INNER JOIN
                           --纯来料的厚度
                          (SELECT     dev0_1.processing_order_number, dev0_1.material_thickness
                            FROM          (SELECT     processing_order_number, /*COUNT(group_id) AS c,*/ SUM(material_thickness) AS material_thickness
                                                    FROM          dbo.processing_order_items AS processing_order_items_2
                                                    WHERE      (product_type = '1') AND (processing_order_item_number = '1') AND (SUBSTRING(material_number, 1, 2) = 'LL')
                                                    GROUP BY processing_order_number) AS dev0_1
                                                     
                                             inner join (select processing_order_number,jumbo_plates_thickness from processing_product where processing_order_item_number = '1') as dev1_2_1
                                            
                                            on (dev0_1.material_thickness = dev1_2_1.jumbo_plates_thickness) and (dev0_1.processing_order_number = dev1_2_1.processing_order_number)
                                                    
                                                   /*LEFT OUTER JOIN
                                                 (SELECT     processing_order_number, COUNT(group_id) AS c
                                                   FROM          dbo.processing_order_items AS processing_order_items_1
                                                   WHERE      (product_type = '1') AND (processing_order_item_number = '1') AND (SUBSTRING(material_number, 1, 2) = 'LL')
                                                   GROUP BY processing_order_number) AS dev1_2_1 ON dev0_1.processing_order_number = dev1_2_1.processing_order_number*/
                                                   
                            ) AS dev2_1 ON dev2_1.processing_order_number = id.order_id 
                            
                            
                            LEFT OUTER JOIN
                          (SELECT     parameter_id, parameter_name
                            FROM          dbo.pmpi_parameter AS pmpi_parameter_1
                            WHERE      (parameter_type_id = '1019')) AS pp_1 ON pp_1.parameter_id = o_1_1.invoice_area
                            
WHERE     
                      (id.order_id LIKE 'RW%') 
                      --and (o_1_1.customer_description like ? or ? is null) and (o_1_1.processing_order_number like ? or ? is null)
                      --and (id.processing_product_desc like ? or ? is null) and (id.invoice_id like ? or ? is null) and (o_1_1.customer_id like ? or ? is null)
GROUP BY fm.floatLine_name, id.processing_machine_line,  o_1_1.invoice_area, id.processing_product_desc, pp_1.parameter_name, o_1_1.customer_description,o_1_1.processing_order_number,id.invoice_id,rin.real_invoice_no,o_1_1.customer_id,o_1_1.order_area
) as dev
where (customer_description like ? or ? is null) and (order_number like ? or ? is null) and (jumbo_plates_type like ? or ? is null)
        and (invoice_id like ? or ? is null) and (customer_id like ? or ? is null)
        
===============================================================================part01============================================================================
SELECT
    pm.jumbo_plates_thickness, o.product_area AS board_type, ioi.float_line, pm.resource, pm.jumbo_plates_id, pm.jumbo_plates_type, SUM(id.quantity) AS sqm,
    SUM(ROUND(id.quantity * pm.jumbo_plates_thickness / 408.76, 3)) AS weight, SUM(id.total_weight) AS total_weight, SUM(id.total_price) AS price, SUM(id.discount) AS return_benifit, SUM(id.total_price)
    - SUM(id.discount) AS kfl_price, SUM(id.processing_price) AS fj_price, SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price) AS heji_price, CASE
        WHEN DATEDIFF(DAY, '2019-03-31', '2020-05-21') <= 0 THEN 0.16 ELSE 0.13
    END AS tax_rate,
    CASE
        WHEN DATEDIFF(DAY, '2019-03-31', '2020-05-21') <= 0 THEN ROUND(((SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price)) / (1 + 0.16)) * 0.16, 2) ELSE ROUND(((SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price)) / (1 + 0.13)) * 0.13, 2)
    END AS se_price
    , CASE
        WHEN DATEDIFF(DAY, '2019-03-31', '2020-05-21') <= 0 THEN ROUND(SUM(id.processing_price) / (1 + 0.16), 2) ELSE ROUND(SUM(id.processing_price) / (1 + 0.13), 2)
    END AS fjfws_price,
    CASE
        WHEN DATEDIFF(DAY, '2019-03-31', '2020-05-21') <= 0 THEN SUM(id.total_price) - SUM(id.discount) - ROUND((SUM(id.total_price) - SUM(id.discount) / (1 + 0.16)) * 0.16, 2) ELSE SUM(id.total_price) - SUM(id.discount) - ROUND((SUM(id.total_price) - SUM(id.discount) / (1 + 0.13)) * 0.13, 2)
    END AS kflws_price, '1' AS type, '' AS LL_flag, fm.floatLine_name,
    pam.product_area_name AS parameter_name, o.customer_description, o.processing_jumbo_order_number AS order_number, id.invoice_id, rin.real_invoice_no, o.customer_id
FROM dbo.invoice_order_item_detail AS id

INNER JOIN (SELECT
        invoice_id, real_invoice_no
    FROM dbo.invoice_order
    WHERE (undo_flag = '0') AND (status IN ('3', '4')) AND (input_date >= '2020-05-01') AND (input_date <= '2020-05-21')) AS rin
    ON rin.invoice_id = id.invoice_id


INNER JOIN /*
            (select aa.quantity,aa.total_price,aa.discount,aa.processing_price,aa.tax_rate,bb.float_line,aa.jumbo_plates_id,aa.invoice_id,aa.order_id
            from invoice_order_item_detail as aa
            inner join 
            (select float_line,jumbo_plates_stock_out_id
            from jumbo_plates_stock_out
            group by float_line,jumbo_plates_stock_out_id) as bb on aa.stock_out_id=bb.jumbo_plates_stock_out_id
            ) as id INNER JOIN
            */
dbo.jumbo_plates_main AS pm
    ON id.jumbo_plates_id = pm.jumbo_plates_id
INNER JOIN (SELECT
        order_id, float_line, invoice_id, stock_out_id
    FROM dbo.invoice_order_item
    GROUP BY order_id, float_line, invoice_id, stock_out_id) AS ioi
    ON ioi.order_id = id.order_id AND ioi.invoice_id = id.invoice_id AND ioi.stock_out_id = id.stock_out_id
LEFT OUTER JOIN dbo.floatLine_main AS fm
    ON ioi.float_line = fm.floatLine_id
INNER JOIN (SELECT
        pjo.processing_jumbo_order_number, pjo.product_area, cm.customer_description, cm.customer_id
    FROM dbo.processing_jumbo_orders AS pjo
    INNER JOIN customer_main AS cm
        ON cm.customer_id = pjo.customer_id) AS o
    ON o.processing_jumbo_order_number = id.order_id
LEFT OUTER JOIN dbo.product_area_main AS pam
    ON o.product_area = pam.product_area_id

WHERE (id.order_id LIKE 'YP%')
--and (o.customer_description like ? or ? is null) and (o.processing_jumbo_order_number like ? or ? is null)
--and (pm.jumbo_plates_type like ? or ? is null) and (id.invoice_id like ? or ? is null) and (o.customer_id like ? or ? is null)
GROUP BY pam.product_area_name, fm.floatLine_name, pm.jumbo_plates_id, pm.jumbo_plates_type, pm.jumbo_plates_thickness, pm.resource, ioi.float_line, --id.tax_rate, 
o.product_area, o.customer_description, o.processing_jumbo_order_number, id.invoice_id, rin.real_invoice_no, o.customer_id    
===============================================================================part01============================================================================
===============================================================================part02============================================================================    
SELECT
    o_1.product_area AS board_type, id.float_line, '4' AS resource, '' AS jumbo_plates_id, '' AS jumbo_plates_thickness, '' AS jumbo_plates_type, 0 AS sqm, 0 AS weight, 0 AS total_weight, 0 AS price, 0 AS return_benifit,
    0 AS kfl_price, SUM(id.pe_package_price + id.jumbo_package_amount) AS fj_price, SUM(id.pe_package_price + id.jumbo_package_amount) AS heji_price, CASE
        WHEN DATEDIFF(DAY, '2019-03-31', '2020-05-21') <= 0 THEN 0.16 ELSE 0.13
    END AS tax_rate,
    CASE
        WHEN DATEDIFF(DAY, '2019-03-31', '2020-05-21') <= 0 THEN ROUND((SUM(id.pe_package_price + id.jumbo_package_amount) / (1 + 0.16)) * 0.16, 2) ELSE ROUND((SUM(id.pe_package_price + id.jumbo_package_amount) / (1 + 0.13)) * 0.13, 2)
    END AS se_price,
    CASE
        WHEN DATEDIFF(DAY, '2019-03-31', '2020-05-21') <= 0 THEN SUM(id.pe_package_price + id.jumbo_package_amount) - ROUND((SUM(id.pe_package_price + id.jumbo_package_amount) / (1 + 0.16)) * 0.16, 2) ELSE SUM(id.pe_package_price + id.jumbo_package_amount) - ROUND((SUM(id.pe_package_price + id.jumbo_package_amount) / (1 + 0.13)) * 0.13, 2)
    END AS fjfws_price, 0 AS kflws_price, '1' AS type, '' AS LL_flag, fm.floatLine_name,
    pam.product_area_name AS parameter_name, o_1.customer_description, o_1.processing_jumbo_order_number AS order_number, id.invoice_id, rin.real_invoice_no, o_1.customer_id
FROM dbo.invoice_order_item AS id
INNER JOIN (SELECT
        invoice_id, real_invoice_no
    FROM dbo.invoice_order AS invoice_order_1
    WHERE (undo_flag = '0') AND (status IN ('3', '4')) AND (input_date >= '2020-05-01') AND (input_date <= '2020-05-21')) AS rin
    ON rin.invoice_id = id.invoice_id

INNER JOIN (SELECT
        pjo.processing_jumbo_order_number, pjo.product_area, cm.customer_description, cm.customer_id
    FROM dbo.processing_jumbo_orders AS pjo
    INNER JOIN customer_main AS cm
        ON cm.customer_id = pjo.customer_id) AS o_1
    ON o_1.processing_jumbo_order_number = id.order_id
LEFT OUTER JOIN dbo.product_area_main AS pam
    ON o_1.product_area = pam.product_area_id
LEFT OUTER JOIN dbo.floatLine_main AS fm
    ON id.float_line = fm.floatLine_id
LEFT JOIN (SELECT
        order_id, tax_rate, stock_out_id
    FROM invoice_order_item_detail
    GROUP BY order_id, tax_rate, stock_out_id) AS dev1
    ON dev1.order_id = id.order_id AND dev1.stock_out_id = id.stock_out_id

WHERE (ISNULL(id.pe_package_price, 0) + ISNULL(id.jumbo_package_amount, 0) <> 0) AND
(id.order_id LIKE 'YP%')
--and (o_1.customer_description like ? or ? is null) and (o_1.processing_jumbo_order_number like ? or ? is null)
--and ('' like ? or ? is null) and (id.invoice_id like ? or ? is null) and (o_1.customer_id like ? or ? is null)
GROUP BY id.float_line, pam.product_area_name, fm.floatLine_name, o_1.product_area, o_1.customer_description, o_1.processing_jumbo_order_number, id.invoice_id, rin.real_invoice_no, o_1.customer_id    
===============================================================================part02============================================================================
===============================================================================part03============================================================================    
SELECT
    o_2.invoice_area AS board_type, id.processing_machine_line AS float_line, CASE
        WHEN o_2.order_area = '5' THEN '2' ELSE '1'
    END AS resource, '' AS jumbo_plates_id, '' AS jumbo_plates_thickness, id.processing_product_desc AS jumbo_plates_type, SUM(id.quantity) AS sqm,
    SUM(ROUND(id.quantity * dev2.material_thickness / 408.76, 3)) AS weight, SUM(id.total_weight) AS total_weight, SUM(id.total_price) AS price, SUM(id.discount) AS return_benifit, SUM(id.total_price)
    - SUM(id.discount) AS kfl_price, SUM(id.processing_price) AS fj_price, SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price) AS heji_price, CASE
        WHEN DATEDIFF(DAY, '2019-03-31', '2020-05-21') <= 0 THEN 0.16 ELSE 0.13
    END AS tax_rate,
    CASE
        WHEN DATEDIFF(DAY, '2019-03-31', '2020-05-21') <= 0 THEN ROUND(((SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price)) / (1 + 0.16)) * 0.16, 2) ELSE ROUND(((SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price)) / (1 + 0.13)) * 0.13, 2)
    END AS se_price,
    CASE
        WHEN DATEDIFF(DAY, '2019-03-31', '2020-05-21') <= 0 THEN ROUND(SUM(id.processing_price) / (1 + 0.16), 2) ELSE ROUND(SUM(id.processing_price) / (1 + 0.13), 2)
    END AS fjfws_price,
    CASE
        WHEN DATEDIFF(DAY, '2019-03-31', '2020-05-21') <= 0 THEN SUM(id.total_price) - SUM(id.discount) - ROUND((SUM(id.total_price) - SUM(id.discount) / (1 + 0.16)) * 0.16, 2) ELSE SUM(id.total_price) - SUM(id.discount) - ROUND((SUM(id.total_price) - SUM(id.discount) / (1 + 0.13)) * 0.13, 2)
    END AS kflws_price, '2' AS type, '' AS LL_flag, fm.floatLine_name,
    pp.parameter_name, o_2.customer_description, o_2.processing_order_number AS order_number, id.invoice_id, rin.real_invoice_no, o_2.customer_id
FROM dbo.invoice_order_item_detail AS id

INNER JOIN (SELECT
        invoice_id, real_invoice_no
    FROM dbo.invoice_order AS invoice_order_2
    WHERE (undo_flag = '0') AND (status IN ('3', '4')) AND (input_date >= '2020-05-01') AND (input_date <= '2020-05-21')) AS rin
    ON rin.invoice_id = id.invoice_id


INNER JOIN (SELECT
        order_id, float_line, stock_out_id
    FROM dbo.invoice_order_item AS invoice_order_item_2
    GROUP BY order_id, float_line, stock_out_id) AS ioi_2
    ON ioi_2.order_id = id.order_id AND ioi_2.stock_out_id = id.stock_out_id
LEFT OUTER JOIN dbo.floatLine_main AS fm
    ON id.processing_machine_line = fm.floatLine_id
INNER JOIN (SELECT
        pjo.processing_order_number, pjo.product_desc, pjo.invoice_area, cm.customer_description, cm.customer_id, pjo.order_area
    FROM dbo.processing_orders AS pjo
    INNER JOIN customer_main AS cm
        ON cm.customer_id = pjo.customer_number) AS o_2
    ON o_2.processing_order_number = id.order_id
INNER JOIN (SELECT
        processing_order_number, SUM(material_thickness) AS material_thickness
    FROM dbo.processing_order_items AS i
    WHERE /*EXISTS
                          (SELECT     processing_order_number, MIN(processing_order_item_number) AS min_item_number
                            FROM          dbo.processing_product AS p
                            WHERE      (processing_order_number = i.processing_order_number)
                            GROUP BY processing_order_number
                            HAVING      (MIN(processing_order_item_number) = i.processing_order_item_number)) */
    processing_order_item_number = '1'
    AND (product_type = 1) AND (SUBSTRING(material_number, 1,
    2) <> 'LL')
    GROUP BY processing_order_number) AS dev2
    ON dev2.processing_order_number = id.order_id
LEFT OUTER JOIN (SELECT
        parameter_id, parameter_name
    FROM dbo.pmpi_parameter
    WHERE (parameter_type_id = '1019')) AS pp
    ON pp.parameter_id = o_2.invoice_area

WHERE (id.order_id LIKE 'RW%')
--and (o_2.customer_description like ? or ? is null) and (o_2.processing_order_number like ? or ? is null)
--and (id.processing_product_desc like ? or ? is null) and (id.invoice_id like ? or ? is null) and (o_2.customer_id like ? or ? is null)
GROUP BY fm.floatLine_name, id.processing_machine_line, o_2.invoice_area, id.processing_product_desc, pp.parameter_name, o_2.customer_description, o_2.processing_order_number, id.invoice_id, rin.real_invoice_no, o_2.customer_id, o_2.order_area    
===============================================================================part03============================================================================
===============================================================================part04============================================================================
SELECT
    o_1_1.invoice_area AS board_type, id.processing_machine_line AS float_line, CASE
        WHEN o_1_1.order_area = '5' THEN '2' ELSE '1'
    END AS resource, '' AS jumbo_plates_id, '' AS jumbo_plates_thickness, id.processing_product_desc AS jumbo_plates_type, SUM(id.quantity)
    AS sqm, SUM(ROUND(id.quantity * dev2_1.material_thickness / 408.76, 3)) AS weight, SUM(id.total_weight) AS total_weight, SUM(id.total_price) AS price, SUM(id.discount) AS return_benifit,
    SUM(id.total_price) - SUM(id.discount) AS kfl_price, SUM(id.processing_price) AS fj_price, SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price)
    AS heji_price, CASE
        WHEN DATEDIFF(DAY, '2019-03-31', '2020-05-21') <= 0 THEN 0.16 ELSE 0.13
    END AS tax_rate,
    CASE
        WHEN DATEDIFF(DAY, '2019-03-31', '2020-05-21') <= 0 THEN ROUND(((SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price)) / (1 + 0.16)) * 0.16, 2) ELSE ROUND(((SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price)) / (1 + 0.13)) * 0.13, 2)
    END AS se_price,
    CASE
        WHEN DATEDIFF(DAY, '2019-03-31', '2020-05-21') <= 0 THEN ROUND(SUM(id.processing_price) / (1 + 0.16), 2) ELSE ROUND(SUM(id.processing_price) / (1 + 0.13), 2)
    END AS fjfws_price,
    CASE
        WHEN DATEDIFF(DAY, '2019-03-31', '2020-05-21') <= 0 THEN SUM(id.total_price) - SUM(id.discount) - ROUND((SUM(id.total_price) - SUM(id.discount) / (1 + 0.16)) * 0.16, 2) ELSE SUM(id.total_price) - SUM(id.discount) - ROUND((SUM(id.total_price) - SUM(id.discount) / (1 + 0.13)) * 0.13, 2)
    END AS kflws_price, '2' AS type, '1' AS LL_flag,
    fm.floatLine_name, pp_1.parameter_name, o_1_1.customer_description, o_1_1.processing_order_number AS order_number, id.invoice_id, rin.real_invoice_no, o_1_1.customer_id
FROM dbo.invoice_order_item_detail AS id
INNER JOIN (SELECT
        invoice_id, real_invoice_no
    FROM dbo.invoice_order AS invoice_order_1
    WHERE (undo_flag = '0') AND (status IN ('3', '4')) AND (input_date >= '2020-05-01') AND (input_date <= '2020-05-21')) AS rin
    ON rin.invoice_id = id.invoice_id


INNER JOIN (SELECT
        order_id, float_line, stock_out_id
    FROM dbo.invoice_order_item AS invoice_order_item_1
    GROUP BY order_id, float_line, stock_out_id) AS ioi_1
    ON ioi_1.order_id = id.order_id AND ioi_1.stock_out_id = id.stock_out_id
LEFT OUTER JOIN dbo.floatLine_main AS fm
    ON id.processing_machine_line = fm.floatLine_id
INNER JOIN (SELECT
        pjo.processing_order_number, pjo.product_desc, pjo.invoice_area, cm.customer_description, cm.customer_id, pjo.order_area
    FROM dbo.processing_orders AS pjo
    INNER JOIN customer_main AS cm
        ON cm.customer_id = pjo.customer_number) AS o_1_1
    ON o_1_1.processing_order_number = id.order_id


INNER JOIN --纯来料的厚度
(SELECT
        dev0_1.processing_order_number, dev0_1.material_thickness
    FROM (SELECT
            processing_order_number, /*COUNT(group_id) AS c,*/ SUM(material_thickness) AS material_thickness
        FROM dbo.processing_order_items AS processing_order_items_2
        WHERE (product_type = '1') AND (processing_order_item_number = '1') AND (SUBSTRING(material_number, 1, 2) = 'LL')
        GROUP BY processing_order_number) AS dev0_1

    INNER JOIN (SELECT
            processing_order_number, jumbo_plates_thickness
        FROM processing_product
        WHERE processing_order_item_number = '1') AS dev1_2_1

        ON (dev0_1.material_thickness = dev1_2_1.jumbo_plates_thickness) AND (dev0_1.processing_order_number = dev1_2_1.processing_order_number) /*LEFT OUTER JOIN
                                                                                                 (SELECT     processing_order_number, COUNT(group_id) AS c
                                                                                                   FROM          dbo.processing_order_items AS processing_order_items_1
                                                                                                   WHERE      (product_type = '1') AND (processing_order_item_number = '1') AND (SUBSTRING(material_number, 1, 2) = 'LL')
                                                                                                   GROUP BY processing_order_number) AS dev1_2_1 ON dev0_1.processing_order_number = dev1_2_1.processing_order_number*/

) AS dev2_1
    ON dev2_1.processing_order_number = id.order_id


LEFT OUTER JOIN (SELECT
        parameter_id, parameter_name
    FROM dbo.pmpi_parameter AS pmpi_parameter_1
    WHERE (parameter_type_id = '1019')) AS pp_1
    ON pp_1.parameter_id = o_1_1.invoice_area

WHERE (id.order_id LIKE 'RW%')
--and (o_1_1.customer_description like ? or ? is null) and (o_1_1.processing_order_number like ? or ? is null)
--and (id.processing_product_desc like ? or ? is null) and (id.invoice_id like ? or ? is null) and (o_1_1.customer_id like ? or ? is null)
GROUP BY fm.floatLine_name, id.processing_machine_line, o_1_1.invoice_area, id.processing_product_desc, pp_1.parameter_name, o_1_1.customer_description, o_1_1.processing_order_number, id.invoice_id, rin.real_invoice_no, o_1_1.customer_id, o_1_1.order_area
===============================================================================part04============================================================================
===============================================================================partAll============================================================================

完成后:

select board_type,float_line,resource,jumbo_plates_id,jumbo_plates_type,sqm,weight,total_weight,price,return_benifit,
        kfl_price,fj_price,heji_price,tax_rate,se_price,fjfws_price,kflws_price,type,case when LL_flag='1' then '纯来料' else LL_flag end as LL_flag,floatLine_name,
        parameter_name,customer_description,order_number,invoice_id,real_invoice_no,customer_id,jumbo_plates_thickness
from (

SELECT   pm.jumbo_plates_thickness, o.product_area AS board_type, ioi.float_line, pm.resource, pm.jumbo_plates_id, pm.jumbo_plates_type, SUM(id.quantity) AS sqm, 
                      SUM(ROUND(id.quantity * pm.jumbo_plates_thickness / 408.76, 3)) AS weight,SUM(id.total_weight) as total_weight, SUM(id.total_price) AS price, SUM(id.discount) AS return_benifit, SUM(id.total_price) 
                      - SUM(id.discount) AS kfl_price, SUM(id.processing_price) AS fj_price, SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price) AS heji_price,case when datediff(day,'2019-03-31',?) <=0 then 0.16 else 0.13 end as tax_rate, 
                      case when datediff(day,'2019-03-31',?) <=0 then ROUND(((SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price)) / (1 + 0.16))*0.16, 2) 
                           else ROUND(((SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price)) / (1 + 0.13))*0.13, 2) end  AS se_price
                      ,case when datediff(day,'2019-03-31',?) <=0 then ROUND(SUM(id.processing_price) / (1 + 0.16), 2) 
                           else  ROUND(SUM(id.processing_price) / (1 + 0.13), 2) end AS fjfws_price, 
                       case when datediff(day,'2019-03-31',?) <=0 then  SUM(id.total_price) - SUM(id.discount) - ROUND((SUM(id.total_price) - SUM(id.discount) / (1 + 0.16))*0.16, 2)
                          else SUM(id.total_price) - SUM(id.discount) - ROUND((SUM(id.total_price) - SUM(id.discount) / (1 + 0.13))*0.13, 2) end AS kflws_price, '1' AS type,'' as LL_flag, fm.floatLine_name, 
                      pam.product_area_name AS parameter_name,o.customer_description,o.processing_jumbo_order_number as order_number,id.invoice_id,rin.real_invoice_no,o.customer_id
FROM         dbo.invoice_order_item_detail AS id 
                    
                     inner join (SELECT     invoice_id, real_invoice_no
                            FROM          dbo.invoice_order
                            WHERE      (undo_flag = '0') AND (status IN ('3', '4')) AND (input_date >= ?) AND (input_date <= ?)) as rin on rin.invoice_id=id.invoice_id


                    INNER JOIN
/*
(select aa.quantity,aa.total_price,aa.discount,aa.processing_price,aa.tax_rate,bb.float_line,aa.jumbo_plates_id,aa.invoice_id,aa.order_id
from invoice_order_item_detail as aa
inner join 
(select float_line,jumbo_plates_stock_out_id
from jumbo_plates_stock_out
group by float_line,jumbo_plates_stock_out_id) as bb on aa.stock_out_id=bb.jumbo_plates_stock_out_id
) as id INNER JOIN
*/
                      dbo.jumbo_plates_main AS pm ON id.jumbo_plates_id = pm.jumbo_plates_id INNER JOIN
                          (SELECT     order_id, float_line, invoice_id,stock_out_id
                            FROM          dbo.invoice_order_item
                            GROUP BY order_id, float_line,invoice_id,stock_out_id) AS ioi ON ioi.order_id = id.order_id and ioi.invoice_id = id.invoice_id and ioi.stock_out_id=id.stock_out_id LEFT OUTER JOIN
                      dbo.floatLine_main AS fm ON ioi.float_line = fm.floatLine_id INNER JOIN
                          (SELECT     pjo.processing_jumbo_order_number, pjo.product_area, cm.customer_description,cm.customer_id
                            FROM          dbo.processing_jumbo_orders as pjo
                            inner join customer_main as cm on cm.customer_id=pjo.customer_id) AS o ON o.processing_jumbo_order_number = id.order_id LEFT OUTER JOIN
                      dbo.product_area_main AS pam ON o.product_area = pam.product_area_id
                     
WHERE      
                      (id.order_id LIKE 'YP%') 
                      --and (o.customer_description like ? or ? is null) and (o.processing_jumbo_order_number like ? or ? is null)
                      --and (pm.jumbo_plates_type like ? or ? is null) and (id.invoice_id like ? or ? is null) and (o.customer_id like ? or ? is null)
GROUP BY pam.product_area_name, fm.floatLine_name, pm.jumbo_plates_id, pm.jumbo_plates_type, pm.jumbo_plates_thickness, pm.resource, ioi.float_line, --id.tax_rate, 
                      o.product_area,o.customer_description,o.processing_jumbo_order_number,id.invoice_id,rin.real_invoice_no,o.customer_id
UNION ALL
SELECT     0 AS jumbo_plates_thickness,o_1.product_area AS board_type, id.float_line, '4' AS resource, '' AS jumbo_plates_id, '' AS jumbo_plates_type, 0 AS sqm, 0 AS weight,0 as total_weight, 0 AS price, 0 AS return_benifit, 
                      0 AS kfl_price, SUM(id.pe_package_price+id.jumbo_package_amount) AS fj_price, SUM(id.pe_package_price+id.jumbo_package_amount) AS heji_price,case when datediff(day,'2019-03-31',?) <=0 then 0.16 else 0.13 end  AS tax_rate,
                      case when datediff(day,'2019-03-31',?) <=0 then round((SUM(id.pe_package_price+id.jumbo_package_amount)/(1+0.16))*0.16,2) 
                          else round((SUM(id.pe_package_price+id.jumbo_package_amount)/(1+0.13))*0.13,2) end  AS se_price, 
                      case when datediff(day,'2019-03-31',?) <=0 then SUM(id.pe_package_price+id.jumbo_package_amount)-round((SUM(id.pe_package_price+id.jumbo_package_amount)/(1+0.16))*0.16,2) 
                          else SUM(id.pe_package_price+id.jumbo_package_amount)-round((SUM(id.pe_package_price+id.jumbo_package_amount)/(1+0.13))*0.13,2) end  AS fjfws_price, 0 AS kflws_price, '1' AS type,'' as LL_flag, fm.floatLine_name, 
                      pam.product_area_name AS parameter_name,o_1.customer_description,o_1.processing_jumbo_order_number as order_number,id.invoice_id,rin.real_invoice_no,o_1.customer_id
FROM         dbo.invoice_order_item AS id 
             inner join (SELECT     invoice_id, real_invoice_no
                            FROM          dbo.invoice_order AS invoice_order_1
                            WHERE      (undo_flag = '0') AND (status IN ('3', '4')) AND (input_date >= ?) AND (input_date <= ?)) as rin on rin.invoice_id=id.invoice_id

                        INNER JOIN
                          (SELECT     pjo.processing_jumbo_order_number, pjo.product_area, cm.customer_description,cm.customer_id
                            FROM          dbo.processing_jumbo_orders as pjo
                            inner join customer_main as cm on cm.customer_id=pjo.customer_id) AS o_1 ON o_1.processing_jumbo_order_number = id.order_id LEFT OUTER JOIN
                      dbo.product_area_main AS pam ON o_1.product_area = pam.product_area_id LEFT OUTER JOIN
                      dbo.floatLine_main AS fm ON id.float_line = fm.floatLine_id
                      left join
                      (select order_id,tax_rate,stock_out_id
                      from invoice_order_item_detail
                      group by order_id,tax_rate,stock_out_id) as dev1 on dev1.order_id=id.order_id and dev1.stock_out_id=id.stock_out_id
                     
WHERE     (ISNULL(id.pe_package_price, 0)+isnull(id.jumbo_package_amount,0) <> 0)  AND 
                      (id.order_id LIKE 'YP%') 
                      --and (o_1.customer_description like ? or ? is null) and (o_1.processing_jumbo_order_number like ? or ? is null)
                      --and ('' like ? or ? is null) and (id.invoice_id like ? or ? is null) and (o_1.customer_id like ? or ? is null)
GROUP BY id.float_line, pam.product_area_name, fm.floatLine_name, o_1.product_area,o_1.customer_description,o_1.processing_jumbo_order_number,id.invoice_id,rin.real_invoice_no,o_1.customer_id
UNION ALL
SELECT   dev2.material_thickness AS jumbo_plates_thickness,  o_2.invoice_area AS board_type, id.processing_machine_line as float_line, case when o_2.order_area='5' then '2' else '1' end AS resource, '' AS jumbo_plates_id, id.processing_product_desc AS jumbo_plates_type, SUM(id.quantity) AS sqm, 
                      SUM(ROUND(id.quantity * dev2.material_thickness / 408.76, 3)) AS weight,SUM(id.total_weight) as total_weight, SUM(id.total_price) AS price, SUM(id.discount) AS return_benifit, SUM(id.total_price) 
                      - SUM(id.discount) AS kfl_price, SUM(id.processing_price) AS fj_price, SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price) AS heji_price, case when datediff(day,'2019-03-31',?) <=0 then 0.16 else 0.13 end  as tax_rate, 
                      case when datediff(day,'2019-03-31',?) <=0 then ROUND(((SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price)) / (1 + 0.16))*0.16, 2) 
                           else ROUND(((SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price)) / (1 + 0.13))*0.13, 2) end AS se_price, 
                      case when datediff(day,'2019-03-31',?) <=0 then   ROUND(SUM(id.processing_price) / (1 + 0.16), 2) 
                          else  ROUND(SUM(id.processing_price) / (1 + 0.13), 2) end AS fjfws_price,
                       case when datediff(day,'2019-03-31',?) <=0 then SUM(id.total_price) - SUM(id.discount) - ROUND((SUM(id.total_price) - SUM(id.discount) / (1 + 0.16))*0.16, 2)
                          else SUM(id.total_price) - SUM(id.discount) - ROUND((SUM(id.total_price) - SUM(id.discount) / (1 + 0.13))*0.13, 2) end AS kflws_price, '2' AS type,'' as LL_flag, fm.floatLine_name, 
                      pp.parameter_name, o_2.customer_description,o_2.processing_order_number as order_number,id.invoice_id,rin.real_invoice_no,o_2.customer_id
FROM         dbo.invoice_order_item_detail AS id 

                inner join (SELECT     invoice_id,real_invoice_no
                            FROM          dbo.invoice_order AS invoice_order_2
                            WHERE      (undo_flag = '0') AND (status IN ('3', '4')) AND (input_date >= ?) AND (input_date <= ?)) as rin on rin.invoice_id=id.invoice_id


                    INNER JOIN
                          (SELECT     order_id, float_line,stock_out_id
                            FROM          dbo.invoice_order_item AS invoice_order_item_2
                            GROUP BY order_id, float_line,stock_out_id) AS ioi_2 ON ioi_2.order_id = id.order_id and ioi_2.stock_out_id=id.stock_out_id LEFT OUTER JOIN
                      dbo.floatLine_main AS fm ON id.processing_machine_line = fm.floatLine_id INNER JOIN
                          (SELECT     pjo.processing_order_number, pjo.product_desc, pjo.invoice_area, cm.customer_description,cm.customer_id,pjo.order_area
                            FROM          dbo.processing_orders as pjo
                            inner join customer_main as cm on cm.customer_id=pjo.customer_number) AS o_2 ON o_2.processing_order_number = id.order_id INNER JOIN
                          (SELECT     processing_order_number, SUM(material_thickness) AS material_thickness
                            FROM          dbo.processing_order_items AS i
                            WHERE      /*EXISTS
                                                       (SELECT     processing_order_number, MIN(processing_order_item_number) AS min_item_number
                                                         FROM          dbo.processing_product AS p
                                                         WHERE      (processing_order_number = i.processing_order_number)
                                                         GROUP BY processing_order_number
                                                         HAVING      (MIN(processing_order_item_number) = i.processing_order_item_number)) */
                                            processing_order_item_number = '1'
                                            AND (product_type=1) AND (SUBSTRING(material_number, 1, 
                                                   2) <> 'LL')
                            GROUP BY processing_order_number) AS dev2 ON dev2.processing_order_number = id.order_id LEFT OUTER JOIN
                          (SELECT     parameter_id, parameter_name
                            FROM          dbo.pmpi_parameter
                            WHERE      (parameter_type_id = '1019')) AS pp ON pp.parameter_id = o_2.invoice_area
                            
    WHERE     
                      (id.order_id LIKE 'RW%') 
                      --and (o_2.customer_description like ? or ? is null) and (o_2.processing_order_number like ? or ? is null)
                      --and (id.processing_product_desc like ? or ? is null) and (id.invoice_id like ? or ? is null) and (o_2.customer_id like ? or ? is null)
GROUP BY fm.floatLine_name, id.processing_machine_line,  o_2.invoice_area, id.processing_product_desc, pp.parameter_name, o_2.customer_description,o_2.processing_order_number,id.invoice_id,rin.real_invoice_no,o_2.customer_id,o_2.order_area,dev2.material_thickness
UNION ALL
SELECT     0 AS jumbo_plates_thickness, o_1_1.invoice_area AS board_type, id.processing_machine_line as float_line, case when o_1_1.order_area='5' then '2' else '1' end AS resource, '' AS jumbo_plates_id,id.processing_product_desc AS jumbo_plates_type, SUM(id.quantity) 
                      AS sqm, SUM(ROUND(id.quantity * dev2_1.material_thickness / 408.76, 3)) AS weight,SUM(id.total_weight) as total_weight, SUM(id.total_price) AS price, SUM(id.discount) AS return_benifit, 
                      SUM(id.total_price) - SUM(id.discount) AS kfl_price, SUM(id.processing_price) AS fj_price, SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price) 
                      AS heji_price, case when datediff(day,'2019-03-31',?) <=0 then 0.16 else 0.13 end  as tax_rate, 
                      case when datediff(day,'2019-03-31',?) <=0 then ROUND(((SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price)) / (1 + 0.16))*0.16, 2) 
                            else ROUND(((SUM(id.total_price) - SUM(id.discount) + SUM(id.processing_price)) / (1 + 0.13))*0.13, 2) end AS se_price, 
                      case when datediff(day,'2019-03-31',?) <=0 then ROUND(SUM(id.processing_price) / (1 + 0.16), 2) 
                            else ROUND(SUM(id.processing_price) / (1 + 0.13), 2) end AS fjfws_price, 
                      case when datediff(day,'2019-03-31',?) <=0 then  SUM(id.total_price) - SUM(id.discount) - ROUND((SUM(id.total_price) - SUM(id.discount) / (1 + 0.16))*0.16, 2) 
                            else SUM(id.total_price) - SUM(id.discount) - ROUND((SUM(id.total_price) - SUM(id.discount) / (1 + 0.13))*0.13, 2) end AS kflws_price, '2' AS type,'1' as LL_flag, 
                      fm.floatLine_name, pp_1.parameter_name, o_1_1.customer_description,o_1_1.processing_order_number as order_number,id.invoice_id,rin.real_invoice_no,o_1_1.customer_id
FROM         dbo.invoice_order_item_detail AS id 
                    inner join (SELECT     invoice_id, real_invoice_no
                            FROM          dbo.invoice_order AS invoice_order_1
                            WHERE      (undo_flag = '0') AND (status IN ('3', '4')) AND (input_date >= ?) AND (input_date <= ?)) as rin on rin.invoice_id=id.invoice_id


                        INNER JOIN
                          (SELECT     order_id, float_line,stock_out_id
                            FROM          dbo.invoice_order_item AS invoice_order_item_1
                            GROUP BY order_id, float_line,stock_out_id) AS ioi_1 ON ioi_1.order_id = id.order_id and ioi_1.stock_out_id=id.stock_out_id LEFT OUTER JOIN
                      dbo.floatLine_main AS fm ON id.processing_machine_line = fm.floatLine_id INNER JOIN
                          (SELECT     pjo.processing_order_number, pjo.product_desc, pjo.invoice_area, cm.customer_description,cm.customer_id,pjo.order_area
                            FROM          dbo.processing_orders as pjo
                            inner join customer_main as cm on cm.customer_id=pjo.customer_number) AS o_1_1 ON o_1_1.processing_order_number = id.order_id 
                          
                          
                          INNER JOIN
                           --纯来料的厚度
                          (SELECT     dev0_1.processing_order_number, dev0_1.material_thickness
                            FROM          (SELECT     processing_order_number, /*COUNT(group_id) AS c,*/ SUM(material_thickness) AS material_thickness
                                                    FROM          dbo.processing_order_items AS processing_order_items_2
                                                    WHERE      (product_type = '1') AND (processing_order_item_number = '1') AND (SUBSTRING(material_number, 1, 2) = 'LL')
                                                    GROUP BY processing_order_number) AS dev0_1
                                                     
                                             inner join (select processing_order_number,jumbo_plates_thickness from processing_product where processing_order_item_number = '1') as dev1_2_1
                                            
                                            on (dev0_1.material_thickness = dev1_2_1.jumbo_plates_thickness) and (dev0_1.processing_order_number = dev1_2_1.processing_order_number)
                                                    
                                                   /*LEFT OUTER JOIN
                                                 (SELECT     processing_order_number, COUNT(group_id) AS c
                                                   FROM          dbo.processing_order_items AS processing_order_items_1
                                                   WHERE      (product_type = '1') AND (processing_order_item_number = '1') AND (SUBSTRING(material_number, 1, 2) = 'LL')
                                                   GROUP BY processing_order_number) AS dev1_2_1 ON dev0_1.processing_order_number = dev1_2_1.processing_order_number*/
                                                   
                            ) AS dev2_1 ON dev2_1.processing_order_number = id.order_id 
                            
                            
                            LEFT OUTER JOIN
                          (SELECT     parameter_id, parameter_name
                            FROM          dbo.pmpi_parameter AS pmpi_parameter_1
                            WHERE      (parameter_type_id = '1019')) AS pp_1 ON pp_1.parameter_id = o_1_1.invoice_area
                            
WHERE     
                      (id.order_id LIKE 'RW%') 
                      --and (o_1_1.customer_description like ? or ? is null) and (o_1_1.processing_order_number like ? or ? is null)
                      --and (id.processing_product_desc like ? or ? is null) and (id.invoice_id like ? or ? is null) and (o_1_1.customer_id like ? or ? is null)
GROUP BY fm.floatLine_name, id.processing_machine_line,  o_1_1.invoice_area, id.processing_product_desc, pp_1.parameter_name, o_1_1.customer_description,o_1_1.processing_order_number,id.invoice_id,rin.real_invoice_no,o_1_1.customer_id,o_1_1.order_area
) as dev
where (customer_description like ? or ? is null) and (order_number like ? or ? is null) and (jumbo_plates_type like ? or ? is null)
        and (invoice_id like ? or ? is null) and (customer_id like ? or ? is null)

结束。

标签:总结,记录,Union,SUM,processing,id,jumbo,total,price
来源: https://www.cnblogs.com/kaspar/p/13024354.html