Solved

return row based on values in another row

Posted on 2014-03-11
4
28 Views
Last Modified: 2016-06-04
Subset of table:
id               date                           product_id
1                11/22/2012                L11
1                11/15/1990                L11.1
2                10/2/2005                  L22
2                10/4/2007                  P3
3                 2/5/2010                   L3
3                 3/2/2011                   L11
4                4/6/2012                    L22
4                6/2/1999                    L4
etc

I want to return all L11, L11.1, L22, where the date is older than two years for all of the product id's I am looking for (L11, L11.1, L22).  All  L11, L11.1, L22 have to be older than two years else nothing should be returned per id.  

For example results would look like:
id               date                           product_id
2                10/2/2005                  L22
3                 3/2/2011                   L11

Thanks!
0
Comment
Question by:needplease
  • 2
4 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39919870
SELECT * FROM table 
WHERE product_id IN ('L11', 'L11.1', 'L22') AND DATE_DIFF(NOW(), date) > 730

Open in new window

HTH,
Dan
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39919907
>older than two years
I would prefer to do this differently than what was suggested above (though that one will work:
AND date < DATE_SUB( NOW(), INTERVAL 2 YEAR )

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-sub
the explanation is that with the above suggested syntax, it will work, but will be unable to use a index on the date column.
so, if you have a index on product_id + date column, that query shall be as efficient as possible.
0
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39920669
Re-reading your question, I see my query would return an unwanted value (id 1, where you had a more recent order). See below the modified query, also incorporating Guy's suggestion:
SELECT * FROM table 
    WHERE product_id IN ('L11', 'L11.1', 'L22') 
        AND date < DATE_SUB( NOW(), INTERVAL 2 YEAR )
        AND id NOT IN (
        SELECT id FROM table 
            WHERE product_id IN ('L11', 'L11.1', 'L22') 
                AND date >= DATE_SUB(NOW(), INTERVAL 2 YEAR) )

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now