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 8318367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:56:41+00:00 2026-06-08T21:56:41+00:00

I have two tables, a Countries table and a Weather table. I would like

  • 0

I have two tables, a Countries table and a Weather table. I would like to retrieve all of the names of countries where it has not rained within the last 15 days.

The weather table has a column called “DayNum”, which goes from 1 -> infinity and increases by 1 on each day, it is unique. This table also has a column called “Rain” which is just a bit boolean value of 0 or 1.

Also, not all Countries were added on the same day, so the max DayNum will be different for each country.

Examples of tables below (data is snipped for readability):

Countries:

    ID     Name
     1      USA
     2      Cananda
     3      Brazil

Weather

    ID    Country_id    DayNum    Rain
     1        1           1         0
     2        1           2         0
     3        1           3         1

Here is my current attempt at a query (been working on this for days):

    SELECT countries.name, weather.daynum
    FROM countries INNER JOIN weather ON countries.id = weather.country_id
    GROUP BY countries.name
    HAVING weather.daynum > (MAX(weather.day_num) - 15) AND SUM(weather.rain) = 0;

I think this should work, but I’m having serious performance issues. The actual query I need to write deals with different data (same exact concept) and millions of rows. This query seems to get slower at an exponential rate.

Can anyone offer any advice?

Another idea I had was to somehow limit the JOIN to only grab the top 15 records (whilst ORDERing BY weather.day_num), but I Haven’t found a way to do this within a JOIN (if it’s even possible).

  • 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-06-08T21:56:42+00:00Added an answer on June 8, 2026 at 9:56 pm

    You’re not interested in the amount of rain, just whether it exists, so…

    select * from countries
    left join
    (
            select weather.country_id 
            from weather 
                inner join 
                (select country_id, MAX(daynum) as maxdaynum from weather group by country_id) maxday
                    on weather.country_id = maxday.country_id
                    and weather.daynum>maxday.maxdaynum-3
                    where rain=1
            ) rainy
    on countries.id = rainy.country_id
    where country_id is null    
    

    I presume you’ve already indexed your tables appropriately

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

Sidebar

Related Questions

I have two table like below CREATE TABLE IF NOT EXISTS `countries` ( `id`
I have two tables, one called countries and one called country. Countries has both
I have two tables (country & ducks) where the country table has every country
I have two tables - countries , tours . countries has fields ( id
I have two tables, one containing cities and one containing countries. Those are bi-directional
I have two tables: table a ida valuea 1 a 2 b 3 c
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables user table user_id | name | 1 | peter |
I have two tables, a user table and a application table: User id username
I have two tables in my MySQL database, one is a library of all

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.