从多个表中检索值

时间:2015-05-08 04:20:25

标签: sql sql-server join

我正在做一个学校项目而且我被困住了。

要求是编写单个查询,从9个不同的表中收集数据并返回结果。

需要客户名称,合同名称,项目名称,4月份记录小时数的员工,员工记录的总小时数,员工费率,项目每位员工的费用以及每份合同的联系人姓名。
(我知道,需要提取大量数据。)

表格是:

 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  

我运行了以下内容,但结果是超过23,000行 - 不是需要的:

select 
   clients.CommonName, projects.ProjectName, employees.FirstName,  
   employees.lastname, sum(workhours.hoursworked) as TotalHours,  
   BillingRates.Rate,contacts.FirstName, contacts.LastName,     
   contacts.AddrLine1, contacts.AddrLine2, contacts.City, 
   contacts.State_Province, contacts.Zip
from 
   employees, BillingRates, Clients, Projects, WorkHours, Contracts, Contacts
group by 
   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

老实说,我不知道如何将所有数据输入一个查询以生成一个报告。

我收到了有关生成查询的帮助,以便返回4月份记录小时数的员工,员工记录的总小时数以及以下项目的每位员工费用:

select 
   employees.EmpID, 
   sum(workhours.hoursworked) as 'TotalHours',
   firstname, lastname, 
   ISNULL(sum(workhours.hoursworked), 0) * ISNULL(min(rate), 0) AS 'TotalRate'
from 
   employees 
inner join 
   workhours on employees.empid = WorkHours.EmpID
left join  
   BillingRates on employees.titleid = billingrates.titleid
                and employees.level = billingrates.level
where 
   WH_Month = 4
group by 
   LastName, firstname, employees.EmpID

我假设此查询将嵌套到外部查询中,但我不知道如何构造它。

是否有人能够协助撰写此查询?

3 个答案:

答案 0 :(得分:0)

您可以根据需要添加任意数量的联接;

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
    ...

只需添加具有不同关系的所有表格。例如,检查它是否需要保留外连接而不是内连接,这意味着右侧表可能没有任何行,并且您希望这些行显示为NULL而不是抑制整行。

答案 1 :(得分:0)

您可以使用内部联接,但我没有看到FK所有表与另一个表连接。 从    雇员    内连接BillingRates ???    内部联接客户端cls on ???    内连接在cls.clientid = cs.clientid上收缩cs    内部联接项目ps on,ps.contractid = cs.contractid    内部联接WorkHours ws on e.empid = ws.empid enter code here    内部联接ContractContacts cscs on cs.contractid = cscs.contractid    内部联接联系人c在c.contact_id = cscs.contactid

如果您知道答案而不是上面的联接来解决您的问题,您将如何加入员工的计费率和客户?

答案 2 :(得分:0)

非常感谢您的帮助。

我能够提出以下问题:

select clients.CommonName, projects.ProjectName, employees.FirstName,  
   employees.lastname, sum(workhours.hoursworked) as TotalHours,  
   BillingRates.Rate,contacts.FirstName, contacts.LastName,     
   contacts.AddrLine1, contacts.AddrLine2, contacts.City, 
   contacts.State_Province, contacts.Zip
   ISNULL(sum(workhours.hoursworked), 0) * ISNULL(min(rate), 0) AS TotalRate  
from Contracts   
    inner join ContractsContacts on contracts.ContractID =         ContractsContacts.contractid  
    inner join contracttypes on contracts.ContractTypeID =         contracttypes.ContractTypeID  
    inner join contacts on ContractsContacts.ContactID = contacts.ContactID
inner join projects on contracts.ContractID = projects.ContractID
inner join Clients on contracts.ClientID = clients.ClientID
inner join WorkHours on WorkHours.ProjectID = projects.ProjectID
inner join Employees on WorkHours.EmpID = Employees.EmpID
left join BillingRates on Employees.TitleID = BillingRates.TitleID and
Employees.Level = BillingRates.Level  
where workhours.WH_Month = 4  
order by Clients.CommonName, Projects.ProjectName, Employees.LastName  

但我无法弄清楚如何将4月份每名员工的总工作时数加起来,以及将费率乘以查询的倍数。我使用以下查询来获取该信息,但我不知道如何将其添加到更长的查询中:

select 
   employees.EmpID, 
   sum(workhours.hoursworked) as 'TotalHours',
   firstname, lastname, 
   ISNULL(sum(workhours.hoursworked), 0) * ISNULL(min(rate), 0) AS 'TotalRate'
from 
   employees 
inner join 
   workhours on employees.empid = WorkHours.EmpID
left join  
   BillingRates on employees.titleid = billingrates.titleid
            and employees.level = billingrates.level
where 
   WH_Month = 4
group by 
   LastName, firstname, employees.EmpID  

我很感激你的帮助。