2016-04-19 6 views
0

私持っている私は、7日の期間にちょうど2倍個人が少なくとも$ 1,000トランザクションを行った回数を識別することができます次のクエリ、:は自動的に再実行している別のパラメータを持つSQLクエリを

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(*) 
    9000     3     x 
    9000     4     x 
    9000     5     x 
    9000     6     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 

パラメータの組み合わせごとに結果を区別できるのであれば、結果の実際の形式はまったく問題ではありません。

答えて

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; 
関連する問題