ods_to_dwd.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/bash
  2. . /usr/local/service/sagaApps/bi_saga/dwh_saga/task_day/config.sh
  3. if [ -n "$2" ] ;then
  4. do_date=$2
  5. else
  6. echo "====没有输入数据的日期,取当前时间的前一天===="
  7. do_date=$(date -d yesterday +"%Y-%m-%d")
  8. fi
  9. echo "$do_date"
  10. env_config="
  11. use saga_dw;
  12. SET mapreduce.job.queuename=default;
  13. "
  14. dwd_energy_15_min_hour="
  15. insert overwrite table saga_dw.dwd_energy_15_min_hour partition (dt = '$do_date')
  16. select building,
  17. func_id,
  18. parent_code,
  19. branch_type,
  20. branch_type_name,
  21. use_range_type,
  22. cast(value_sum as decimal(30, 15)) as value_sum ,
  23. hour_ as current_hour,
  24. current_year,
  25. year_mouth,
  26. week_of_year
  27. from (
  28. select building,
  29. func_id,
  30. parent_code,
  31. branch_type,
  32. branch_type_name,
  33. use_range_type,
  34. dt,
  35. t1.hour_,
  36. (ap_sum - al_sum) value_sum
  37. from (
  38. select building,
  39. func_id,
  40. 'AP1' parent_code,
  41. 'ALU' as branch_type,
  42. '其他' as branch_type_name,
  43. 'OTHER' use_range_type,
  44. sum(if((branch_type == 'AP' and use_range_type == '1'), data_value, 0)) ap_sum,
  45. sum(if((branch_type == 'AL' and use_range_type in (2, 3, 4, 6, 7)), data_value, 0)) al_sum,
  46. dt,
  47. o15mh.hour_
  48. from (
  49. select building,
  50. func_id,
  51. meter,
  52. data_time,
  53. substr(data_time, 12, 2) hour_,
  54. data_value,
  55. dt
  56. from ods_energy_15_min oe15m
  57. where dt = '$do_date'
  58. ) o15mh
  59. left join dim_office_meter dom on o15mh.meter = dom.meter
  60. where dt = '$do_date' and branch_type in ('AP', 'AL')
  61. group by building, func_id, dt, o15mh.hour_ ) t1
  62. union
  63. select building,
  64. func_id,
  65. parent_code,
  66. branch_type,
  67. branch_type_name,
  68. use_range_type,
  69. dt,
  70. o15mh.hour_,
  71. sum(data_value) value_sum
  72. from (
  73. select building,
  74. func_id,
  75. meter,
  76. data_time,
  77. substr(data_time, 12, 2) hour_,
  78. data_value,
  79. dt
  80. from ods_energy_15_min oe15m
  81. where dt = '$do_date'
  82. ) o15mh
  83. left join dim_office_meter dom2 on o15mh.meter = dom2.meter
  84. group by building, func_id, parent_code, branch_type, branch_type_name, use_range_type, dt, o15mh.hour_
  85. ) teh
  86. left join dim_date_day ddd on dt = date_id
  87. order by current_hour;"
  88. insert_into_table(){
  89. case $1 in
  90. "all")
  91. hive -e "$env_config$dwd_energy_15_min_hour"
  92. ;;
  93. esac
  94. }
  95. insert_into_table "$1"