简体   繁体   中英

Oracle Query to get two values from single table with different criteria

I have this below table. I am trying to get the information as follow:

1st Criteria: Get all Ticket details except TAG information from the table When the first instance of Telephone occurred.

2nd Criteria: Get the latest recent tag information into the Tag field along with 1st criteria.

Here is the sample data:
    +----------+-----------+--------------+--------------+-------------------+----------------+-------------------+
    | TICKETID | SUBTICKET |  DEPARTMENT  | CUSTOMER REP | COMMUNICATIONTYPE |      TAG       | COMMUNICATIONDATE |
    +----------+-----------+--------------+--------------+-------------------+----------------+-------------------+
    | A112421  |       323 | FOOD PROCESS | ANDY         | NOTES             | SATISFIED      | 5/21/18 9:30 AM   |
    | A112421  |       324 | FOOD PROCESS | ANDY         | NOTES             | AVERAGE        | 5/22/18 7:10 AM   |
    | A112421  |       325 | FOOD PROCESS | ANDY         | TELEPHONE         | AVERAGE        | 5/24/18 4:10 PM   |
    | A112421  |       326 | KITCHEN      | ALEX         | NOTES             | SATISFIED      | 5/25/18 6:10 AM   |
    | A112421  |       327 | KITCHEN      | ALEX         | EMAIL             | NOT SATISFIED  | 5/25/18 7:15 AM   |
    | A112421  |       328 | KITCHEN      | SAM          | EMAIL             | AVERAGE        | 5/26/18 5:25 PM   |
    | A112421  |       329 | KITCHEN      | SAM          | TELEPHONE         | ABOVE AVERAGE  | 5/26/18 5:45 PM   |
    | A112421  |       330 | TRANSPORT    | RAHUL        | NOTES             | VERY SATISFIED | 5/27/18 6:25 AM   |
    +----------+-----------+--------------+--------------+-------------------+----------------+-------------------+

The results I am looking for as follows:

    +----------+-----------+--------------+--------------+-------------------+----------------+--------------------+
    | TICKETID | SUBTICKET |  DEPARTMENT  | CUSTOMER REP | COMMUNICATIONTYPE |      TAG       | COMMUNICATION DATE |
    +----------+-----------+--------------+--------------+-------------------+----------------+--------------------+
    | A112421  |       325 | FOOD PROCESS | ANDY         | TELEPHONE         | VERY SATISFIED | 5/24/18 4:10 PM    |
    +----------+-----------+--------------+--------------+-------------------+----------------+--------------------+

any help is much appreciated.

Thank you in advance.

Try this? I'm using SQL

    DECLARE @FIRSTCOMMDATE DATETIME =(SELECT MIN (communicationdate)  from table) 

DECLARE @MOSTRECENTCOMMDATE DATETIME =(SELECT MAX (communicationdate)  from table) 
;WITH MOSTRECENTCOMMDATE AS

(
     SELECT  TICKETID, TAG, COMMDATE, SUBTICKET 
    FROM TABLE_NAME (NOLOCK) 
    WHERE communicationdate =@MOSTRECENTCOMMDATE

) 


     SELECT  TICKETID, SUBTICKET, DEPARTMENT, CUSTOMER_REP, REC.TAG,COMMUNICATIONTYPE, COMMDATE
    FROM TABLE_NAME  t1 (NOLOCK) 
INNER JOIN MOSTRECENTCOMMDATE rec on rec.ticketid =t1.ticketid and rec.substicket=t1.subticket 
    WHERE communicationdate =@FIRSTCOMMDATE

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