DRama
DRama

Reputation: 1

How to perform iteration in SQL

I have a query that I need to perform 1000 times. Each time i need to increment two filter values within that query.

select min(a), min(bo
from(select*
from(select unmatchedpow.id, unmatchedpow.ut_sec-difference AS A, unmatchedpow.ut_sec-difference AS B
from(select*
from(select unmatchedpow.ut_sec-unmatchedsaaf.ut_sec AS difference,*
from(select*from unmatchedpow, unmatchedsaaf 
order by unmatchedpow.id))))
where unmatchedpow.id between 0 and 1)

So with each iteration the last between statement parameters increment by 1, ie between 1 and 2, then between 2 and 3... up until 999 and 1000.

is there any way i can do this and store the results as well?

Upvotes: 0

Views: 123

Answers (1)

Barmar
Barmar

Reputation: 781096

Create a table that contains the numbers from 0 to 999, and join with that to get the values for the BETWEEN test.

SELECT n.num, min(x.a), min(x.b)
from number_table AS n
LEFT JOIN (select*
    from(select unmatchedpow.id, unmatchedpow.ut_sec-difference AS A, unmatchedpow.ut_sec-difference AS B
        from(select*
            from(select unmatchedpow.ut_sec-unmatchedsaaf.ut_sec AS difference,*
                from(select*from unmatchedpow, unmatchedsaaf)))) AS x
ON x.id BETWEEN n.num AND n.num+1

Upvotes: 1

Related Questions