简体   繁体   中英

SQL: Workaround for the maximum expression limit

I am trying to perform some basic customer segmentation/profile analysis on a set of 4000 accounts.

I'd like to do a quick customer segmentation based on usage and tenure. Tactically, this means querying an Oracle SQL database for Acocunt_IDs that meet certain criteria.

For example, the usage query will return the subset of accounts that have more than > 100 hours of usage over the last month.

Once I have this list of accounts, I'd like ask a different set of profiling questions.

For example, for the customers that had more than 100 hours of usage: what products did they use? how long have they been a customer? what was the referral source?

My basic approach has been:

  1. Run the customer segment queries
  2. download the accountIds for each segment into Excel
  3. Formulate the profile queries
  4. Run the Profile queries for each of the customer segments, using the structure below:

     Select * From fooo where Account_ID in ('00001','00002','00003') 

The challenge is that the customer segment queries return more than 1000 results, so i have to manually substitute in different batches of account_IDs in sets of 1000. This is required because of Oracle SQL's 1,000 expression limit.

The alternative would be to combine the customer segment queries in the profile queries, but this makes the queries take a ages to execute.

SO. The question is there an efficient way to create and populate a temporary table in Oracle SQL that I can use to dump the customer segment query results and then throw those results into profile queries to ensure that they run more efficicently?

Would using the ROW_NUMBER() window aggregate function to assign a RowID based on the ASCENDING order of the AccountID then use a WHERE clause to process n number of rows be acceptable:

SELECT DT1.*
FROM (SELECT f1.*
           , ROW_NUMBER() OVER (ORDER BY F1.AccountID) AS RowID_
      FROM foo f1
     ) DT1
WHERE DT1.RowID_ BETWEEN 1 and /* n */
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM