как превратить запрос в функцию в postgreSQL - PullRequest
0 голосов
/ 11 января 2019

Я написал запрос в postgreSql, который возвращает таблицу со следующими полями:
Staff_Id int, year int, текст за январь, ....., текст за декабрь.

показывает рабочее время каждого человека в каждом месяце. Мне нужна функция, которая использует этот запрос, но таким образом, что он получает Staff_Id в качестве входных данных и показывает рабочее время только этого человека, а не всего персонала. Как я могу превратить мой запрос в функцию, как я объяснил? вот запрос:

`

with hours as (
  select "Staff_Id", 
         "Date", 
         case 
           when row_number() over w % 2 = 0 then 
              TO_CHAR("Time" - lag("Time") over w,'HH24:MI:SS')
         end as hours
  from "Org"."Clock"
  window w as (partition by "Staff_Id", "Date" order by "Time")
), hours_per_month as (
  select "Staff_Id", 
         extract(year from "Date")::int as work_year,
         extract(month from "Date")::int as work_month,
         sum(hours::interval) work_hours
  from hours
  where hours is not null
  group by "Staff_Id", work_year, work_month
)
select "Staff_Id", 
       work_year,
       sum("work_hours") filter (where work_month = 1) as jan,
       sum("work_hours") filter (where work_month = 2) as feb,
       sum("work_hours") filter (where work_month = 3) as march,
       sum("work_hours") filter (where work_month = 4) as april,
       sum("work_hours") filter (where work_month = 5) as may,
       sum("work_hours") filter (where work_month = 6) as june,
       sum("work_hours") filter (where work_month = 7) as july,
       sum("work_hours") filter (where work_month = 8) as augest,
       sum("work_hours") filter (where work_month = 9) as sep,
       sum("work_hours") filter (where work_month = 10) as oct,
       sum("work_hours") filter (where work_month = 11) as nov,
       sum("work_hours") filter (where work_month = 12) as dec
from hours_per_month  
group by "Staff_Id", work_year

`

1 Ответ

0 голосов
/ 11 января 2019

Я нашел два функциональных формата для него. как сказал Лоренц, мы можем добавить предложение where в первом или, как я сказал, добавить в конце запроса. Вот два решения. первый:

 CREATE OR REPLACE FUNCTION staff_clock(p_id int)
  RETURNS TABLE (
    id   int
  , year   int
  , فروردین  interval, 
        اردیبهشت  interval,
       خرداد  interval,
       تیر  interval,
        مرداد  interval,
       شهریور  interval,
       مهر  interval,
        آبان interval,
        آذر  interval,
        دی  interval,
        بهمن  interval,
        اسفند interval) AS
$func$
BEGIN
   RETURN QUERY
with hours as (
  select "Staff_Id", 
         "Date", 
         case 
           when row_number() over w % 2 = 0 then 
              TO_CHAR("Time" - lag("Time") over w,'HH24:MI:SS')
         end as hours
  from "Org"."Clock"
  window w as (partition by "Staff_Id", "Date" order by "Time")
), hours_per_month as (
  select "Staff_Id", 
         extract(year from "Date")::int as work_year,
         extract(month from "Date")::int as work_month,
         sum(hours::interval) work_hours
  from hours
  where hours is not null
  group by "Staff_Id", work_year, work_month
)
select "Staff_Id", 
       work_year,
       sum("work_hours") filter (where work_month = 1) as فروردین,
       sum("work_hours") filter (where work_month = 2) as اردیبهشت,
       sum("work_hours") filter (where work_month = 3) as خرداد,
       sum("work_hours") filter (where work_month = 4) as تیر,
       sum("work_hours") filter (where work_month = 5) as مرداد,
       sum("work_hours") filter (where work_month = 6) as شهریور,
       sum("work_hours") filter (where work_month = 7) as مهر,
       sum("work_hours") filter (where work_month = 8) as آبان,
       sum("work_hours") filter (where work_month = 9) as آذر,
       sum("work_hours") filter (where work_month = 10) as دی,
       sum("work_hours") filter (where work_month = 11) as بهمن,
       sum("work_hours") filter (where work_month = 12) as اسفند
from hours_per_month  
group by "Staff_Id", work_year
having "Staff_Id"=p_id;

END
$func$  LANGUAGE plpgsql;

select * from staff_clock(2);

второй:

CREATE OR REPLACE FUNCTION staff_clock(p_id int)
  RETURNS TABLE (
    id   int,
    year   int,
    فروردین  interval, 
        اردیبهشت  interval,
       خرداد  interval,
       تیر  interval,
        مرداد  interval,
       شهریور  interval,
       مهر  interval,
        آبان interval,
        آذر  interval,
        دی  interval,
        بهمن  interval,
        اسفند interval) 
AS $$
BEGIN
   RETURN QUERY
with hours as (
  select "Staff_Id", 
         "Date", 
         case 
           when row_number() over w % 2 = 0 then 
              TO_CHAR("Time" - lag("Time") over w,'HH24:MI:SS')
         end as hours
  from "Org"."Clock"
where "Staff_Id" = $1
    window w as (partition by "Staff_Id", "Date" order by "Time")
), hours_per_month as (
  select "Staff_Id", 
         extract(year from "Date")::int as work_year,
         extract(month from "Date")::int as work_month,
         sum(hours::interval) work_hours
  from hours
  where hours is not null
  group by "Staff_Id", work_year, work_month
)
select "Staff_Id", 
       work_year,
       sum("work_hours") filter (where work_month = 1) as فروردین,
       sum("work_hours") filter (where work_month = 2) as اردیبهشت,
       sum("work_hours") filter (where work_month = 3) as خرداد,
       sum("work_hours") filter (where work_month = 4) as تیر,
       sum("work_hours") filter (where work_month = 5) as مرداد,
       sum("work_hours") filter (where work_month = 6) as شهریور,
       sum("work_hours") filter (where work_month = 7) as مهر,
       sum("work_hours") filter (where work_month = 8) as آبان,
       sum("work_hours") filter (where work_month = 9) as آذر,
       sum("work_hours") filter (where work_month = 10) as دی,
       sum("work_hours") filter (where work_month = 11) as بهمن,
       sum("work_hours") filter (where work_month = 12) as اسفند
from hours_per_month  
group by "Staff_Id", work_year;   
END; $$

LANGUAGE 'plpgsql';

select * from staff_clock(3)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...