I would like to create a quick report on the Customer Order History which basically lists:
Order No
Date Added
Date Released
Date Order Conf Printed
Date Delivered
UserId
etc.,
The problem is that there can be many Order Conf Printed rows for the same order no.
I have tried using Order No, MIN(history_no), Message Text, State etc., but would also want the UserId displayed but as different users may have printed the order confirmation then I could still end up with multiple rows being reported for the same order with the message_text ‘Order confirmation printed’.
I did try:
SELECT c1.order_no, c1.date_entered entered_date, c1.userid entered_userid,
c1.hist_state entered_state, c2.date_entered released_date,
c2.userid released_userid, c2.hist_state release_state,
c6.date_entered conf_printed_date, c6.userid conf_printed_userid,
c6.hist_state conf_printed_state,
ABS(to_date(c1.date_entered,'dd/mm/yyyy') - to_date(c6.date_entered,'dd/mm/yyyy')) entered_to_conf_printed_days_taken,
c3.date_entered reserved_date, c3.userid reserved_userid, c3.hist_state reserved_state,
c4.date_entered delivered_date, c4.userid delivered_user, c4.hist_state delivered_state,
ABS(to_date(c2.date_entered,'dd/mm/yyyy') - to_date(c4.date_entered,'dd/mm/yyyy')) delivered_days_taken,
ROUND((((c2.date_entered - c4.date_entered) * 1440)/60),0)*-1 delivered_hours_taken,
c5.date_entered invoiced_date, c5.userid invoiced_user, c5.hist_state invoiced_state,
ABS(to_date(c2.date_entered,'dd/mm/yyyy') - to_date(c5.date_entered,'dd/mm/yyyy')) invoiced_days_taken,
ROUND((((c2.date_entered - c5.date_entered) * 1440)/60),0)*-1 invoiced_hours_taken,
ifsapp.CUSTOMER_ORDER_API.get_objstate(c1.order_no) order_status
FROM ifsapp.CUSTOMER_ORDER_HISTORY c1
LEFT OUTER JOIN ifsapp.CUSTOMER_ORDER_HISTORY c2 ON c1.order_no = c2.order_no AND c2.message_text = 'Released' AND c2.history_no = (SELECT MIN(history_no) FROM ifsapp.CUSTOMER_ORDER_HISTORY h1
WHERE h1.message_text = 'Released' AND h1.order_no = c1.order_no)
LEFT OUTER JOIN ifsapp.CUSTOMER_ORDER_HISTORY c3 ON c1.order_no = c3.order_no AND c3.message_text = 'Reserved' AND c3.history_no = (SELECT MIN(history_no) FROM ifsapp.CUSTOMER_ORDER_HISTORY h2
WHERE h2.message_text = 'Released' AND h2.order_no = c1.order_no)
LEFT OUTER JOIN ifsapp.CUSTOMER_ORDER_HISTORY c4 ON c1.order_no = c4.order_no AND c4.message_text = 'Delivered' AND c4.history_no = (SELECT MIN(history_no) FROM ifsapp.CUSTOMER_ORDER_HISTORY h3
WHERE h3.message_text = 'Released' AND h3.order_no = c1.order_no)
LEFT OUTER JOIN ifsapp.CUSTOMER_ORDER_HISTORY c5 ON c1.order_no = c5.order_no AND c5.message_text = 'Invoiced/Closed' AND c5.history_no = (SELECT MIN(history_no) FROM ifsapp.CUSTOMER_ORDER_HISTORY h4
WHERE h4.message_text = 'Released' AND h4.order_no = c1.order_no)
LEFT OUTER JOIN ifsapp.CUSTOMER_ORDER_HISTORY c6 ON c1.order_no = c6.order_no AND c6.message_text = 'Order confirmation printed' AND c6.history_no = (SELECT MIN(history_no) FROM ifsapp.CUSTOMER_ORDER_HISTORY h5
WHERE h5.message_text = 'Released' AND h5.order_no = c1.order_no)
WHERE c1.message_text = 'Planned'
ORDER BY c1.order_no
This works in PL/SQL but cannot be used in a view/IAL or Quick Report as the following is received:
Can anyone please advise.