Oracle SQL - 在组中重复相同的列值

时间:2011-08-21 18:27:20

标签: sql oracle group-by

我想帮助调整以下查询

select 
    data.smalldate, 
    mip.mip_step_description, 
    error_code.error_code_en, 
    count(case when (error_code is null and quality_plan is null) then data.part_serial_number end) as "Input", 
    count(case when error_code is not null then data.part_serial_number end) as "Defects"
from Data

left join MIP
On data.equipment = mip.equipment

left join error_code
on data.error_code = error_code.error_code_sn 

group by data.smalldate, mip.mip_step_description, error_code.error_code_en

order by data.smalldate, mip.mip_step_description, count(data.part_serial_number) desc

正如您在select语句中看到的那样,我在count函数中使用了case语句。这很好用。数据输出如下所示

Date    MIP_Desc    Error_Code    Input    Defects
1/1/2011    MIP Z    (null)       100      0
1/1/2011    MIP Z    A            0        10
1/1/2011    MIP Z    B            0        15

我想在具有相同日期和MIP_Desc的所有行中的输入列中填入相同的输入值。

输出应该如下所示

Date    MIP_Desc    Error_Code    Input    Defects
1/1/2011    MIP Z    (null)       100      0
1/1/2011    MIP Z    A            100      10
1/1/2011    MIP Z    B            100      15

1 个答案:

答案 0 :(得分:3)

这有帮助吗? (另):

SELECT smalldate, mip_step_description
     , error_code_en
     , MAX("Input") OVER (PARTITION BY smalldate, mip_step_description) "Input"
     , "Defects"
  FROM (select data.smalldate, mip.mip_step_description, error_code.error_code_en
             , count(case when (error_code is null and quality_plan is null)
                          then data.part_serial_number end) as "Input"
             , count(case when error_code is not null 
                          then data.part_serial_number end) as "Defects"
             , count(data.part_serial_number) sn_ct
          from DATA left join MIP On data.equipment = mip.equipment
                    left join ERROR_CODE on data.error_code = error_code.error_code_sn 
         group by data.smalldate, mip.mip_step_description, error_code.error_code_en)
order by smalldate, mip_step_description, sn_ct desc;