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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:07:34+00:00 2026-05-25T21:07:34+00:00

I’m having trouble getting MySQL to return a query properly. Here’s my data: id

  • 0

I’m having trouble getting MySQL to return a query properly.

Here’s my data:

id            date            value
2             2011-01-04         55.66
2             2011-03-23         22.33
2             2011-04-21          9.44
5             2010-01-04        104.55
5             2011-02-03         38.82
...              ...              ...

i’m trying to get a query to return:

select t1.id, max(t1.date), t1.value, t2.id, min(t2.date), t2.value
from tab1 as t1, tab1 as t2
where t1.id = t2.id
and t1.date <= '2011-03-31'
and t2.date >= '2011-04-01'
group by t1.id;

But it is taking forever (db has ~ 1mm lines). I’ve tried various joins but then it seems to ignore the date < and > statements. Basically I want each customers last purchase date and amount before 4/1/2011 and their first purchase and date on or after 4/1/2011. Any suggestions would be great.

  • 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-25T21:07:35+00:00Added an answer on May 25, 2026 at 9:07 pm

    Your query is flawed, the columns t1.value and max(t1.date) do not have a relation to one and other.

    You need to rewrite it as follows, if you want to know the total purchases per the date selected.

    SELECT st1.id, st1.date, st1.total_value, st2.id, st2.date, st2.total_value
    FROM (SELECT t1.id, t1.date, sum(t1.value) as total_value
          FROM tab1 t1
          WHERE t1.date <= '2011-03-31'
          GROUP BY t1.id
          HAVING t1.date = MAX(t1.date)
          ) st1
    INNER JOIN (SELECT t2.id, t2.date, sum(t2.value) as total_value
               FROM tab1 t2
               WHERE t2.date > '2011-03-31'
               GROUP BY t2.id
               HAVING t2.date = MAX(t2.date)
               ) st2 
      ON (st1.id = st2.id)
    

    Make sure you have an index on id and date

    Remarks
    id is generally understand as a shorthand for the primary key.
    Having a field called id that does not a unique index, is confusing and widely considered a code smell.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to construct a data frame in an Rcpp function, but when I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.