0

I have 2 different transtions in the same table how can i join them into one line without subquerys and then joining them because i'm trying to combine multiple transtions cases if i create subquery for each transtions case and join them its causing performance issue. really appricate your help

CUREENT VIEW

ORDER         ACTION    DATE_TIME_CREATED
D8ZH4RJSB-689   PICK    8/1/2018 0:35
D8ZH4RJSB-689   SHIP    8/1/2018 12:03

Requiered Output

ORDER          PICK_DATE_TIME_CREATED    SHIP_DATE_TIME_CREATED
D8ZH4RJSB-689     8/1/2018 0:35            8/1/2018 12:03

This is my current code

SELECT "ORDER", --,"ACTION",ship_complete_time as date_time_created
CASE WHEN "ACTION" = 'SHIP'
THEN ship_complete_time
END AS SHIP_COM_TIME,
CASE WHEN "ACTION" = 'PICK'
THEN ship_complete_time
END AS pick_time  
FROM TRANSATIONS
WHERE "ORDER" = 'D8ZH4RJSB-689'

current output

ORDER           SHIP_COM_TIME              PICK_TIME
D8ZH4RJSB-689        (null)              2018-08-01 00:35
D8ZH4RJSB-689   2018-08-01 12:03          (null)
4
  • Is this oracle, or MySQL? These are 2 different database engines. Commented Aug 2, 2018 at 21:05
  • Are there cases when TRANSACTIONS contains data for only one of 2 actions, for any specific ORDER value? If so, which one would that be? How would you expect the result to look like then? Would it be an empty result (rows omitted), or just a NULL value in the appropriate column? Commented Aug 2, 2018 at 22:26
  • This is for Oracle. Commented Aug 3, 2018 at 2:25
  • If an order is missing a case then it would show null value. Commented Aug 3, 2018 at 2:27

2 Answers 2

3

Use aggregation:

SELECT "ORDER", --,"ACTION",ship_complete_time as date_time_created
       MAX(CASE WHEN "ACTION" = 'SHIP' THEN ship_complete_time END) AS SHIP_COM_TIME,
       MAX(CASE WHEN "ACTION" = 'PICK' THEN ship_complete_time END) AS pick_time  
FROM TRANSATIONS
WHERE "ORDER" = 'D8ZH4RJSB-689'
GROUP BY "ORDER";
Sign up to request clarification or add additional context in comments.

Comments

0

If you know that there will be a row for picking and shipping you can do the following:

SELECT P.ORDER "ORDER", P.DATE_TIME_CREATED "PICK_DATE_TIME_CREATED", S.DATE_TIME_CREATED "SHIP_DATE_TIME_CREATED"
  FROM TRANSACTIONS P, TRANSACTIONS S
 WHERE P.ORDER = S.ORDER
   AND P.ACTION = 'PICK'
   AND S.ACTION = 'SHIP'
   AND P.ORDER = 'D8ZH4RJSB-689';

Use outer joins if you can't be sure that both rows will exist for a given order. For performance you might also want to add an index on the columns ORDER, ACTION.

1 Comment

And you got empty rowset.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.