我有两张这样的表:
**Invoices:**
InvoiceId Type Number EmployeeId
1          A   100    -1
2          B   200    11
3          A   300    -1
4          B   400    13
**Deliveries:**
DeliveryId InvoiceId EmployeeId
1          1         10
2          2         500
3          2         501
4          3         12
5          4         502
6          4         503
发票与 1 个或多个交货相关联。员工是签署交货或发票的人。
我的数据中的逻辑是:如果只有一个交货加入到发票中,我们从交货中读取员工,这是 A 类。如果有更多交货,我们从发票中读取员工,这是 B 类
我想得到:
InvoiceId Number TrueEmployee
1         100    10
2         200    11
3         300    12
4         400    13
或最终
InvoiceId Number InvoiceEmployee DeliveryEmployee
1         100    whatever        10
2         200    11              whatever
3         300    whatever        12
4         400    13              whatever
我试过了
Select InvoiceId,Number,Invoices.EmployeeId,Deliveries.EmployeeId
from Invoices inner join Deliveries
on Invoices.InvoiceId=Deliveries.InvoiceId
但如果有超过 1 个交货连接到它,它将为每张发票返回多行
有任何想法吗?如果这很重要,我正在使用 Ms SQL Server。