두 테이블을 결합하고 있습니다. 그런 다음 두 개의 다른 날짜 기간에 대해 하나의 테이블에서 두 개의 열을 합산합니다.
그러나 내가 원하는 것은 테이블 중 하나의 다른 필드에서 WHERE 조건으로 정의 된 두 개의 필드 세트로 날짜 범주를 더 분리하는 것입니다.
그러나이 결과를 얻을 수 없습니다. ORA-00936 :식이 없습니다.
이것은 구문 오류 일 가능성이 높지만 시도하는 동안 여러 날짜와 조건으로 예제를 찾을 수 없었기 때문에 이것을 작성하는 방법에 대한 예제를 찾을 수 없습니다.
내 코드 :
select SOME_CATEGORY_1, SOME_CATEGORY_2,
WHERE (SOME_CATEGORY_3 = 'Value A',
sum(case when oc.week_start_date between SYSDATE-(52*7) and SYSDATE then ms.cash_sales else 0 end) A_TY_CashSales,
sum(case when oc.week_start_date between SYSDATE-(52*7) and SYSDATE then ms.unit_sales else 0 end) A_TY_UnitSales,
sum(case when oc.week_start_date between SYSDATE-(104*7) and SYSDATE-(53*7) then ms.cash_sales else 0 end) A_LY_CashSales,
sum(case when oc.week_start_date between SYSDATE-(104*7) and SYSDATE-(53*7) then ms.unit_sales else 0 end) A_LY_UnitSales
),
WHERE (SOME_CATEGORY_3 = 'Value B',
sum(case when oc.week_start_date between SYSDATE-(52*7) and SYSDATE then ms.cash_sales else 0 end) B_TY_CashSales,
sum(case when oc.week_start_date between SYSDATE-(52*7) and SYSDATE then ms.unit_sales else 0 end) B_TY_UnitSales,
sum(case when oc.week_start_date between SYSDATE-(104*7) and SYSDATE-(53*7) then ms.cash_sales else 0 end) B_LY_CashSales,
sum(case when oc.week_start_date between SYSDATE-(104*7) and SYSDATE-(53*7) then ms.unit_sales else 0 end) B_LY_UnitSales
)
from MY_TABLE mt
join MY_OTHER_TABLE mot on mot.mykey = mt.mykey
join calendar_table ct on ct.week_id = ms.week_id
group by SOME_CATEGORY_1, SOME_CATEGORY_2
order by 1,2,3 desc
결과는 두 개의 개별 선택을 사용하여 수행 할 수 있지만 가능하면 단일 쿼리에서 두 사례에 대한 올해와 지난 연도 합계를 보여주는 단일 쿼리를 갖고 싶습니다.
어떻게하면 되나요?
참고로이 쿼리는 Oracle 데이터베이스에 적용됩니다.
예를 들어 case
제한 할 문에 다른 절을 추가합니다 .some_category_3
select some_category_1
, some_category_2
, sum(case
when some_category_3 = 'value a'
and oc.week_start_date between sysdate-(52*7) and sysdate
then ms.cash_sales
else 0
end) a_ty_cashsales
, sum(case
when some_category_3 = 'value a'
and oc.week_start_date between sysdate-(52*7) and sysdate
then ms.unit_sales
else 0
end) a_ty_unitsales
, sum(case
when some_category_3 = 'value a'
and oc.week_start_date between sysdate-(104*7) and sysdate-(53*7)
then ms.cash_sales
else 0
end) a_ly_cashsales
, sum(case
when some_category_3 = 'value a'
and oc.week_start_date between sysdate-(104*7) and sysdate-(53*7)
then ms.unit_sales
else 0
end) a_ly_unitsales
, sum(case
when some_category_3 = 'value b'
and oc.week_start_date between sysdate-(52*7) and sysdate
then ms.cash_sales
else 0
end) b_ty_cashsales
, sum(case
when some_category_3 = 'value b'
and oc.week_start_date between sysdate-(52*7) and sysdate
then ms.unit_sales
else 0
end) b_ty_unitsales
, sum(case
when some_category_3 = 'value b'
and oc.week_start_date between sysdate-(104*7) and sysdate-(53*7)
then ms.cash_sales
else 0
end) b_ly_cashsales
, sum(case
when some_category_3 = 'value b'
and oc.week_start_date between sysdate-(104*7) and sysdate-(53*7)
then ms.unit_sales
else 0
end) b_ly_unitsales
from my_table mt
join my_other_table mot
on mot.mykey = mt.mykey
join calendar_table ct
on ct.week_id = ms.week_id
group by some_category_1
, some_category_2
order by some_category_1
, some_category_2
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다