Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1003707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:01:12+00:00 2026-05-16T08:01:12+00:00

This is continuing questions from Selecting the highest salary Assuming a table ‘ wagetable

  • 0

This is continuing questions from 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:01:12+00:00Added an answer on May 16, 2026 at 8:01 am

    Here’s what I mentioned in my comment to Alex:

    SELECT s.wage
      FROM (SELECT x.wage
              FROM WAGETABLE x
             WHERE x.name LIKE '$Employee'
               AND '$Hour' BETWEEN x.lowhours AND x.highhours
            UNION ALL
            SELECT y.wage AS wage
              FROM WAGETABLE y
              JOIN (SELECT wt.name,
                           MAX(wt.highhours) AS max_hours
                      FROM WAGETABLE wt
                  GROUP BY wt.name) bb ON bb.name = y.name
                                      AND bb.max_hours = y.highhours
             WHERE y.name LIKE '$Employee'
            UNION ALL
            SELECT t.wage
              FROM WAGETABLE t
             WHERE t.name = 'Default'
               AND '$Hour' BETWEEN t.lowhours AND t.highhours
            UNION ALL
            SELECT z.wage
              FROM WAGETABLE z
              JOIN (SELECT wt.name,
                           MAX(wt.highhours) AS max_hours
                      FROM WAGETABLE wt
                  GROUP BY wt.name) aa ON aa.name = z.name
                                      AND aa.max_hours = z.highhours
             WHERE z.name LIKE 'Default') s
     LIMIT 1
    

    Previously:

    SELECT s.wage
      FROM (SELECT x.wage
              FROM WAGETABLE x
             WHERE x.name LIKE '$Employee'
               AND '$Hour' BETWEEN x.lowhours AND x.highhours
            UNION ALL
            SELECT y.wage
              FROM WAGETABLE y
             WHERE y.name LIKE 'Default'
               AND y.highhours = (SELECT MAX(highhours)
                                    FROM WAGETABLE wt
                                   WHERE wt.name = y.name)) s
     LIMIT 1
    

    Because of selecting for a match on the user/etc is in the upper portion of the UNION, if such a record exists – it will be the first row. If there are no matches, the Default match will be the first row. The Default will always be in the resultset, hence the need for the LIMIT…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Continuing from this question : When I am trying to do fopen on Windows,
Continuing from this question With each system call, the function constructs a set of
Continuing from this question . I'm having trouble deserializing the following json array (Sorry
Continuing from this question , i am confused whether DISPID_VALUE on IDispatch::Invoke() for script
Continuing from this question, I've got a form with all the Vehicles listed for
Continuing on from this question programmatically creating a drop down list I would like
Continuing from this question: Why can't you reduce the visibility of a method in
Continuing my problem from yesterday, the Silverlight datagrid I have from this issue is
I have a list like this: ['Ww','Aa','Bb','Cc','ww','AA','BB','CC'] And continuing in such a pattern, with
I don't know how to query this one out. I have a table structure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.