Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

return row based on values in another row

Posted on 2014-03-11
4
Medium Priority
?
39 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 35

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 143

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 35

Accepted Solution

by:
Dan Craciun earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

670 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