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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:51:39+00:00 2026-06-14T00:51:39+00:00

I’m trying to write a complex (at least, for my level of knowledge) string

  • 0

I’m trying to write a complex (at least, for my level of knowledge) string but I’m having one hell of a time.

Here’s the problem. I have two tables, one named t1 and one named c1.

The tables are defined as follow:

table T1:

e_id, char(8),  
e_date, datetime,  
e_status, varchar(2)

table C1:

e_id, char(8),  
e_date, datetime,  
e_status, varchar(2)

Each table contains a list of identifiers that may or may not be found in both tables (they may or may not be unique within each table), and associated statuses (can be ‘OK’ or ‘R’ in the T1 table, can be ‘OK’ or ‘C’ in the C1 table), and a datetime, e_date, associated with each occurence of e_id’s

I’m trying to write a query that will:

  • Retrieve all the e_id values in the T1 table that have an e_date that is within the last 24 hours.
  • Retrieve all the occurences of the e_id’s that have occured within the last 24hrs (e_date is bigger than current time – 24h) in T1 within the last 30 days (e_date > now – 30 days), still within table T1 (eg: if e_id’s AAAAAAAA and BBBBBBBB are found in t1 with an e_date that is within the last 24 hours, retrieve all the occurences of e_id’s AAAAAAAA and BBBBBBBB in the same table but that have an e_date that is within the last 30 days)
  • Append the count of e_status = 'OK' for each specific e_id found in the entire T1 table to the row results
  • Append the count of e_Status = 'OK' for each specific e_id found in the entire C1 table to the row results

I’ll do my best to write some sample data/results here. For clarity, I will disregard the tables datatypes. Assume the current date and time are 2012-Nov-08 19:00:00

T1:

  1. e_id: ‘A’, e_date: 2012-Nov-08 10:00:00, e_status: ‘OK’
  2. e_id: ‘A’, e_date: 2012-Nov-08 10:00:00, e_status: ‘R’
  3. e_id: ‘A’, e_date: 2012-Oct-15 10:00:00, e_status: ‘R’
  4. e_id: ‘B’, e_date: 2012-Oct-15 10:00:00, e_status: ‘OK’
  5. e_id: ‘A’, e_date: 2012-Oct-15 10:00:00, e_status: ‘OK’
  6. e_id: ‘A’, e_date: 2012-Oct-15 10:00:00, e_status: ‘R’
  7. e_id: ‘A’, e_date: 2012-Oct-15 10:00:00, e_status: ‘R’
  8. e_id: ‘A’, e_date: 2010-Jan-01 10:00:00, e_status: ‘R’
  9. e_id: ‘A’, e_date: 2010-Jan-01 10:00:00, e_status: ‘R’

C1:

  1. e_id: ‘A’, e_date: 2012-Oct-01 10:00:00, e_status: ‘C
  2. e_id: ‘B’, e_date: 2012-Oct-01 10:00:00, e_status: ‘OK’
  3. e_id: ‘A’, e_date: 2012-Oct-01 10:00:00, e_status: ‘C
  4. e_id: ‘B’, e_date: 2012-Oct-01 10:00:00, e_status: ‘OK’
  5. e_id: ‘A’, e_date: 2012-Oct-01 10:00:00, e_status: ‘OK’

Running the query would yield:

e_id, e_date, e_status, r_count, c_count
1. e_id: ‘A’, e_date: 2012-Nov-08 10:00:00, e_status: ‘OK’, r_count: 6, c_count: 2
2. e_id: ‘A’, e_date: 2012-Nov-08 10:00:00, e_status: ‘R’, r_count: 6, c_count: 2
3. e_id: ‘A’, e_date: 2012-Oct-15 10:00:00, e_status: ‘R’, r_count: 6, c_count: 2
4. e_id: ‘A’, e_date: 2012-Oct-15 10:00:00, e_status: ‘OK’, r_count: 6, c_count: 2
5. e_id: ‘A’, e_date: 2012-Oct-15 10:00:00, e_status: ‘R’, r_count: 6, c_count: 2
6. e_id: ‘A’, e_date: 2012-Oct-15 10:00:00, e_status: ‘R’, r_count: 6, c_count: 2

I am really sorry, I have had to change the date on T1 rows 3 to 7 (rows 3 4 5 6 of the results) as the values were erroneous.

T1’s Row 4 was not returned because no e_id: B was found in the last 24 hours
T1 Rows 8 and 9 were not returned because they were outside of the last 30 days

  • 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-14T00:51:41+00:00Added an answer on June 14, 2026 at 12:51 am

    Time to do some TDQD — Test-Driven Query Design.

    Rows in T1 from the last 24 hours

    SELECT DISTINCT e_id
      FROM T1
     WHERE e_date >= DATE_SUB(NOW(), INTERVAL 24 HOUR)
    

    This will be a prevalent sub-query in the other parts of the query.

    Rows in T1 from the last 30 days…

    …where there was an entry in T1 within the last 24 hours.

    SELECT a.e_id
      FROM t1 AS a
      JOIN (SELECT DISTINCT e_id
              FROM T1
             WHERE e_date >= DATE_SUB(NOW(), INTERVAL 24 HOUR)
           ) AS b ON b.e_id = a.e_id
     WHERE a.e_date >= DATE_SUB(NOW(), INTERVAL 30 DAY)
    

    We can add other columns as we need them.

    Count of rows in T1 with status ‘R’ …

    …where there was an entry in T1 within the last 24 hours

    SELECT a.e_id, COUNT(*) AS r_count  -- Per question; why not t_count?
      FROM t1 AS a
      JOIN (SELECT DISTINCT e_id
              FROM T1
             WHERE e_date >= DATE_SUB(NOW(), INTERVAL 24 HOUR)
           ) AS b ON b.e_id = a.e_id
     WHERE a.e_status = 'R'
     GROUP BY a.e_id
    

    Count of rows in C1 with status ‘C’ …

    …where there was an entry in T1 within the last 24 hours

    SELECT a.e_id, COUNT(*) AS c_count
      FROM c1 AS a
      JOIN (SELECT DISTINCT e_id
              FROM T1
             WHERE e_date >= DATE_SUB(NOW(), INTERVAL 24 HOUR)
           ) AS b ON b.e_id = a.e_id
     WHERE a.e_status = 'C'
     GROUP BY a.e_id
    

    Assemble the set of queries to produce the result

    SELECT a.e_id, a.e_date, a.e_status, c.r_count, d.c_count
      FROM t1 AS a
      JOIN (SELECT DISTINCT e_id
              FROM T1
             WHERE e_date >= DATE_SUB(NOW(), INTERVAL 24 HOUR)
           ) AS b ON b.e_id = a.e_id
      LEFT JOIN -- Because there might be no OK rows in T1
           (SELECT a.e_id, COUNT(*) AS r_count
              FROM t1 AS a
              JOIN (SELECT DISTINCT e_id
                      FROM T1
                     WHERE e_date >= DATE_SUB(NOW(), INTERVAL 24 HOUR)
                   ) AS b ON b.e_id = a.e_id
             WHERE a.e_status = 'OK'
             GROUP BY a.e_id
           ) AS c ON c.e_id = a.e_id
      LEFT JOIN -- Because there might be no OK rows in C1
           (SELECT a.e_id, COUNT(*) AS c_count
              FROM c1 AS a
              JOIN (SELECT DISTINCT e_id
                      FROM T1
                     WHERE e_date >= DATE_SUB(NOW(), INTERVAL 24 HOUR)
                   ) AS b ON b.e_id = a.e_id
             WHERE a.e_status = 'OK'
             GROUP BY a.e_id
           ) AS d ON d.e_id = a.e_id
     WHERE a.e_date >= DATE_SUB(NOW(), INTERVAL 30 DAY)
    

    You probably could write the sub-queries without the 24 hour sub-sub-query, but it is likely to be effective to eliminate as many rows as soon as possible.


    One advantage of the concept behind TDQD is that you can check interim results. There were some basically trivial syntax issues (in part because MySQL is not my primary DBMS), but the change from JOIN to LEFT JOIN for the two COUNT sub-queries is the sort of thing you’re apt to spot as you assemble the query. Trying to get everything right first time is — hard, if not futile. But the step-by-step build-up can give you confidence in what you’ve done. I’d never build a query as complex as this from scratch without testing the component sub-queries.

    Thanks for the (minor) updates, FatalMojo.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I've got a string that has curly quotes in it. I'd like to replace

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.