我使用Oracle SQL Developer并希望定义多个日期并在select和where子句中多次使用它们。他们会在我全年都这样做的时候改变。它们是这里显示的四个季度的日期,但它并不总是季度,我将有大约10个日期。
我希望查询提供4个类别的计数和转换到下一个阶段。阶段:观看,吸引,订购,实现。它们是从4个子查询中获得的日期。
--this is the part I don't know how to do
Define date1 = '2017-01-01'
Define date2 = '2017-03-31'
Define date3 = '2017-04-01'
Define date4 = '2017-06-30'
--I want to set dates here and call them out in the query below.
select count(case when f_view between date1 and date2 then 1 else null end) c_view
,count(case when f_attract between date1 and date2 then 1 else null end) c_attract
,count(case when f_order between date1 and date2 then 1 else null end) c_order
,count(case when f_fulfill between date1 and date2 then 1 else null end) c_fulfill
--the prev 4 statements are my method of getting the count
--the next 3 are for the conversion rates (case going from view to attract, etc)
,count(case when f_view between date1 and date2 then 1 else null end)
/ count(case when f_attract is not null then 1 else null end) conv_view
,count(case when f_attract between date1 and date2 then 1 else null end)
/ count(case when f_order is not null then 1 else null end) conv_attract
,count(case when f_order between date1 and date2 then 1 else null end)
/ count(case when f_fulfill is not null then 1 else null end) conv_order
--I would continue this for 4 other date ranges
from (select f_case
,f_view
from view_table
where seq = 1) view
--every case has a view, but not every case has the other 3 dates
left join (select f_case
,max(f_attract) over (partition by f_case) f_attract
from order_table) attract
on view.f_case = attract.f_case
left join (select f_case
,max(f_order) over (partition by f_case) f_order
from order_table
where order_type = 'order') order
on view.f_case = order.f_case
left join (select f_case
,max(f_fulfill) over (partition by f_case) f_fulfill
from fulfill_table) fulfill
on view.f_case = fulfill.f_case
where f_view between date1 and date4
or f_attract between date1 and date4
or f_order between date1 and date4
or f_fulfill between date1 and date4
我可以将日期放入查询中,但每次运行时我都必须更改它们(并且那里有很多!)。
答案 0 :(得分:1)
这似乎是相当基本的PL / SQL,但它看起来像这样:
declare
v_date1 date;
v_date2 date;
v_date3 date;
v_date4 date;
v_field_1 number;
v_field_2 number;
begin
v_date1 := '2017-01-01';
v_date2 := '2017-03-31';
v_date3 := '2017-04-01';
v_date4 := '2017-06-30';
select sum(case when field1 between v_date1 and v_date2 then 1 else 0 end) as c_field1_1
sum(case when field2 between v_date1 and v_date2 then 1 else 0 end) c_field2_1
into v_field1, v_field2
from ?
where field1 between v_date1 and v_date4 or
field2 between v_date1 and v_date4;
dbms_output.print_line(v_field1 || ' ' || v_field2);
end;
请注意,您的查询未指定FROM
子句。
答案 1 :(得分:0)
你不能像这样“定义”(并赋值给)变量。如果您希望它们在您的示例中进行硬编码,则可以在子查询中“定义”它们(如果您知道那是什么,则可以使用WITH子句)并使用连接:
select <whatever>
from <your_table> t -- you are missing the FROM clause in your sample code!
join
( select date '2017-01-01' as date1,
date '2017-03-31' as date2,
date '2017-04-01' as date3,
date '2017-06-30' as date4
from dual
) d
on t.field1 between d.date1 and d.date2 -- why "date4" in your sample code??
or t.field2 between d.date3 and d.date4
;
事实上,如果要求仅仅是按日历季度划分的数据“做某事”,那么有更简单的方法 - 您不需要任何类型的变量。如果你告诉我们你试图解决的原始问题,而不是你攻击它的方式(这似乎不太有效,除了也不是很正确)之外,这将是最好的。