i have a query that will create a calculated priority (c_priority) by store based on a primary priority column, the query works well to calculate c_priority using row_number() over (partition by store), however i need to go one step further and also sort this results by groups of 25 based on the campaign_column and priority and this is where i need help. I need the query to pull the first 25 records of the campaign with the highest priority, then pull the next 25 records of the following campaign and once it has finished with all campaigns gets back to the first campaign and pull another 25 records and cycle again until all records have a single sequence order (Calculated Priority) by store.
i will appreciate if someone can give an idea on how to achieve this, thanks!
current Query:
SELECT CONTACT_ID
,LEAD_NUMBER
,LEAD_CREATION_DATE
,CUSTOMER_FIRST_NAME
,CUSTOMER_LAST_NAME
,CAMPAIGN_NAME
,STORE
,MOBILE_NUMBER
,HOME_NUMBER
,WORK_NUMBER
,CELL_DO_NOT_CONTACT_FLAG
,HOME_DO_NOT_CONTACT_FLAG
,WORK_DO_NOT_CONTACT_FLAG
,PRIORITY
,ROW_NUMBER() OVER (PARTITION by store ORDER BY priority asc, campaign_name, store) AS C_PRIORITY
,HISTORICAL_DATE
FROM stores_campaigns_all
WHERE historical_date = '2023-03-10'
AND store IS NOT NULL
This is the current data output with my query: Current data output by my query
and this is how i want it to be, by rows of 2 to make it simple for this demostration how i need the data to be