i have written a query which returns a table showing monthly total working hours of each person in the company. so the result is:
name*****jan************feb**********march ......... dec
Tom 170:24:31 186:27:09 140:00:00........158:17:56
i need a query which gives the following output:
name***jan***jan_salary****feb***feb_salary***....***dec***dec_salary
the salary of each month is the total working hours of that month multiplied by 150$. how can i do this? below is the query i have written to compute total working hours of each person for each month:
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 aug,
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