PostgreSQL count entries from one table for a given id and mutliple that by rates in another table and sum it all up

user2695222

I have two sql tables provided below:

Worked_Days Table            Employee_Info Table
Employee_ID |      Dates     Employee_ID |  Rate  |  Office
------------------------     ------------------------------
          1 | 05/29/2013               1 |  44.95 |      AZ
          1 | 05/30/2013               2 |  32.63 |      AZ
          2 | 05/28/2013               3 |  36.52 |      VA
          3 | 05/30/2013
          3 | 05/31/2013  

I am looking for a PostgreSQL 9.1 query that will take all the AZ office employees and count each of the days they have worked and multiple them by their respective rates and sum up the multipled values for the whole office.

The expected result for the AZ office is 122.53 = 2 Days * 44.95 + 1 * 32.63

The best that I have been able to do is the following which will count all the days worked for a given office but I am at a loss on how to multiply it by the given employee's rate.

SELECT COUNT(Dates) FROM Worked_Days LEFT JOIN Employee_Info ON Worked_Days.Employee_ID = Employee_Info.Employee_ID WHERE Employee_Info.Office = 'AZ';

Any help would be greatly appreciated. Thank you,

Mosty Mostacho

Try this out:

SELECT SUM(ei.Rate) FROM worked_days wd
JOIN employee_info ei ON wd.Employee_ID = ei.Employee_ID
WHERE ei.Office = 'AZ'

This will output the desired number: 122.53

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Selecting SUM+COUNT from one table and COUNT from another in Single query

From Dev

PostgreSQL select all from one table and join count from table relation

From Dev

Can you copy table privileges from one table to another in postgresql?

From Dev

MySQL count of count, use result from one table with another

From Dev

using postgresql, how can I join tables , select all from left table and sum and count from right table?

From Dev

Selecting entries from a table based on another table

From Dev

Joining row from one table with the sum value from another table

From Dev

groupby, sum and count to one table

From Dev

PostgreSQL count entries from one table for a given id and mutliple that by rates in another table and sum it all up

From Dev

Select from one table where id (from another table) exists

From Dev

PostgreSQL: Joining the count of another table

From Dev

MySQL join / sum value from one table colum with a count values from another

From Dev

Count entries from second Table that match id from first Table

From Dev

Retriving the count from one table given a value from another

From Dev

Group by one table and sum from another table in Linq

From Dev

PostgreSQL for each row from one table join all rows from another table

From Dev

Select from one table and count from another

From Dev

Display 2 columns from one table having max count in column 3 and display computed sum of values from another table

From Dev

Select * as well as count/sum from another table

From Dev

List names from one table whose ID matches entries in another table

From Dev

groupby, sum and count to one table

From Dev

How to get all columns from one table and only one column from another table with ID ? - MySql

From Dev

POSTING an ID from one table to another

From Dev

Updating a table with mutliple criteria from another table

From Dev

select from one table, count from another where id is not linked

From Dev

sum up amount of entries in a table in Ruby on Rails

From Dev

Display the details from one table and count from another table

From Dev

Select all entries from one table which has two specific entries in another table

From Dev

MYSQL entries from one table that appear most often in another table

Related Related

  1. 1

    Selecting SUM+COUNT from one table and COUNT from another in Single query

  2. 2

    PostgreSQL select all from one table and join count from table relation

  3. 3

    Can you copy table privileges from one table to another in postgresql?

  4. 4

    MySQL count of count, use result from one table with another

  5. 5

    using postgresql, how can I join tables , select all from left table and sum and count from right table?

  6. 6

    Selecting entries from a table based on another table

  7. 7

    Joining row from one table with the sum value from another table

  8. 8

    groupby, sum and count to one table

  9. 9

    PostgreSQL count entries from one table for a given id and mutliple that by rates in another table and sum it all up

  10. 10

    Select from one table where id (from another table) exists

  11. 11

    PostgreSQL: Joining the count of another table

  12. 12

    MySQL join / sum value from one table colum with a count values from another

  13. 13

    Count entries from second Table that match id from first Table

  14. 14

    Retriving the count from one table given a value from another

  15. 15

    Group by one table and sum from another table in Linq

  16. 16

    PostgreSQL for each row from one table join all rows from another table

  17. 17

    Select from one table and count from another

  18. 18

    Display 2 columns from one table having max count in column 3 and display computed sum of values from another table

  19. 19

    Select * as well as count/sum from another table

  20. 20

    List names from one table whose ID matches entries in another table

  21. 21

    groupby, sum and count to one table

  22. 22

    How to get all columns from one table and only one column from another table with ID ? - MySql

  23. 23

    POSTING an ID from one table to another

  24. 24

    Updating a table with mutliple criteria from another table

  25. 25

    select from one table, count from another where id is not linked

  26. 26

    sum up amount of entries in a table in Ruby on Rails

  27. 27

    Display the details from one table and count from another table

  28. 28

    Select all entries from one table which has two specific entries in another table

  29. 29

    MYSQL entries from one table that appear most often in another table

HotTag

Archive