Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8825329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:51:08+00:00 2026-06-14T06:51:08+00:00

Hi I have a select query where I am selecting 2 different date columns

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:51:09+00:00Added an answer on June 14, 2026 at 6:51 am

    its not totally clear what you want without you putting the sample data you want. but would this be it ?

    select *
      from (select work_order_no,install_remove_type,installed_item_id,
                       installed_performed_on as irdate 
                  from install_remove_items 
                 where work_order_no is not null  
                   and installed_item_id = 'IID000174096'
                union all
                select work_order_no,install_remove_type,removed_item_id,
                       removed_performed_on as irdate
                  from install_remove_items 
                 where work_order_no is not null  
                   and removed_item_id= 'IID000174096')
             order by irdate desc;
    

    revised to as per your sample:

    SQL> select *
      2    from (select workOrderno, install_remove_type,
      3                 case
      4                   when installed_performed_on > removed_performed_on
      5                     or removed_performed_on is null then ir.installed_performed_on
      6                   else removed_performed_on
      7                 end last_action_date,
      8                 coalesce(ir.installed_item_id,ir.removed_item_id)  item_id
      9            from install_remove_items ir
     10           where workOrderno is not null
     11             and (installed_item_id = 'IID000174096' or removed_item_id= 'IID000174096')
     12           order by last_action_date desc )
     13   where rownum = 1
     14  /
    
    WORKORDERNO INSTALL_RE LAST_ACTI ITEM_ID
    ----------- ---------- --------- --------------------
         444444 REPLACED   15-NOV-12 IID000174096
    

    or a list for every item you can do like this:

    SQL> select * from install_remove_items;
    
    IR_ID                INSTALL_RE WORKORDERNO INSTALLED_ITEM_ID    INSTALLED REMOVED_ITEM_ID      REMOVED_P
    -------------------- ---------- ----------- -------------------- --------- -------------------- ---------
    xxxxyyyy5555         INSTALL       22335522 IID000174096         10-NOV-12
    xxxxyyyy3333         REMOVE          223542                                IID000174096         12-NOV-12
    xxxxyyyy1111         WORKEDON        111111                                IID000174096         13-NOV-12
    xxxxyyyy2222         REPLACED        444444 IID000174096         15-NOV-12 IID000174096         14-NOV-12
    3242                 REMOVE              43                                IID000174097         14-DEC-12
    123                  INSTALL            123 IID000174097         15-NOV-12
    
    6 rows selected.
    
    SQL> select workorderno, install_remove_type, item_id, last_action_date
      2    from (select l.*,
      3                  row_number() over(partition by item_id order by last_action_date desc) rn
      4             from (select workorderno, install_remove_type,
      5                           case
      6                             when installed_performed_on > removed_performed_on or
      7                                  removed_performed_on is null then
      8                              ir.installed_performed_on
      9                             else
     10                              removed_performed_on
     11                           end last_action_date,
     12                           coalesce(ir.installed_item_id, ir.removed_item_id) item_id
     13                      from install_remove_items ir
     14                     where workorderno is not null) l)
     15   where rn = 1;
    
    WORKORDERNO INSTALL_RE ITEM_ID              LAST_ACTI
    ----------- ---------- -------------------- ---------
         444444 REPLACED   IID000174096         15-NOV-12
             43 REMOVE     IID000174097         14-DEC-12
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query (notice that all 3 subqueries are selecting different columns from
I have 6 columns in a table. I have a select query which selects
I have a SELECT query that I am expecting millions of results from. I
I have a select query which returns a list of data and I need
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
I'm looking for a query to select rows from two different tables, keeping the
Is it possible to have selective queries in PostgreSQL which select different tables/columns based
Using the following query to select data from a table in 15 minute intervals
I have a table with the columns, id, date, estValue & gradeid. Each grade
I have one select: select * from table1 where col1=10; and another select: select

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.