Hi All,
Hope you are doing well!... I am trying to get the exact ticket raised for each of the orders in a company which does online sales...The tickets are raised by the customers due to the issues faced by them with regard to the order...One table has the order and the corresponding tickets raised for the order ... Another table has the order and whether the order is a pre-sell -Yes/no..Now I have to follow the following rules to determine the final table containing the order and the exact title..
-
In Table 1 (##input1) I have to ignore the following ticket titles while trying to determine the exact ticket title for the order..(ticket titles that contain the following should be ignored)...Shipping Carrier Package Claim, Item Verification, Internal - Return Credit Queue, Purchase Order Vendor Transfer, Purchase Order High Shipping Rate, Amazon A-To-Z Guarantee Claims, Item Verification -, Chargeback
-
After ignoring the above ticket titles for any order ID I have to take the latest ticket title (most recent date-ts) for every Order ID
-
After that I have to consider the following ticket titles as Backorder (ticket title)
Cancellation - Expected Beyond 30 Days
Cancellation - Extended Backorder
Cancellation - Backorder
Cancellation - Delayed Shipment
Cancellation - Expected Late
Cancellation - Past 30 days
Also other ticket titles that have cancellation - discontinued as discontinued or anything with Order Issues - as just Order Issues ..See mapping below:
Cancellation - Buyer's Remorse Buyer's Remorse
Cancellation - Discontinued Discontinued
Cancellation - Listing Error Listing Error
Cancellation - NOFRAUD verification status fail Fraud
Cancellation - OJ Requested OJ Requested
Order Issues - Order Issues
- After that if the ticket title from ##Input 1 after running through the above steps comes to be either Buyers Remorse or Order Issues then it remains the same as the final ticket title for that order..If the ticket title from ## input1 is other than Buyers remorse or Order Issues then if the presell from ##Input2 table is yes then it should be presell else the ##input1 ticket title remains for the order....Can you please help me here...The input and the output tables below..
##Input1
Create table ##input1
(orderid int,
ticket_title varchar(100),
ts DATETIME2)
insert into ##input1 values
('4539463','Chargeback','2020-07-09 21:20:56:05'),
('4539463','Cancellation - Buyers Remorse','2020-07-03 18:59:02:03'),
('4539463','Order Issues - Damaged - Complete Item','2020-05-27 15:30:05:02'),
('4579758','Chargeback','2020-07-07 20:13:12:06'),
('4579758','Amazon A-To-Z Guarantee Claims','2020-07-06 14:59:04:07'),
('4579758','Order Issues - Defective - Parts','2020-06-11 05:15:02:09'),
('4612474','Chargeback','2020-07-02 20:45:20:05'),
('4612474','Customer Backorder Notification','2020-05-27 05:02:12:04'),
('4620316','Chargeback','2020-07-09 21:14:05:08'),
('4620316','Amazon A-To-Z Guarantee Claims','2020-07-06 22:59:11:07'),
('4650014','Chargeback','2020-07-06 20:23:20:09'),
('4650014','Amazon A-To-Z Guarantee Claims','2020-07-05 14:58:44:08'),
('4650014','Order Issues - Defective - Parts','2020-06-21 16:00:03:07'),
('4663584','Chargeback','2020-07-04 20:15:15:09'),
('4663584','Amazon A-To-Z Guarantee Claims','2020-07-03 14:58:50:08'),
('4663584','Order Issues - Defective - Parts','2020-06-23 12:30:03:07'),
('4665480','Chargeback','2020-07-03 20:04:04:09'),
('4665480','Amazon A-To-Z Guarantee Claims','2020-07-02 14:59:00:08'),
('4665480','Order Issues - Remorse','2020-06-28 21:45:04:07'),
('4665656','Cancellation - Expected Late','2020-07-09 05:05:03:06'),
('4667841','Shipping Carrier Package Claim - 977225711067255','2020-07-09 12:59:35:07'),
('4667841','Order Issues - Damaged','2020-07-08 17:30:03:06'),
('4675796','Cancellation - Extended Backorder','2020-07-09 09:36:58:09'),
('4675796','Shipping Carrier Package Claim - 394025727836','2020-07-08 15:13:26:08'),
('4675796','Shipping Carrier Package Claim - 394025727620','2020-07-08 15:13:26:07'),
('4675796','Shipping Carrier Package Claim - 394025727295','2020-07-08 15:13:26:06'),
('4675796','Shipping Carrier Package Claim - 394025728979','2020-07-08 14:42:41:05'),
('4675796','Order Issues - Not Delivered - Not Received','2020-07-08 13:12:47:04')
## Input2
create table ##input2
(orderid int,
presellyesno varchar(10))
insert into ##input2 values
('4539463','yes '),
('4579758','no'),
('4612474','yes'),
('4620316','yes'),
('4650014','no'),
('4663584','yes'),
('4665480','yes'),
('4665656','yes'),
('4667841','no'),
('4675796','yes')
##Output
Create table ##output
(orderid int,
ticket_title varchar(20)
)
insert into ##output values
('4539463','Buyers Remorse '),
('4579758','Order Issues'),
('4612474','Presell'),
('4650014','Order Issues'),
('4663584','Order Issues'),
('4665480','Order Issues'),
('4665656','Presell'),
('4667841','Order Issues'),
('4675796','Backorder'),
('4675796','Order Issues')
The query that I tried
select o.orderid ,
case when o.ticket_title like '%Buyers Remorse%' then 'Buyers Remorse'
when o.ticket_title Like '%Order Issues%' then 'Order Issues'
when o.ticket_title like '%Shipping Carrier Package Claim%' then rn+1
when o.ticket_title like '%Chargeback%' then rn+1
when o.ticket_title like '%Item Verification%' then rn+1
when o.ticket_title like '%Internal - Return Credit Queue%' then rn+1
when o.ticket_title like '%Purchase Order Vendor Transfer%' then rn+1
when o.ticket_title like '%Purchase Order High Shipping Rate%' then rn+1
when o.ticket_title like '%Amazon A-To-Z Guarantee Claims%' then rn+1
when b.presellyesno='yes' and o.ticket_title not like '%Buyers Remorse%' then 'pre-sell'
when b.presellyesno='yes' and o.ticket_title not like '%Order Issues%' then 'pre-sell'
when o.ticket_title='Cancellation - Discontinued' then 'Discontinued'
when o.ticket_title='Cancellation - Listing Error' then 'Listing Error'
when o.ticket_title='Cancellation - NOFRAUD verification status fail' then 'Fraud'
when o.ticket_title='Cancellation - OJ Requested' then 'OJ Requested'
else o.ticket_title
end as ticket_title
from
(select a.orderid,ticket_title,row_number() over(partition by a.orderid order by ts desc) as rn
from ##input1 a (nolock)
)o
join ##input2 b (nolock)
on o.orderid=b.orderid
Thanks,
Arun