自动重新运行具有不同参数的SQL查询

时间:2016-04-19 12:11:35

标签: sql oracle-sqldeveloper

我有以下查询,这使我能够确定个人在七天内完成两次至少1,000美元交易的次数:

select count(*)
from (

select id, date, visit_count, daily_total_amount,
sum(visit_count) over (partition by id order by date range between interval '6' day preceding and current row) as rolling_visit_sum
from (

select id, date, count(*) as visit_count, sum(total_amount) as daily_total_amount
from (

select id, date, time, store, sum(currency_amount) as total_amount
from table
group by id, date, time, store
having sum(currency_amount) >= 1000

)
group by id, date
order by id, date

)
group by id, date, visit_count, daily_total_amount

)
where rolling_visit_sum = 2;

我想重新运行查询,看看结果如何随着我更改参数而改变(使用[$ 1000,$ 2000,$ 3000,$ 4000,$ 5000,$ 6000,$ 7000,$ 8000,$ 9000]作为最小值 sum(currency_amount)阈值和[1,2,3,4,5,6]作为必需 rolling_visit_sum )。我想这可以通过某种循环自动化,但是当我试图自己搜索如何做到这一点时,我是新手并且迷惑自己。

理想情况下,我最终得到的输出表类似于以下内容( x 填充了查询结果:

sum(currency_amount) |  rolling_visit_sum  | count(*)
    1000                    1                    x
    1000                    2                    x
    1000                    3                    x
    1000                    4                    x
    1000                    5                    x
    1000                    6                    x
    2000                    1                    x
    2000                    2                    x
    2000                    3                    x
    2000                    4                    x  

.......

sum(currency_amount) |  rolling_visit_sum  | count(*)
    9000                    3                    x
    9000                    4                    x
    9000                    5                    x
    9000                    6                    x

只要能够区分每个参数组合的结果,结果的实际格式就不重要了。任何指导都将非常感谢!

1 个答案:

答案 0 :(得分:0)

这里有一个如何在T-SQL中执行此操作的示例,也许它可以帮助您(甚至我假设您使用oracle但oracle确实具有与游标类似的逻辑)。你也可以使用普通的oracle循环语句(google中有很多例子)。

-- declare variables

declare @ParamterCurrencyAmount numeric(26, 2),
        @ParameterRollingVisitSum int


-- declare table variable

declare @CurrencyAmounts table
(
    CurrencyAmount numeric(26, 2)
)

-- declare table variable

declare @RollingVisitSums table
(
    RollingVisitSum int
)


-- insert sample data 

insert into @CurrencyAmounts
(
    CurrencyAmount
)
select 1000 union all
select 2000 union all
select 3000 union all
select 4000 union all
select 5000 union all
select 6000 union all
select 8000 union all
select 9000

insert into @RollingVisitSums
(
    RollingVisitSum
)
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6



-- join the data together for a cursor and loop through the cursor

declare ParameterCursor cursor local static read_only forward_only for 
select CurrencyAmounts.CurrencyAmount,
RollingVisitSums.RollingVisitSum
from @CurrencyAmounts CurrencyAmounts,
@RollingVisitSums RollingVisitSums

open ParameterCursor 
fetch next from ParameterCursor  

into @ParamterCurrencyAmount, @ParameterRollingVisitSum

while @@FETCH_STATUS = 0
begin


    print @ParamterCurrencyAmount
    print @ParameterRollingVisitSum


    -- do your code here whatever you wanna do with your parameters



    fetch next from ParameterCursor 
    into @ParamterCurrencyAmount, @ParameterRollingVisitSum

end 


close ParameterCursor;
deallocate ParameterCursor;