Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. unit | days | stays | income | charges | mgmt fee | bal
  2. 1001 20 6 775.00 1500.00 310.00 0.00
  3. 1002 40 14 5000.00 200.00 2100.00 2700.00
  4. 1003 50 20 6000.00 10.00 2500.00 3490.00
  5.  
  6. Unit
  7. id | name | mgmt_fee
  8. 1001 blossom 30
  9. 1002 charlie 25
  10. 1003 deniro 30
  11. 1004 archie 20
  12. 1005 lilly 25
  13.  
  14. Reservations
  15. id | unit | arrival | depart | total_price
  16. 10111 1001 2014-02-09 2014-02-13 400.00
  17. 10012 1001 2014-03-10 2014-03-15 300.00
  18. 10145 1002 2014-04-01 2014-04-05 600.00
  19. 10043 1003 2014-05-30 2014-06-03 350.00
  20.  
  21. Charges
  22. id | unit | amount | charged_dtm
  23. 1 1001 40.00 2014-03-24 19:04:31
  24. 2 1001 30.00 2014-03-25 20:51:08
  25. 3 1002 100.00 2014-04-05 12:52:25
  26.  
  27. SELECT u.name AS unit,
  28. (r.departure-r.arrival) AS days,
  29. COUNT(r.id) AS stays,
  30. SUM(r.total_price) AS income,
  31. SUM(c.amount) AS charges,
  32. (SUM(r.total_price)*(.01*u.mgmt_fee)) AS management,
  33. ((SUM(r.total_price)-(SUM(r.total_price)*
  34. (.01*u.mgmt_fee)))-SUM(c.amount)) AS bal
  35. FROM reservations r
  36. JOIN units u ON r.unit = u.id
  37. JOIN charges c ON u.id = c.unit
  38. GROUP BY u.id
  39.  
  40. SELECT CONCAT(u.unit_name,', "',u.unit_nickname,'"') AS unit,
  41. SUM(r.departure-r.arrival) AS days,
  42. COUNT(r.id) AS stays,
  43. SUM(r.total_price) AS income
  44. FROM reservations r
  45. JOIN units u ON r.unit = u.id
  46. GROUP BY u.id
  47.  
  48. select unit, name, stays, days, total_price, charges, mgmt_fee,
  49. (total_price*.01*mgmt_fee) AS management,
  50. (total_price-(total_price*.01*mgmt_fee)-charges) AS bal
  51. from (
  52. select
  53. x.unit,
  54. name,
  55. count(*) as stays,
  56. sum(days) as days,
  57. sum(total_price) as total_price,
  58. charges,
  59. mgmt_fee
  60. from
  61. (select
  62. unit ,
  63. datediff(depart,arrival) as days,
  64. total_price
  65. from
  66. Reservations
  67. ) as x
  68. join (
  69. select unit, sum(amount) as charges
  70. from Charges
  71. group by unit
  72. ) as y
  73. on x.unit = y.unit
  74. join Unit as u
  75. on u.id = x.unit
  76. group by unit
  77. ) as inner_result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement