简体   繁体   中英

Filter data with query

id     refid    date1         date2         nextdate
5      10       2008-02-21    2009-02-21    004/2008
6      10       2009-02-09    2010-02-09    002/2009
7      10       2010-02-08    2011-02-08    001/2010
10     11       2007-02-15    2008-02-15    002/2007
11     11       2008-02-21    2009-02-21    001/2008
12     11       2009-02-09    2010-02-09    001/2009
13     11       2010-02-09    2011-02-09    002/2010
14     11       2011-07-19    2012-07-19    054/2011
15     11       2012-07-17    2014-07-17    066/2012
18     14       2007-02-15    2008-02-15    006/2007
25     16       2007-02-15    2008-02-15    004/2007
27     16       2009-02-10    2010-02-10    004/2009
28     16       2010-02-12    2011-02-12    005/2010
29     16       2011-07-26    2012-07-26    055/2011
30     16       2012-07-18    2014-07-18    067/2012

I have this datatable. I need query that will do the following: Return all rows for the refid where there exists a date2 value greater than 2014-01-01 (If there is id then I need all ids)

Result should be like this:

id    refid    date1         date2         nextdate
10    11       2007-02-15    2008-02-15    002/2007
11    11       2008-02-21    2009-02-21    001/2008
12    11       2009-02-09    2010-02-09    001/2009
13    11       2010-02-09    2011-02-09    002/2010
14    11       2011-07-19    2012-07-19    054/2011
15    11       2012-07-17    2014-07-17    066/2012
25    16       2007-02-15    2008-02-15    004/2007
27    16       2009-02-10    2010-02-10    004/2009
28    16       2010-02-12    2011-02-12    005/2010
29    16       2011-07-26    2012-07-26    055/2011
30    16       2012-07-18    2014-07-18    067/2012

This is quite basic but if I've understood you correctly:

DELETE FROM tableName 
WHERE date2 < '2014-01-01' AND NOT (refid IS NULL)

Edit

As stated in the comment below I possibly misunderstood the following part of the question to mean that the entries needed deleting.

Remove all rows with refid where date2 is less than 2014-01-01

In which case the following would return all results after the specified date instead:

SELECT *
FROM tableName 
WHERE date2 > '2014-01-01' AND NOT (refid IS NULL)

I have also assumed that the "with refid" means that refid should be populated.

To get the results described in the original question however I'd simplify the query as follows as the date part specificied wouldn't give the results:

SELECT *
FROM tableName 
WHERE (refid = 11) OR (refid = 16)

Another Edit

The following should return all entries assigned to a refid with any entries with a date2 after 2014-01-01

SELECT DISTINCT t1.*
FROM tableName t1
INNER JOIN tableName t2 ON t1.refid = t2.refid AND t2.date2 > '2014-01-01'

Alternatively you could use exists:

SELECT *
FROM tableName t1
WHERE EXISTS(
    SELECT * 
    FROM tableName t2
     WHERE t1.refid = t2.refid AND t2.date2 > '2014-01-01'
)

Or link to a separate table of refid 's which fit the profile

SELECT *
FROM tableName t1
INNER JOIN (
    SELECT DISTINCT refid 
    FROM tableName 
    WHERE date2 > '2014-01-01'
)t2 ON t1.refid = t2.refid

This might do what you want, if I understand your question correctly:

SELECT   *
FROM     datatable
WHERE    refid IN (SELECT  DISTINCT refid 
                   FROM    datatable
                   WHERE   date2 > '20140101')

The subquery gives back all distinct refid values for which a date2 exists that is greater than 2014-01-01. You use the list of these refid 's to return all rows with those refid 's.

Edit
Adding an SQL Fiddle to experiment with. (Also, SQL Fiddle is a very useful tool to help you formulate your database questions clearer & make them more likely to be answered in a useful way!)

To get the results described in your question the following works.

SELECT 
    *
FROM 
    datatable
WHERE 
    refid = 11
    OR refid = 16

If you look at your required results:

id    refid    date1         date2         nextdate
10    11       2007-02-15    2008-02-15    002/2007
11    11       2008-02-21    2009-02-21    001/2008
12    11       2009-02-09    2010-02-09    001/2009
13    11       2010-02-09    2011-02-09    002/2010
14    11       2011-07-19    2012-07-19    054/2011
15    11       2012-07-17    2014-07-17    066/2012
25    16       2007-02-15    2008-02-15    004/2007
27    16       2009-02-10    2010-02-10    004/2009
28    16       2010-02-12    2011-02-12    005/2010
29    16       2011-07-26    2012-07-26    055/2011
30    16       2012-07-18    2014-07-18    067/2012

You will notice that the date2 field does not meet the criteria which you have defined in your question:

I have this datatable. I need to filter datatable by following condition: Remove all rows with refid where date2 is less than 2014-01-01

The date2 fields range from between 2008-02-15 and 2014-07-18. The only other reference which you have stated in your criteria was "with refid ". Looking at your table and the results required, you will notice that the refid of the items you wanted to select were either 11 or 16 so the filtering on date specified in the question is proving to be more of a distraction to those attempting to help. You should consider clarifying your question to prevent further confusion and maybe creating an sql fiddle (goto: http://sqlfiddle.com/ ) so that people have the ability to compare their results to your required results rather than telling people to re-read your question.

EDIT:

The following should return all dataTable results where there is one or more dataTable entries with the same refid and date2 > '2014-01-01'

SELECT *
FROM datatable t1
INNER JOIN (
    SELECT DISTINCT refid 
    FROM datatable 
    WHERE date2 > '2014-01-01'
)t2 ON t1.refid = t2.refid

尝试这个

SELECT * FROM table WHERE date2 >='2014-01-01'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM