简体   繁体   English

SQL Server:有关数据返回的问题

[英]SQL Server: Issue on Data Return

As a novice SQL Server user, I ran into a problem that I did not know a solution to. 作为SQL Server的新手用户,我遇到了一个我不知道解决方案的问题。 I am trying to draw a master list of items and their cost. 我正在尝试绘制项目及其成本的主列表。 Some items have component items with their own cost, and those need to be called as well. 有些项目包含带有其自身成本的组件项目,因此也需要调用它们。 However, whenever I try to include the component items, the query does not return items without components. 但是,每当我尝试包括组件项时,查询都不会返回没有组件的项。

For example: I call 3 items from the query: A, B, and C. It displays their costs perfectly. 例如:我从查询中调用3个项目:A,B和C。它完美地显示了它们的成本。 However, I learn that Item C has a Component Item X. When I try to add a column for component item and it's cost, Items A and B are not returned in the query as they do not have a component item. 但是,我了解到Item C具有一个组件项X。当我尝试为组件项添加列及其成本时,由于查询A和B没有组件项,因此它们不会在查询中返回。 I can't figure out why a NULL isn't placed when an item does not have a component item to accompany it. 我无法弄清楚为什么一个项目没有组件项目时却没有放置NULL。

Here is my query: 这是我的查询:

SELECT 
    T1.item_no, T2.item_cost, T1.component_item_no, T2.item_cost(2)
FROM 
    T1 
INNER JOIN 
    T2 ON T2.item_no = T1.item_no
INNER JOIN 
    T2(2) ON T2.item_no = T1.component_item_no  

I had to call T2 twice so I could call item cost for both the top level item as well as it's component. 我不得不打电话给T2两次,这样我就可以为顶级物料及其组件拨打物料成本。

Sample return data: 样本返回数据:

在此处输入图片说明

将组件项目表的INNER JOIN更改为LEFT JOIN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM