Hi I have a select query where I am selecting 2 different date columns from the table, I need to merge them into one column in the result and order in the descending order of the date.
The query looks like following:
select work_order_no,install_remove_type,removed_item_id,installed_item_id,
removed_performed_on as irdate,
installed_performed_on as irdate
from install_remove_items
where work_order_no is not null
and (installed_item_id = 'IID000174096' or removed_item_id= 'IID000174096')
The result of the above query is that there are 2 columns created
irdate – for removed_performed_on
and
irdate_1 – for installed_performed_on
which is not the way I want it..How do I have the date related data in the ‘irdate’ column and also sorted in desc order?
Update:
sample data in install_remove_items table (4 records for 4 Action types in INSTALL_REMOVE_TYPE field):
ir_id: xxxxyyyy5555, install_remove_type= 'INSTALL', workOrderno = 22335522, installed_item_id = IID000174096, installed_performed_on = '11/10/2012', removed_item_id = null, removed_performed_on = null.
ir_id: xxxxyyyy3333, install_remove_type= 'REMOVE', workOrderno = 223542, installed_item_id = null, installed_performed_on = null, removed_item_id = IID000174096, removed_performed_on = '11/12/2012'.
ir_id: xxxxyyyy1111, install_remove_type= 'WORKEDON', workOrderno = 111111, installed_item_id = null, installed_performed_on = null, removed_item_id = IID000174096, removed_performed_on = '11/13/2012'.
ir_id: xxxxyyyy2222, install_remove_type= 'REPLACED', workOrderno = 444444, installed_item_id = IID000174096, installed_performed_on = 11/15/2012, removed_item_id = IID000174096, removed_performed_on = '11/14/2012'.
So there are 4 types of Action, ‘removed’ fields are populated for REMOVE action, ‘install’ fields for INSTALL action, both fields for REPLACED action and ‘removed’ fields for WORKEDON action.
I want to write a query to find what kind of action out of the four was performed last (compared to today’s date or an input date value) based on the install_performed_on and removed_performed_on date (and also get that date).
Hope this makes things clear.
I know union may be good, but currently the Java API we have for SQL doesnt support union, so I may have to do a combination of SQL and JAVA code to achieve my output.
Thanks in advance.
its not totally clear what you want without you putting the sample data you want. but would this be it ?
revised to as per your sample:
or a list for every item you can do like this: