sql - Retrieving values from multiple tables -


i working on school project , i'm stuck.

the requirement write single query gathers data 9 different tables , returns result.

it needs client names, contract names, project names, employees logged hours april, total hours logged employee, employee rate, charges per employee projects, , contact names each contract.
(i know, that's lot of data pull.)

the tables are:

 clients    ------------------------------- clientid | commonname     contracttypes    --------------------------------------   contracttypeid | contracttype      contracts    ---------------------------------------------------------   contractid | clientid (fk) | contracttype (fk)      projects    ---------------------------------------------------   projectid | projectname | contractid (fk)      contacts    -----------------------------------------   contactid | firstname | lastname     contractcontacts    --------------------------------------------------   contractid (pk/fk) | contactid (pk/fk)      employees    -----------------------------------   empid | name | titleid | level      billingrates    ----------------------   titleid | level | rate     workhours    ----------------------------   empid | hoursworked   

i ran following, resulted on 23,000 rows - not needed:

select     clients.commonname, projects.projectname, employees.firstname,      employees.lastname, sum(workhours.hoursworked) totalhours,      billingrates.rate,contacts.firstname, contacts.lastname,         contacts.addrline1, contacts.addrline2, contacts.city,     contacts.state_province, contacts.zip     employees, billingrates, clients, projects, workhours, contracts, contacts group     clients.commonname, projects.projectname, employees.firstname,      employees.lastname, billingrates.rate, contacts.firstname,     contacts.lastname, contacts.addrline1, contacts.addrline2,     contacts.city, contacts.state_province, contacts.zip 

i have no idea how pull in data 1 query generate 1 report.

i received on generating query return employees logged hours april, total hours logged employee, , charges per employee projects in following:

select     employees.empid,     sum(workhours.hoursworked) 'totalhours',    firstname, lastname,     isnull(sum(workhours.hoursworked), 0) * isnull(min(rate), 0) 'totalrate'     employees  inner join     workhours on employees.empid = workhours.empid left join      billingrates on employees.titleid = billingrates.titleid                 , employees.level = billingrates.level     wh_month = 4 group     lastname, firstname, employees.empid 

i assume query nested outer query, don't know how construct it.

would able assist writing query?

you can add many joins need;

from     contracts     inner join contractcontacts on contracts.contractid = contractcontacts.contractid     inner join contracttypes on contracts.contracttype = contracttypes.contracttype     inner join contacts on contactcontacts.contactid = contacts.contactid     inner join clients on contracts.clientid = clients.clientid     ... 

just add tables distinct relation have. , check if needs left outer join instead of inner join example, meaning right-side table might not have rows , appear null instead of suppressing whole row.


Comments