简体   繁体   中英

Oracle Pivot Query Compare Columns

I am rusty on my Oracle skills anyways I have the following Oracle Query that pivots and give me what I need however I need to compare the last two columns. Here is the query:

SELECT * FROM (select * 
FROM (select TO_ENTITY_ID, END_DATE_DAY_ID, EASSIGNMENT_ID from ENTITY_eassignment WHERE TO_ENTITY_ID IN  (11466248, 993051,993037))
PIVOT (MAX(END_DATE_DAY_ID) AS t FOR (EASSIGNMENT_ID) in (1, 1002)));

This ultimately returns 3 rows and 3 columns

TO_ENTITY_ID    1_T     1002_T     
993037          29220   29220      
11466248        29220   16126      
993051          15255   16004   

What I need to do is add a 4th column here that compares the last two columns if they are the same value then the 4th column should be true/false.

TO_ENTITY_ID    1_T     1002_T  SAME_VALUE     
993037          29220   29220   TRUE
11466248        29220   16126   FALSE   
993051          15255   16004   FALSE

Any help would be greatly appreciated.

(CASE WHEN "1_T" = "1002_T"
      THEN 'TRUE'
      ELSE 'FALSE'
  END)

in an outer query should work. That is

SELECT to_entity_id,
       "1_T",
       "1002_T",
       (CASE WHEN "1_T" = "1002_T"
             THEN 'TRUE'
             ELSE 'FALSE'
         END)
  FROM( <<your existing query>> )

If you want to filter

SELECT to_entity_id,
       "1_T",
       "1002_T",
       (CASE WHEN "1_T" = "1002_T"
             THEN 'TRUE'
             ELSE 'FALSE'
         END)
  FROM( <<your existing query>> )
 WHERE "1_T" = "1002_T" -- (or !=)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM