3

Suppose I have some data like:

id  status activity_date
--- ------ -------------
101   R     2014-01-12
101   Mt    2014-04-27 
101   R     2014-05-18
102   R     2014-02-19

Note that for rows with id = 101 we have activity between 2014-01-12 to 2014-04-26 and 2014-05-18 to current date.

Now I need to select that data where status = 'R' and the date is the most current date as of a given date, e.g. if I search for 2014-02-02, I would find the status row created on 2014-01-12, because that was the status that was still valid at the time for entity ID 101.

3
  • Do you specifically want to select the activity date 2014-02-02 and no other values?
    – Scott
    Aug 27, 2015 at 9:44
  • @Scott, I want to select data which status R(running) on the selected date like '2014-02-02'
    – Ashish
    Aug 27, 2015 at 9:47
  • This is a common problem in Data Warehousing and known as a Kimball Slowly Changing Dimension.
    – Alex R
    Nov 3, 2018 at 17:02

7 Answers 7

1

If I understand correctly:

Step 1: Convert the start and end date rows into columns. For this, you must join the table with itself based on this criteria:

SELECT
    dates_fr.id,
    dates_fr.activity_date AS date_fr,
    MIN(dates_to.activity_date) AS date_to
FROM test AS dates_fr
LEFT JOIN test AS dates_to ON 
    dates_to.id = dates_fr.id AND
    dates_to.status = 'Mt' AND
    dates_to.activity_date > dates_fr.activity_date
WHERE dates_fr.status = 'R'
GROUP BY dates_fr.id, dates_fr.activity_date
+------+------------+------------+
| id   | date_fr    | date_to    |
+------+------------+------------+
|  101 | 2014-01-12 | 2014-04-27 |
|  101 | 2014-05-18 | NULL       |
|  102 | 2014-02-19 | NULL       |
+------+------------+------------+

Step 2: The rest is simple. Wrap the query inside another query and use appropriate where clause:

SELECT * FROM (
    SELECT
        dates_fr.id,
        dates_fr.activity_date AS date_fr,
        MIN(dates_to.activity_date) AS date_to
    FROM test AS dates_fr
    LEFT JOIN test AS dates_to ON 
        dates_to.id = dates_fr.id AND
        dates_to.status = 'Mt' AND
        dates_to.activity_date > dates_fr.activity_date
    WHERE dates_fr.status = 'R'
    GROUP BY dates_fr.id, dates_fr.activity_date
) AS temp WHERE '2014-02-02' >= temp.date_fr and ('2014-02-02' < temp.date_to OR temp.date_to IS NULL)
+------+------------+------------+
| id   | date_fr    | date_to    |
+------+------------+------------+
|  101 | 2014-01-12 | 2014-04-27 |
+------+------------+------------+

SQL Fiddle

1

You can try

select id, status, activity_date 
from TABLE 
where status = "R" and activity_date = "2014-02-02"

where TABLE is name of your table

5
  • 1
    May as well just use SELECT * rather than name all of his attributes individually
    – Scott
    Aug 27, 2015 at 9:45
  • yes, but if somebody asks of simple things I try to show it as simple as possible...Maybe he will read it... Aug 27, 2015 at 9:47
  • I can not select it activity_date = "2014-02-02" , because it is the date range. Ex. 101 product is R(running) from (2014-01-12 to 2014-04-26) and also (2014-05-18 to till today). So how can select this product was running on the "2014-02-02"
    – Ashish
    Aug 27, 2015 at 9:50
  • I don't understand why you've directed that at me
    – Scott
    Aug 27, 2015 at 9:51
  • I also dont understant what you mean. Do you want to select only with id = 101? Aug 27, 2015 at 9:53
1

I think you need following ans

SELECT id,MAX(CAST(ACTIVITY_DATE AS date),MIN(CAST (ACTIVITY_DATE AS date)  
FROM Table_Name WHERE CAST('2014-02-02' AS date)
BETWEEN  MIN(CAST (ACTIVITY_DATE AS date) AND MAX(CAST(ACTIVITY_DATE AS date) 
 AND Status='R'
GROUP BY id
0

Try this:

select * from yourtable 
where status='R' and activity_date= '2014-02-02'
3
  • Read OP's comment below the question.
    – Salman A
    Aug 27, 2015 at 9:54
  • @SalmanA:- I want to select data which status R(running) on the selected date like '2014-02-02' If I take this then the query which I posted seems correct. But I think OP is still not making it clear as to what s/he is looking for specially the line in question **which** activity_date 2014-02-02 Aug 27, 2015 at 9:56
  • The description in bold makes it somewhat clear that OP is talking about date ranges.
    – Salman A
    Aug 27, 2015 at 10:40
0

You can make a query to effectively give you the most status as of a date, e.g.

SELECT
    id, 
    substr(max(concat(activity_date, status)),11) as status,
    max(activity_date) as activity_date
FROM table
WHERE activity_date <= '2014-02-02'
GROUP by id;

Then, similar to Salman's answer, you can use this result inside another query and look for all those results with a status of 'R'

SELECT * from (
    SELECT
        id,
        substr(max(concat(activity_date, status)),11) as status,
        max(activity_date) as activity_date
    FROM table
    WHERE activity_date <= '2014-02-02'
    GROUP by id
) AS temp WHERE temp.status = 'R';

Edit: Rather than use the questionable method of sorting the statuses, you could identify the relevant maximum record with a sub-query, so the original query would become

SELECT join1.* FROM table AS join1
INNER JOIN (
    SELECT id, max(activity_date) as max_activity_date
    FROM table
    WHERE activity_date < '2014-02-02'
    GROUP BY id
) AS join2
ON join1.id = join2.id AND join1.activity_date = join2.max_activity_date;

and the full query

SELECT * from (
    SELECT join1.* FROM table AS join1
    INNER JOIN (
        SELECT id, max(activity_date) as max_activity_date
        FROM table
        WHERE activity_date < '2014-02-02'
        GROUP BY id
    ) AS join2
    ON join1.id = join2.id AND join1.activity_date = join2.max_activity_date
) AS temp WHERE temp.status = 'R';
-1

try the following

SELECT *
FROM your_relation
WHERE status='R'
AND activity_data="2014-02-02"
0
-1

I completely agree with Salman's response, the table could be designed in a fashion that allows for greater query accuracy and extensibility. However, the question asked, with regards to a query selecting information based on status and date range can be expressed as.

SELECT * FROM Table_1
WHERE ((status = 'R')
AND ((activity_date BETWEEN '2014-01-12' AND '2014-04-26')
OR activity_date  > CONVERT(DATETIME, '2014-05-17')))

This will select all data with a status of 'R' and will use the BETWEEN operator for the range desired; moreover, the conversion of the final operator is because the expression is evaluated as a mathematical expression and requires explicit conversion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.