Saturday, 15 January 2011

mysql - SQL selecting the rows with latest datetime group by another column -


I have a database with several tables that looks like this (slightly simplified)

 < Code> ID | Item_ID | Type_ID | Processed_time | Other calls ------ | ----------- | --------- | ------------------- --- | ------------- 1 | 1 | 1 | 2015-03-17 00:11:22 | ... 2 | 1 | 1 | 2015-03-17 00:22:33 | ... 3 | 1 | 2 | 2015-03-17 00:11:22 | ... 4 | 1 | 2 | 2015-03-17 00:33:44 | ... 5 | 2 | 1 | 2015-03-17 00:22:33 | ... 6 | 2 | 1 | 2015-03-17 00:11:22 | ... 7 | 2 | 2 | 2015-03-17 00:22:33 | ... 8 | 2 | 2 | 2015-03-17 00:33:44 | ... ... | ... | ... | ... | ...  

I am trying to execute several questions, but they all follow the same subject: For all the latest (processed time (datetime) ) Selecting rows) is grouped by type_id.

For example, to get item_id = 1, for example:

  id | Item_ID | Type_ID | Processed_time | Other calls ------ | ----------- | --------- | ------------------- --- | ------------- 2 | 1 | 1 | 2015-03-17 00:22:33 | ... 4 | 1 | 2 | 2015-03-17 00:33:44 | ... ... | ... | ... | ... | ...  

Additionally, I want to query this for all the items, I.E:

  id | Item_ID | Type_ID | Processed_time | Other calls ------ | ----------- | --------- | ------------------- --- | ------------- 2 | 1 | 1 | 2015-03-17 00:22:33 | ... 4 | 1 | 2 | 2015-03-17 00:33:44 | ... 5 | 2 | 1 | 2015-03-17 00:22:33 | ... 8 | 2 | 2 | 2015-03-17 00:33:44 | ... ... | ... | ... | ... | ...  

The main issue I am running is actually certain how to get an ID after grabbing the most recently processed time. In the direction in which I place towards direction, something happens in that direction such as SELECT id, MAX (processed_time) by item group type_id though id clear in that query And

(Note that processed_time is not unique) I'm not really sure

  SELECT t. * From the table where not (select 'one' from table table 2 where t 2.item_id = t.item_id and t2.type_id = t.type_id and t2.processed_time & gt; t .processed_time and t2.id & gt; T.id - (Note that processed_time is not unique))  

Return the last item for the same item i_id and type_id


No comments:

Post a Comment