Skip to content
Advertisement

showing columns in a particular pattern in postgresql

i have written a query which returns a table showing monthly total working hours of each person in the company. so the result is:

i need a query which gives the following output:

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:

Advertisement

Answer

I would think you can just add the salary into your “hours_per_month” subquery as follows:

This presupposes that you want to pay whole hours (15.8 hours = 15 hours of pay), but if not, that’s easy enough to tweak.

extract (hours might work also, but not if the number of total hours is > 24. In my opinion, it’s not worth it to assume it will, even in a case like this where it would be unrealistic to assume a human would work in excess of 24 hours at one time. Also, you could unwittingly later clone this code to work for something like “machine hours,” in which case it would be very possible.

Then, in your main query:

Totally off-topic, and I’m not trying to tell you how to live your life, but when I see quotes around field and table names, unelss it’s at the very last step of the query (rendering output) it makes my skin crawl. It also introduces so many chances for errors. As it happens, I’m not alone in my opinion:

https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_upper_case_table_or_column_names

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement