2016-04-19 3 views
0

Ich habe die folgende Abfrage, die mir die Anzahl der Male eine Person führt eine Transaktion von mindestens $ 1.000 genau zweimal in einem Zeitraum von sieben Tagen zu identifizieren erlaubt:automatisch erneut ausführt eine SQL-Abfrage mit verschiedenen Parametern

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; 

Ich möchte die Abfrage erneut ausführen, um zu sehen, wie sich die Ergebnisse ändern, wenn ich die Parameter ändere (mit [$ 1000, $ 2000, $ 3000, $ 4000, $ 5000, $ 6000, $ 7000, $ 8000, $ 9000] als Minimum Summe (currency_amount) Schwelle und [1, 2, 3, 4, 5, 6] als Voraussetzung rolling_visit_sum). Ich kann mir vorstellen, dass dies mit einer Art von Schleife automatisiert werden kann, aber ich bin neu darin und habe mich verwirrt, als ich versuchte, selbst zu suchen, wie ich das machen könnte.

Idealerweise würde ich mit einer Ausgabetabelle so etwas wie das folgende am Ende (mit dem x ‚s mit den Abfrageergebnissen bevölkert:

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 

Das tatsächliche Format der Ergebnisse spielt keine große Rolle, solange ich in der Lage bin, die Ergebnisse für jede Kombination von Parametern zu unterscheiden.Eine Anleitung wäre sehr willkommen!

Antwort

0

Hier ein Beispiel, wie Sie es in T-SQL machen könnten, vielleicht hilft es Ihnen (sogar ich nehme an, Sie verwenden Oracle, aber Oracle hat eine ähnliche Logik wie Cursor). Sie können auch eine normale Orakel-Loop-Anweisung verwenden (viele Beispiele in Google verfügbar).

-- 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; 
Verwandte Themen