0
我有我从昨天起与雇员在各种条件下试图与员工的一些33记录如下查询:查询SQL Server中使用CTE获得总和,并加入
With CTE
(
select EmployeeId, and other colums with joins and conditions.
)
现在我想加盟此查询以从下表table1和table2中获得每个员工的发票总和。
table1
有employeeid
使我的CTE有employeeid
我可以用表1
With CTE
(
select EmployeeId, and other colums with joins and conditions.
)
select *, table1.invoiceId
from CTE
left join table1 on table1.employeeid = CTE.employeeId
left join table2 on table2.invoiceid = table1.invoiceid
groupby
加入它,但我table1的只有发票和为每个这样的发票有量在其他表即表2花。 table2有列“金额”,我需要根据invoiceid来总结。
为了更加清晰,我正在编写表格结构或输出如下。 我试图像上面,但他们都没有表现出正确的结果
假设CTE具有
Emplyeeid empName Empaddress empcode
1 john America 121
2 sandy America 122
现在table1的具有
InvoiceId EmployeeId RecordId PAyeeid
1 1 223 202
2 1 222 212
3 1 121 378
4 2 229 987
5 2 345 333
表2具有我们需要的epmloyee的每张发票的coulmm量
现在的表2
InvLine Invoiceid Amount
1 1 30
2 1 30
3 1 20
4 2 10
5 2 10
6 2 10
输出应该按雇工约翰在表1,即两份发票ID为1和2,1个2 invoiceds有需要被合计达
Emplyeeid empName Empaddress empcode Amount
1 john America 121 80