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

发布于 2024-11-30 13:45:37 字数 1190 浏览 1 评论 0原文

我需要一些帮助来调整以下查询

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 语句中所看到的,我在计数函数中使用了 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

I'd like some help tweaking the following query

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

As you can see in the select statement, I'm using case statements within my count functions. This works fine. Data output looks like this

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

I'd like to fill in the same input value in the input column throughout all the rows that have the same date and MIP_Desc.

Output should look like this

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼趣 2024-12-07 13:45:37

这有帮助吗? (未经测试):

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;

Does this help? (untested):

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文