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

  • Home
  • SEARCH
  • 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 894197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:20:41+00:00 2026-05-15T14:20:41+00:00

I have this two tables: mysql> desc vat_rates; +————-+—————+——+—–+———+——-+ | Field | Type |

  • 0

I have this two tables:

mysql> desc vat_rates;
+-------------+---------------+------+-----+---------+-------+
| Field       | Type          | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| rate_id     | varchar(5)    | NO   | PRI | NULL    |       |
| name        | varchar(255)  | NO   |     | NULL    |       |
| type        | enum('O','I') | NO   |     | NULL    |       |
| default     | tinyint(1)    | YES  |     | 0       |       |
+-------------+---------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

mysql> desc vat_rates_details;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| rate_id     | varchar(10) | NO   |     | NULL    |       |
| effect_date | date        | NO   |     | NULL    |       |
| rate        | float       | NO   |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

I need a query that gives me vat_rates.name, vat_rates.rate_id, vat_rates_details.rate WHERE the effect_date is MAX() but <= than now().

Simplifying, I need to select all vat_rates with rate field being the last date based on effect_date but not in the future.

Example with data:

vat_rates:
rate_id = ‘L9’
name = ‘MyVatName’
type = ‘O’
default = 1

vat_rates_details (line 1):
rate_id = ‘L9’
effect_date = ‘2000-01-01’
rate = 20

vat_rates_details (line 2):
rate_id = ‘L9’
effect_date = ‘2010-06-01’
rate = 19

vat_rates_details (line 3):
rate_id = ‘L9’
effect_date = ‘2010-07-01’
rate = 21

The expected result:

+-------------+-------------+------+
| rate_id     | name        | rate |
+-------------+-------------+------+
| L9          | MyVatName   | 19   |
+-------------+-------------+------+

Thanks for your time 🙂

EDIT: Expected rate is 19 not 20. Tks Justin.

  • 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-15T14:20:41+00:00Added an answer on May 15, 2026 at 2:20 pm
    select rd.rate_id, r.name, rd.rate
    from (
        select rate_id, max(effect_date) as MaxEffectDate
        from vat_rates_details
        where effect_date < now()
        group by rate_id
    ) rdm
    inner join  vat_rates_details rd on rdm.rate_id = rd.rate_id and rdm.MaxEffectDate = rd.effect_date
    inner join vat_rates r on rd.rate_id = r.rate_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two mysql tables - a sales table: +----------------+------------------------------+------+-----+---------+-------+ | Field | Type
I have two tables (well, two relevant for this question) : Bets (holds the
Having difficulty articulating this correlated subquery. I have two tables fictitious tables, foo and
I'm wondering if this is possible in SQL. Say you have two tables A
In MySql, I have two tables, A and B. A has as columns A.id,
I have in my MySQL database these two tables: CREATE TABLE IF NOT EXISTS
I have two mysql tables which both have a typeID in common. I am
I have two tables: Status(status_id, desc1, desc2, desc3, desc4) Status_Level(status_level_id, desc, levelD, levelS) The
I have two tables, each with an id field. Here are the two tables:
I'm creating a new DB and I have this problem: I have two type

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.