如何使用我创建的视图来获取不同的数据输出。我创建了视图VJobCustomerLaborCost。该视图为每个客户提供每项工作的总工资成本。我需要为每位客户提供每位客户的总人工成本。我已经给出了视图和示例输出。
GO
CREATE VIEW VJobCustomerLaborCost
AS
SELECT DISTINCT
TJ.intJobID
,TJ.strJobDescription
,TC.intCustomerID
,TC.strLastName + ', ' + TC.strFirstName AS strCustomerName
,SUM(TJE.intHoursWorked * TE.monHourlyRate) AS monTotalLaborCost
FROM
TJobs AS TJ
,TJobCustomers AS TJC
,TCustomers AS TC
,TJobEmployees AS TJE
,TEmployees AS TE
WHERE
TJ.intJobID = TJC.intJobID
AND TJC.intCustomerID = TC.intCustomerID
AND TJ.intCustomerID = TJC.intCustomerID
AND TJE.intJobID = TJ.intJobID
GROUP BY
TJ.intJobID
,TJ.strJobDescription
,TC.intCustomerID
,TC.strLastName
,TC.strFirstName
GO
这是我视图的输出。这给了我所有客户和每个工作总劳动力成本
intJobID strJobDescription intCustomerID strCustomerName monTotalLaborCost
----------- ------------------------- ------------------- ----------------- ---------------------
1 Kitchen Remodel 1 Belcher, Bob 8740.00
8 Basement Remodel 1 Belcher, Bob 13300.00
9 Bathroom Remodel 1 Belcher, Bob 12065.00
10 Roof Replacement 1 Belcher, Bob 3325.00
11 Living Room Remodel 1 Belcher, Bob 0.00
3 Bedroom Remodel 3 Parker, Peter 3800.00
6 Roof Replacement 3 Parker, Peter 0.00
7 Basement Remodel 3 Parker, Peter 1710.00
4 Bedroom Remodel 4 Solo, Hans 2850.00
5 Basement Remodel 2 Stark, Tony 0.00
我需要的是使用创建的视图来获得每个客户的总人工成本。所以输出将是
intCustomerID strCustomerName monTotalLaborCost
------------------- ----------------- ---------------------
1 Belcher, Bob 37,430
3 Parker, Peter 5,510
4 Solo, Hans 2,850
2 Stark, Tony 0.00
如何使用上述视图获取此输出?
答案 0 :(得分:2)
看起来您只需要对数据进行分组和汇总:
select
intCustomerID,
strCustomerName,
sum(monTotalLaborCost) as monTotalLaborCost
from VJobCustomerLaborCost
group by intCustomerID, strCustomerName