我正在尝试对提取值进行分组。我通过用Cases提取季度开始。我正在尝试将此Sales_Quarter添加到我的团购中,当我在Toad上在Oracle服务器上运行时,出现错误“ Sales_Quarter”无效标识符。
with c_prod as (select prod_id_pri, prod_desc,prod_num from rm_mrb.V_PRODUCT_PROD_LIST)
select 'Excluded' as Status, tr.product_id, prod.prod_desc,
sum(tr.QUANTITY) QUANTITY, sum(total_Prescriptions) Total_#_OF_PRESCRIPTIONS, count(*) Claims
,CASE
When
EXTRACT(MONTH FROM Tr.Fill_Date) IN (
'1',
'2',
'3' )
THEN
'01-01-'||EXTRACT(Year FROM Tr.Fill_Date)
When
EXTRACT(MONTH FROM Tr.Fill_Date) IN (
'4',
'5',
'6' )
THEN
'04-01-'||EXTRACT(Year FROM Tr.Fill_Date)
When
EXTRACT(MONTH FROM Tr.Fill_Date) IN (
'7',
'8',
'9' )
THEN
'07-01-' ||EXTRACT(Year FROM Tr.Fill_Date)
ELSE '10-01-'||EXTRACT(Year FROM Tr.Fill_Date)
End As Sales_Quarter
from VALIUSER.ivd_transaction_record_details RD, valiuser.IVD_TRANSACTION_RECORDS TR
left join c_prod prod on (tr.product_id=prod.prod_id_pri)
where TR.transaction_record_id=RD.transaction_record_id
and status='1'
and tr.File_id in (1765)
group by Status,Product_id,prod.prod_desc,Sales_Quarter
答案 0 :(得分:2)
您可以按年份和季度分组
group by Status,Product_id,prod.prod_desc,EXTRACT(Year FROM Tr.Fill_Date),EXTRACT(Quarter FROM Tr.Fill_Date)
您还可以使用以下方法消除案例陈述:
to_string(
((EXTRACT(Quarter FROM Tr.Fill_Date)-1)*3)+1)
|| '-01-'
|| EXTRACT(Year FROM Tr.Fill_Date)
答案 1 :(得分:1)
您无法像在GROUP BY
中那样ORDER BY
使用列别名。
或者在group by子句中为Sales_Quarter
重复整个表达式,或者在WITH
子句中计算它,然后选择它并对其进行分组。我将在下面演示此稍后的方法:
with c_prod as (select prod_id_pri, prod_desc,prod_num from rm_mrb.V_PRODUCT_PROD_LIST),
extracted as (
select 'Excluded' as Status, tr.product_id, prod.prod_desc,
tr.QUANTITY, total_prescriptions
,CASE
When
EXTRACT(MONTH FROM Tr.Fill_Date) IN (
'1',
'2',
'3' )
THEN
'01-01-'||EXTRACT(Year FROM Tr.Fill_Date)
When
EXTRACT(MONTH FROM Tr.Fill_Date) IN (
'4',
'5',
'6' )
THEN
'04-01-'||EXTRACT(Year FROM Tr.Fill_Date)
When
EXTRACT(MONTH FROM Tr.Fill_Date) IN (
'7',
'8',
'9' )
THEN
'07-01-' ||EXTRACT(Year FROM Tr.Fill_Date)
ELSE '10-01-'||EXTRACT(Year FROM Tr.Fill_Date)
End As Sales_Quarter
from VALIUSER.ivd_transaction_record_details RD, valiuser.IVD_TRANSACTION_RECORDS TR
left join c_prod prod on (tr.product_id=prod.prod_id_pri)
where TR.transaction_record_id=RD.transaction_record_id
and status='1'
and tr.File_id in (1765)
)
SELECT 'Excluded' as Status, product_id, prod_desc, sales_quarter,
sum(QUANTITY) QUANTITY, sum(total_Prescriptions) Total_#_OF_PRESCRIPTIONS, count(*) Claims
from extracted
group by Status,Product_id,prod.prod_desc,Sales_Quarter