widana

query

Dec 13th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Current Sales Revenue Achievement per Salesman
  2. SELECT SUM(A.sales_amount) AS sales_amount, ((SUM(A.sales_amount) / B.target_amount) * 100) AS sales_percentage
  3. FROM dwh_sales A
  4. INNER JOIN m_target_salesman B ON A.salesman_id = B.salesman_id AND B.year_month = '201701'
  5. WHERE A.year_month = '201701'
  6.     AND A.salesman_id = 3596
  7.     AND A.doc_type_id IN (311, 312)
  8. GROUP BY B.target_amount;
  9.  
  10. -- Current Sales Revenue Achievement per Salesman
  11. WITH tt_gorss_profit AS (
  12.     SELECT COALESCE(SUM(gross_profit_amount), 0) AS gross_profit_amount, COALESCE(SUM(A.sales_amount),0 ) AS sales_amount
  13.     FROM dwh_sales A
  14.     WHERE A.year_month = '201701'
  15.         AND A.salesman_id = 3596
  16.         AND A.doc_type_id IN (311, 312))
  17. SELECT sales_amount, gross_profit_amount, ((sales_amount - gross_profit_amount) / sales_amount) AS gross_profit_percentage
  18. FROM tt_gorss_profit;
  19.  
  20. -- History Sales Revenue Achievement per Salesman
  21. 1. SELECT SUM(sales_amount) AS sales_amount
  22. FROM dwh_sales A
  23. WHERE A.salesman_id = 3596
  24.     AND A.year_month IN (
  25.         SELECT Z.year_month
  26.         FROM m_target_salesman Z
  27.         WHERE Z.salesman_id = A.salesman_id
  28.             AND SUBSTRING(Z.year_month, 0, 5) = '2017')
  29. GROUP BY A.year_month
  30.  
  31. 2. SELECT SUM(A.sales_amount) AS sales_amount
  32. FROM dwh_sales A
  33. WHERE A.salesman_id = 3596
  34.     AND SUBSTRING(A.year_month, 0, 5) = '2017'
  35. GROUP BY A.year_month
  36. ORDER BY A.year_month
  37.  
  38.  
  39. -- History Gross Revenue Achievement per Salesman
  40. 1. SELECT SUM(A.gross_profit_amount) AS gross_profit_amount
  41. FROM dwh_sales A
  42. WHERE A.salesman_id = 3596
  43.     AND A.year_month IN (
  44.         SELECT Z.year_month
  45.         FROM m_target_salesman Z
  46.         WHERE Z.salesman_id = A.salesman_id
  47.             AND SUBSTRING(Z.year_month, 0, 5) = '2017')
  48. GROUP BY A.year_month
  49.  
  50. 2. SELECT SUM(A.gross_profit_amount) AS gross_profit_amount
  51. FROM dwh_sales A
  52. WHERE A.salesman_id = 3596
  53.     AND SUBSTRING(A.year_month, 0, 5) = '2017'
  54. GROUP BY A.year_month
  55. ORDER BY A.year_month
Add Comment
Please, Sign In to add comment