Basically I don't know where to start here as far as coding this query and my searching on it has been largely unfruitful. To begin with the database we're querying is Oracle 11. I work in a payroll company and trying to build out a query that will have a variable number of columns returned. So for background our clients can define labor fields, these designate types of work a company may keep track of such as what department, job or project is worked. There are 12 hard coded fields, one of which must always be used but the rest is up to what any individual client needs to track. For any defined field there's a flag for if it is used in a Rate Program which can modify the pay rate of an employee based on the labor field selected for hours.
So the issue I'm running into is when trying to query all the Rate Programs for a client. Not only are the labor fields present different for each client, but which ones they use in the rate program can vary.
Here's a bit of an example of how the database tables look for these two fields
Labor Field
LABORID LABEL USEINRATEPROGRAM
------- ---------- ----------------
1 FALSE
2 DEPARTMENT TRUE
3 JOB FALSE
Rate Program
RATEPROGRAMID RATEPROGRAMDESC LCF1 LCF2 LCF3
------------- ----------------- ----- ------------ ----
MACHINE SHOP MACHINE SHOP RATE ~ANY~ MACHINE SHOP ~ANY
To expand slightly the Labor Rate table has spots for 12 fields, any of them not in use has a blank Label field and which ones they use is completely arbitrary, from the above example JOB could simply be defined under LABORID field 8, it just depends on where we enter it. I can do a simple query to pull a list of all validly configured Labor Fields for a given client and I can do one for Labor Fields configured to to be used by the Rate Program. What I can't seem to do is make a SELECT query for the Rate Program that will only include any of the LCF# columns that have USEINRATEPROGRAM set to TRUE. From what I can tell I'll have to use Dynamic SQL in order to do this since the number of columns returned will be different depending on which client database I run the query against but it's not something that I've ever had to use before.
If anyone could provide any pointers on how to proceed it would be amazing. I have written some, for me, dumbly complicated queries, but they were all fixed column results so it was just a matter of iterating on a beginning query until it all flowed together but this is another thing altogether that's just slightly outside of my reach.
EDIT: Adding some more details as it was asked for.
My interface with the database is an html portal that restricts me to SELECT queries only so there are limits to my interactions with the database but as far as I've been told dynamic queries should still function correctly. The output through this interface is an html table that can exported as a csv file.
The portal requires me to enter the database info of any individual client before a query can be passed so my hope is just a general script that I can insert into any client's database to get the relevant fields. Currently what happens when I query the Rate Program information is I will receive the actions of the rate program (I didn't include them here because they're not important towards the question) and the combination of labor fields that allows that action to happen. Every Labor Field is included in the Rate Program table, our overlying system will just display/apply those that are set as TRUE, if a field does not have a specific entry for a labor field it is populated with "~ANY~" which is true of both the fields that are used and not used by the Rate Program.
Using my tables from above what I would want to see in my query results would be:
RATEPROGRAMID RATEPROGRAMDESC LCF2
------------- ----------------- ------------
MACHINE SHOP MACHINE SHOP RATE MACHINE SHOP
I would want to omit LCF1 because it's not configured at all and LCF3 because the USEINRATEPROGRAM is set to FALSE. If a different client had 10 different labor fields configured and 6 were set to TRUE for USEINRATEPROGRAM I would want the query to dynamically export just those 6 fields.