This is continuing questions from http://stackoverflow.com/questions/3539673/sqlite-selecting-the-highest-salary
Assuming a table 'wagetable'
name lowhours highhours wage priority
Default 0.0 40.0 100 0
Default 40.0 50.0 150 0
Default 50.0 70.5 154 0
Default 70.5 100.0 200 0
Brian 0.0 40.0 200 1
Brian 40.0 50.0 250 1
Brian 50.0 60.0 275 1
Brian 60.0 70.0 300 1
Brian 70.0 80.0 325 1
Brian 80.0 9999.0 350 1
Chad 0.0 40.0 130 1
Chad 40.0 9999.0 170 1
I currently performing two types of queries 3-4 times a second, so performance is key, not really readability (but preferred).
This query choose the wage if and only if the $Hour is between lowhours and highhours:
SELECT wage
FROM wagetable
WHERE name LIKE '$Employee' OR name LIKE 'Default'
AND '$Hour' BETWEEN lowhours AND highhours
ORDER BY priority DESC
LIMIT 1
This 2nd query takes off if the first failed to find $Hour between lowhours and highhours:
SELECT wage
FROM wagetable
WHERE name LIKE '$Employee' OR name LIKE 'Default'
ORDER BY priority DESC, highhours DESC
LIMIT 1
I am looking to see if there could be a query where I could combine both to do the same thing in just one query
Edit:
Because I didn't correctly test the above queries.. I will tell you what I want in english and examples of answers.
It will check rather $Employee exists on the table, then use it. If it doesnt, then check for 'Default' instead. Once it knows the name, it will check to see if $Hour is between a known lowhours and highhours, if it does, SELECT that wage, if its higher than anything listed, automatically take the wage for the highest hour.
Here are some values. Please note that "sam" is not on the table, so he will be 'Default' The examples follow this format: (Name, Hour) EXPECTED ANSWER
(Sam, 1) 100
(Sam, 51) 154
(Sam, 999999) 200
(Brian, 1) 200
(Brian, 51) 275
(Brian, 999999) 350
Here is the table again so you can quick reference, remember Sam will be 'Default'
name lowhours highhours wage priority
Default 0.0 40.0 100 0
Default 40.0 50.0 150 0
Default 50.0 70.5 154 0
Default 70.5 100.0 200 0
Brian 0.0 40.0 200 1
Brian 40.0 50.0 250 1
Brian 50.0 60.0 275 1
Brian 60.0 70.0 300 1
Brian 70.0 80.0 325 1
Brian 80.0 9999.0 350 1
Edit 2:
The point of this is so that you can define a table of wages where if someone gets more money than the rest, they get paid a certain amount plus overtime. If it is a standard worker, then will be get Default
wages. If the $Employee
is on the list, then he gets special wages.
The lowest lowhours
will -always- be 0.0, The lowhours
and highhours
pair will -never- have any gaps (It won't allow 40-50 and then skip and do 60-70). What is uncertain is the highest highhours
. Therefore is $Hour
is higher than the $Employee
's highest highhours
, then it should use the $Employee
's highest highhours
's wage.
For example; If "Betty Sue" worked 200 hours, it will get Default
's highest highhours
wage... which is 200. Therefore the wage "Betty Sue" makes on her 200th hour is 200 per hour.
Now if Brian works 10000 hours, he will NOT earn Default
's highest highhours
wage...but instead he will earn Brian
's highest highhours
wage, which is 350.
The results of the benchmark:
This is based on 2000 queries (1000 for a match and 1000 for a default non match):
Timwi's Method: 4348 milliseconds
OMG Ponie's Method: 5843 milliseconds
My method using up to 5 queries and atleast 2: 5844 milliseconds