Twist_Nemo

SQL_EXAM

Jul 10th, 2025
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.45 KB | None | 0 0
  1. USE daraz_sql_exam;
  2. SHOW TABLES;
  3. SELECT * FROM daraz_sql_raw_data;
  4.  
  5. -- Question 01:
  6. -- Write an SQL query to identify if a session is resolved or not?
  7. -- Condition for resolved session:
  8. -- 1. It can't be unsatisfied
  9. -- 2. The last chat can't be a no answer, nor a recommendation & its not clicked.
  10. -- 3. It can not be a click to agent, intention to agent, switch to agent or direct to agent session
  11.  
  12. -- Solution of Question 01:
  13. SELECT * FROM daraz_sql_raw_data
  14. WHERE
  15.     is_session_unsatisfied = 'N'
  16. AND is_session_last_chat_no_answer = 'N'
  17. AND is_session_last_chat_recommend_not_clk = 'N'
  18. AND is_session_click_to_agent = 'N'
  19. AND is_session_intention_to_agent = 'N'
  20. AND is_session_switch_to_agent = 'N'
  21. AND is_session_direct_agent = 'N';
  22.  
  23.  
  24. -- Question 02:
  25. -- B. Write an SQL query that will help you get distinct count of total chats, total buyers, total sessions, total resolved sessions, total direct to agent sessions, total switch to agent sessions & segregate based on tenant.
  26.  
  27. SELECT
  28.   tenant_name_new AS tenant,
  29.  
  30.   -- Unique counts
  31.   COUNT(DISTINCT chat_uuid) AS total_chats,
  32.   COUNT(DISTINCT buyer_xspace_id) AS total_buyers,
  33.   COUNT(DISTINCT session_id) AS total_sessions,
  34.  
  35.   -- Resolved sessions: must follow resolution rules
  36.   COUNT(DISTINCT IF(
  37.     is_session_unsatisfied = 'N' AND
  38.     is_session_last_chat_no_answer = 'N' AND
  39.     is_session_last_chat_recommend_not_clk = 'N' AND
  40.     is_session_click_to_agent = 'N' AND
  41.     is_session_intention_to_agent = 'N' AND
  42.     is_session_switch_to_agent = 'N' AND
  43.     is_session_direct_agent = 'N',
  44.     session_id,
  45.     NULL
  46.   )) AS total_resolved_sessions,
  47.  
  48.   -- Direct to agent sessions
  49.   COUNT(DISTINCT IF(is_session_direct_agent = 'Y', session_id, NULL)) AS total_direct_to_agent_sessions,
  50.  
  51.   -- Switch to agent sessions
  52.   COUNT(DISTINCT IF(is_session_switch_to_agent = 'Y', session_id, NULL)) AS total_switch_to_agent_sessions
  53.  
  54. FROM daraz_sql_raw_data
  55. GROUP BY tenant_name_new
  56. ORDER BY tenant_name_new;
  57.  
  58.  
  59. -- Question 03
  60. -- "C. Write an SQL query that helps you in calculating the channel wise resolution rate & transfer to agent rate provided that formulae are:  
  61. -- 1. resolution rate= total resolved sessions/(total sessions - total direct agent sessions)
  62. -- 2. transfer to agent rate = (total switch to agent sessions + total direct to agent sessions)/total sessions"
  63.  
  64. -- Solution of question 03
  65. SELECT
  66.   channel_name_new AS channel,
  67.  
  68.   -- Resolution Rate (%)
  69.   CONCAT(
  70.     ROUND(
  71.       COUNT(DISTINCT IF(
  72.         is_session_unsatisfied = 'N' AND
  73.         is_session_last_chat_no_answer = 'N' AND
  74.         is_session_last_chat_recommend_not_clk = 'N' AND
  75.         is_session_click_to_agent = 'N' AND
  76.         is_session_intention_to_agent = 'N' AND
  77.         is_session_switch_to_agent = 'N' AND
  78.         is_session_direct_agent = 'N',
  79.         session_id, NULL
  80.       )) * 100.0 /
  81.       NULLIF(COUNT(DISTINCT session_id) -
  82.              COUNT(DISTINCT IF(is_session_direct_agent = 'Y', session_id, NULL)), 0),
  83.       2
  84.     ), '%'
  85.   ) AS resolution_rate_percent,
  86.  
  87.   -- Transfer to Agent Rate (%)
  88.   CONCAT(
  89.     ROUND(
  90.       (
  91.         COUNT(DISTINCT IF(is_session_switch_to_agent = 'Y', session_id, NULL)) +
  92.         COUNT(DISTINCT IF(is_session_direct_agent = 'Y', session_id, NULL))
  93.       ) * 100.0 /
  94.       NULLIF(COUNT(DISTINCT session_id), 0),
  95.       2
  96.     ), '%'
  97.   ) AS transfer_to_agent_rate_percent
  98.  
  99. FROM daraz_sql_raw_data
  100. GROUP BY channel_name_new
  101. ORDER BY channel_name_new;
  102.  
  103.  
  104.  
Advertisement
Add Comment
Please, Sign In to add comment