سؤال

هذا الاستعلام يعطي خطأ:

select ep, 
  case
    when ob is null and b2b_ob is null then 'a'
    when ob is not null or b2b_ob is not null then 'b'
    else null
  end as type,
  sum(b2b_d + b2b_t - b2b_i) as sales
from table
where ...
group by ep, type

خطأ: ORA-00904: "النوع": معرف غير صالح

عندما أقوم بتشغيله مع group by ep, ، تصبح رسالة الخطأ:

ORA-00979: ليست مجموعة عن طريق التعبير

يعمل الاستعلام بأكمله جيدًا إذا قمت بإزالة الخطوط sum(b2b_d+b2b_t-b2b_i) as sales و group by ..., ، وبالتالي يجب أن تكون المشكلة مرتبطة بالمبلغ والمجموعة حسب الوظائف. كيف يمكنني جعل هذا العمل؟ شكرا مقدما لمساعدتكم.

هل كانت مفيدة؟

المحلول

لسوء الحظ ، لا تسمح لك SQL باستخدام الاسم المستعار للعمود في المجموعة حسب الجملة ، لذلك عليك إما تكرار الحالة بأكملها مثل هذا:

select ep, 
  case
    when ob is null and b2b_ob is null then 'a'
    when ob is not null or b2b_ob is not null then 'b'
    else null
  end as type,
  sum(b2b_d + b2b_t - b2b_i) as sales
from table
where ...
group by ep,
  case
    when ob is null and b2b_ob is null then 'a'
    when ob is not null or b2b_ob is not null then 'b'
    else null
  end

أو استخدم طريقة عرض في الخط مثل هذا:

select ep, 
  type,
  sum(b2b_d + b2b_t - b2b_i) as sales
from
( select ep, 
    case
      when ob is null and b2b_ob is null then 'a'
      when ob is not null or b2b_ob is not null then 'b'
      else null
    end as type,
    b2b_d,
    b2b_t,
    b2b_i
  from table
  where ...
)
group by ep, type
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top