Skip to content
Advertisement

My query works but I need to make is scalable. I need to remove unions but not sure how to proceed

I need a list of the number of times a file has processed through our system. For example in week 1, for a given warehouseID, x number of files have processed 1 time, x number have processed 2 times, and so on. I have written a query that uses a temp table and multiple unions but its not scalable. For instance if a file is processed 42 times the query in its current form would be absurdly long. I thinkI need some self joins maybe? Or sub queries? But I’m having a really hard time sorting my next step out.

Currently I’m stacking it all in union statements. The answer is right and accurate. But my goal is to make it scalable and not rely on hard coding. I’ve tried rewriting using having count(*)>1 but that just goes back to hardcoding and using unions. There’s got to be a more dynamic way.

Looking for a count of how many files processed 1 time, 2 times, 3 times, and so on.

Advertisement

Answer

You seem to be looking for a simple aggregated query. The exact syntax might slightly change depending on your RDBMS (namely, whether it supports aliases in the GROUP BY clause or not), but it should look like:

If you want to limit the query to a specific list of TimesProcessed values, you can add a WHERE clause:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement