SQL 连接三个具有相同外键的 table 不合并 table 2 和 3 数据
SQL Joining three tables with same foreign key not combining table 2 and 3 data
我想要完成的是连接三个具有相同外键的 table。但我想显示 table 2 和 table 3 的详细信息,而不将它们组合成一行。您可以在下面看到返回数据的屏幕截图。 InvoiceAmount 多次打印相同的金额。 "Name" 来自第一个 table,它也是其他两个 table 中的外键(CC1)。 "InvoiceAmount"和"Description"来自第二个table,"NetAmount"来自第三个Table。
如果 "InvoiceAmount" 和 "Description" 有一个值,那么 "NetAmount" 将为 null
如果 "NetAmount" 有值,则 "InvoiceAmount" 和 "Description" 将为空
下面是我希望如何在 select 中打印数据(如果可能的话)。我不知道我是否必须创建一个视图,或者是否可以通过 Select 查询来创建视图,或者是否有可能。
Name InvoiceAmount Description NetAmount
19-SHOP -10041.98 BULKITEMTRANSFER NULL
19-SHOP NULL NULL 84.840625
19-SHOP NULL NULL 109.8613
19-SHOP NULL NULL 110.1601
19-SHOP NULL NULL 154.5323
19-SHOP NULL NULL 187.5742
下面附上的是我现在拥有的 SQL,它完全按照我的指示进行操作,但我就是想不通,也找不到任何地方如何打印数据就像我上面展示的那样。非常感谢任何帮助!
SELECT j.Name, a.InvoiceAmount, a.Description, p.NetAmount FROM Job AS j
LEFT OUTER JOIN
APInvoiceBatchDetail AS a
ON a.CC1 = j.Name
LEFT OUTER JOIN
PPEReport AS p
ON p.CC1 = j.Name
下面是第一个 table 'Job'
的屏幕截图
下面是第二个的屏幕截图 table 'APInvoiceBatchDetail'
下面是第三个的屏幕截图 table 'PPERecord'
SELECT j.Name, a.InvoiceAmount, a.Description, null as NetAmount
FROM Job AS j
JOIN APInvoiceBatchDetail AS a ON a.CC1 = j.Name
union all
SELECT j.Name, null as InvoiceAmount, null as Description, p.NetAmount
FROM Job AS j
JOIN PPEReport AS p ON p.CC1 = j.Name
如果您需要订单,您可以这样做:
SELECT Name, InvoiceAmount, Description, NetAmount
FROM (
SELECT j.Name, a.InvoiceAmount, a.Description, null as NetAmount,
1 as ord
FROM Job AS j
JOIN APInvoiceBatchDetail AS a ON a.CC1 = j.Name
union all
SELECT j.Name, null as InvoiceAmount, null as Description, p.NetAmount,
2 as ord
FROM Job AS j
JOIN PPEReport AS p ON p.CC1 = j.Name
) X
ORDER BY X.ORD, X.Name
我想要完成的是连接三个具有相同外键的 table。但我想显示 table 2 和 table 3 的详细信息,而不将它们组合成一行。您可以在下面看到返回数据的屏幕截图。 InvoiceAmount 多次打印相同的金额。 "Name" 来自第一个 table,它也是其他两个 table 中的外键(CC1)。 "InvoiceAmount"和"Description"来自第二个table,"NetAmount"来自第三个Table。
如果 "InvoiceAmount" 和 "Description" 有一个值,那么 "NetAmount" 将为 null
如果 "NetAmount" 有值,则 "InvoiceAmount" 和 "Description" 将为空
下面是我希望如何在 select 中打印数据(如果可能的话)。我不知道我是否必须创建一个视图,或者是否可以通过 Select 查询来创建视图,或者是否有可能。
Name InvoiceAmount Description NetAmount
19-SHOP -10041.98 BULKITEMTRANSFER NULL
19-SHOP NULL NULL 84.840625
19-SHOP NULL NULL 109.8613
19-SHOP NULL NULL 110.1601
19-SHOP NULL NULL 154.5323
19-SHOP NULL NULL 187.5742
下面附上的是我现在拥有的 SQL,它完全按照我的指示进行操作,但我就是想不通,也找不到任何地方如何打印数据就像我上面展示的那样。非常感谢任何帮助!
SELECT j.Name, a.InvoiceAmount, a.Description, p.NetAmount FROM Job AS j
LEFT OUTER JOIN
APInvoiceBatchDetail AS a
ON a.CC1 = j.Name
LEFT OUTER JOIN
PPEReport AS p
ON p.CC1 = j.Name
下面是第一个 table 'Job'
的屏幕截图下面是第二个的屏幕截图 table 'APInvoiceBatchDetail'
下面是第三个的屏幕截图 table 'PPERecord'
SELECT j.Name, a.InvoiceAmount, a.Description, null as NetAmount
FROM Job AS j
JOIN APInvoiceBatchDetail AS a ON a.CC1 = j.Name
union all
SELECT j.Name, null as InvoiceAmount, null as Description, p.NetAmount
FROM Job AS j
JOIN PPEReport AS p ON p.CC1 = j.Name
如果您需要订单,您可以这样做:
SELECT Name, InvoiceAmount, Description, NetAmount
FROM (
SELECT j.Name, a.InvoiceAmount, a.Description, null as NetAmount,
1 as ord
FROM Job AS j
JOIN APInvoiceBatchDetail AS a ON a.CC1 = j.Name
union all
SELECT j.Name, null as InvoiceAmount, null as Description, p.NetAmount,
2 as ord
FROM Job AS j
JOIN PPEReport AS p ON p.CC1 = j.Name
) X
ORDER BY X.ORD, X.Name