tags:

views:

88

answers:

1

Assuming a wagetable:

name     lowhours  highhours  wage 
 Default  0.0       40.0       100  
 Default  40.0      50.0       150  
 Default  50.0      70.5       154  
 Default  70.5      100.0      200  
 Brian    0.0       40.0       200  
 Brian    40.0      50.0       250  
 Brian    50.0      60.0       275  
 Brian    60.0      70.0       300  
 Brian    70.0      80.0       325  
 Brian    80.0      9999.0     350  
 Chad     0.0       40.0       130  
 Chad     40.0      9999.0     170 

I am currently using this code (AutoIT scripting:

func getCurrentWage($Employee, $Hour_number)
    Dim  $row
    Local $Wage = 0
    Local $Found = "found"
    _SQLite_QuerySingleRow(-1, "SELECT wage FROM wagetable WHERE name LIKE " & _SQLite_Escape($Employee) & " AND " & _SQLite_Escape($Hour_number) & " BETWEEN lowhours AND highhours;", $row)
    if @error then
        _SQLite_QuerySingleRow(-1, "SELECT wage FROM wagetable WHERE name LIKE 'Default' AND " & _SQLite_Escape($Hour_number) & " BETWEEN lowhours AND highhours;", $row)
        $Found = "not found"
    endif

    If ($row[0] == "") Then Msgbox(0,0,"Error getCurrentWage")
    $Wage = $row[0]

    Debug("Wage='" & $Wage & "'  because " &$Employee&" was "& $Found& " -- and Hours Elapsed is " & $Hour_number, true)
    return $Wage
EndFunc

So those 2 queries are perfect if the Hour_number is inbetween a low hour or a high hours. I need some sort of query where it will basically do something like:

SELECT wage from wagetable WHERE name LIKE $Employee AND max(highhours)

and then just repeat it for 'Default' is the employee is not found.

Extra: Is it possible to try the 'Default' if $Employee is not found with just 1 query?

+1  A: 

Is it possible to try the 'Default' if $Employee is not found with just 1 query?

  SELECT name, wage, highhours
    FROM wagetable
   WHERE name like 'Brian' OR
         name like 'Default'
ORDER BY name,
         highhours desc

This query works when querying for Brian, but for it to work for any name the Default should be stored in your database starting with a special character _-+@, because numbers and letters come first in sorting.

Another way would be for you to create another column to the table, for the sake of our argument, called priority which should have the value 0 for Default and 1 for any other user. Then you could simply do:

  SELECT name, wage, highhours
    FROM wagetable
   WHERE name like 'Brian' OR
         name like 'Default'
ORDER BY priority desc,
         highhours desc

Of course it is the same solution, but it is a better approach, than relying on a special character in the name of the default values.

mhitza
Thanks for the detailed answer. I will set a priority because this process seems easiest.
BHare
Surely no need for a new column. `ORDER BY CASE WHEN name='default' THEN 1 ELSE 0 END DESC, highhours desc` would do the same?
Martin Smith
@Martin Very nice. I think it would be easier to set the 1 or 0 in the beginning when the table is initialized, rather than running an if else on every query. I am sure the performance difference is small, but I will be running this query 3-4 times in 1 second
BHare
@Brian: Evaluating a `CASE WHEN` might take a few microseconds. Retrieving an additional value from an additional `Priority` column might take milliseconds, i.e. a thousand times longer. (Yes, I know, I’m not taking caching into account... that’s not the point. The point is that it is pointless to worry about performance in this kind of time magnitude.)
Timwi
@Martin interesting approach. I am considering it even better than mine. But if the table is going to change and there will be more "accounts" like default, my approach would be less complicated. +1 non the less
mhitza