I have a table which looks like this:
- id
- create_date (stored as dateTime)
- industry
Example entries:
1, '2012-12-01 00:00:00', 'retail'
1, '1969-12-31 19:00:00', 'leisure and tourism'
1, '2010-10-01 00:00:00', 'telesales'
2, '2010-2-01 00:00:00', 'finance'
2, '2010-2-01 00:00:00', 'retail'
2, '2009-08-01 00:00:00', 'finance'
Now the create date is the most recent if, the create date is '1969-12-31' OR if that does not exist then it it the most recent date. Hence I change any dates '1969-12-31' to todays date.
However now I want to look at only the recent date value.
This seems a trickier problem, because you have to deal with conversion as well as order after conversion to then choose the first rank. Also some people you don't even need to convert their most recent date, such as 2.
I want the data to look like the following:
1, '1969-12-31 19:00:00', ['leisure and tourism']
2, '2010-2-01 00:00:00', ['finance', 'retail']
I've been looking at examples, here, but it doesn't deal with the data transform. Also another issue is the case when a single id, has two identical most recent create_date's, as shown with id = 2, where we have to concatenate the results.
I've also attempted to create a case statement to assist in conversion:
CASE WHEN create_date = '1969-12-31 19:00:00'
THEN CONVERT(VARCHAR(19), GETDATE(),120)
ELSE create_date --Format: yyyy-mm-dd hh:mi:ss
END AS createDate
Please note, I am using SQL Server.