3.class.sql 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. create table if not exists numeric_constants(key varchar primary key, value numeric);
  2. -- Функция формата даты
  3. create or replace function approx_report_date(t_date timestamptz)
  4. returns varchar(5)
  5. language plpgsql
  6. as
  7. $$
  8. begin
  9. return lpad(extract(day from t_date)::text, 2, '0') || lpad(extract(hour from t_date)::text, 2, '0') || floor((extract(minute from t_date) / 10))::varchar;
  10. end;
  11. $$;
  12. -- Функция формата высоты
  13. create or replace function approx_report_height(height numeric)
  14. returns varchar(4)
  15. language plpgsql
  16. as
  17. $$
  18. begin
  19. return lpad(height::text, 4, '0');
  20. end;
  21. $$;
  22. -- Функция формата отклонения по давлению и температуре
  23. create or replace function approx_report_deviation(pressure numeric, temperature numeric)
  24. returns varchar(5)
  25. language plpgsql
  26. as
  27. $$
  28. declare
  29. pressure_dev numeric;
  30. temperature_dev numeric;
  31. res varchar(5);
  32. begin
  33. pressure_dev = (pressure - (select value from numeric_constants where key = 'rel_pressure'));
  34. temperature_dev = (temperature - (select value from numeric_constants where key = 'rel_temperature'));
  35. temperature_dev := round(temperature_dev + calc_adjustment(calc_interpolation(temperature)));
  36. res = case when pressure_dev >= 0 then pressure_dev::text else ('5' || lpad((pressure_dev*-1)::text, 2, '0')) end;
  37. res = concat(res, lpad(temperature_dev::text, 2, '0'));
  38. return res;
  39. end;
  40. $$;
  41. -- Тип приблизительного отчета
  42. CREATE TYPE approx_report AS
  43. (
  44. date character varying(5),
  45. height character varying(4),
  46. deviation character varying(5)
  47. );
  48. -- Функция получения приблизительного отчета
  49. create or replace function approx_report(date timestamptz, height numeric, pressure numeric, temperature numeric)
  50. returns approx_report
  51. language plpgsql
  52. as
  53. $$
  54. begin
  55. return (approx_report_date(date)::varchar(5), approx_report_height(height)::varchar(4), approx_report_deviation(pressure, temperature)::varchar(5)) as approx_report;
  56. end;
  57. $$;
  58. insert into numeric_constants(key, value) values ('rel_pressure', 750);
  59. insert into numeric_constants(key, value) values ('rel_temperature', 15.9);