我尝试了以下代码
SELECT
a.sodtl_order_no 'Document_Number',
a.ATTDESC 'Attribute_Desc'
,b.Customer_name 'Cust_Name'
,b.Address_Line1 'Add_Line1'
,b.City 'City'
,b.State 'State'
,b.Zip_Code 'Zipcode'
,b.Item_Description 'Itemdescription'
,b.Document_Date
,convert(int,c.sohdr_total_value)'Total_Amount'
from QBE0017_MaintainSaleOrder_QualityAttributes_VW a INNER JOIN
QBE0022_SALEORDER_VW b ON a.sodtl_order_no = b.Document_number AND
a.sodtl_line_no=b.line_no
INNER JOIN QBE0017_MaintainSaleOrder_Hdr_VW c ON
b.Document_Number=c.sohdr_order_no
where b.LINE_NO IN(select top 10 d.sodtl_line_no from QBE0017_MaintainSaleOrder_Dtl_VW d INNER JOIN QBE0017_SO_TCD_VW e ON d.sodtl_line_no=e.[LINENO]
)
我使用了内部查询,我需要显示d
表中的一些列和e
表中的一些列,即我应该在哪里提到需要显示的列名?
答案 0 :(得分:1)
SELECT
a.sodtl_order_no 'Document_Number',
a.ATTDESC 'Attribute_Desc'
,b.Customer_name 'Cust_Name'
,b.Address_Line1 'Add_Line1'
,b.City 'City'
,b.State 'State'
,b.Zip_Code 'Zipcode'
,b.Item_Description 'Itemdescription'
,b.Document_Date
,convert(int,c.sohdr_total_value)'Total_Amount'
,f.columnOne
,f.columnTwo
from QBE0017_MaintainSaleOrder_QualityAttributes_VW a INNER JOIN
QBE0022_SALEORDER_VW b ON a.sodtl_order_no = b.Document_number AND
a.sodtl_line_no=b.line_no
INNER JOIN QBE0017_MaintainSaleOrder_Hdr_VW c ON
b.Document_Number=c.sohdr_order_no
INNER JOIN (select top 10 d.sodtl_line_no, d.columnOne, e.columnTwo from QBE0017_MaintainSaleOrder_Dtl_VW d INNER JOIN QBE0017_SO_TCD_VW e ON d.sodtl_line_no=e.[LINENO]
) f ON b.LINE_NO = f.sodtl_line_no
只要您从子选择中选择列,就可以在您加入的外部选择中使用它们。
注意:如果您正在使用TOP子句,您可能也应该使用ORDER BY子句来保证获得结果的顺序,因为如果您没有明确告诉它什么,SQL可以决定任何旧订单你想要哪个会导致不一致的结果。
答案 1 :(得分:0)
SELECT
a.sodtl_order_no AS Document_Number,
a.ATTDESC AS Attribute_Desc
,b.Customer_name AS Cust_Name
,b.Address_Line1 AS Add_Line1
,b.City AS City
,b.State AS State
,b.Zip_Code AS Zipcode
,b.Item_Description AS Itemdescription
,b.Document_Date
,convert(int,c.sohdr_total_value) AS Total_Amount
from QBE0017_MaintainSaleOrder_QualityAttributes_VW a INNER JOIN
QBE0022_SALEORDER_VW b ON a.sodtl_order_no = b.Document_number AND
a.sodtl_line_no=b.line_no
INNER JOIN QBE0017_MaintainSaleOrder_Hdr_VW c ON
b.Document_Number=c.sohdr_order_no
where b.LINE_NO IN(select top 10 d.sodtl_line_no from QBE0017_MaintainSaleOrder_Dtl_VW d INNER JOIN QBE0017_SO_TCD_VW e ON d.sodtl_line_no=e.[LINENO])
答案 2 :(得分:0)
您可以在WHERE
:
where b.LINE_NO IN(select top 10 d.sodtl_line_no from QBE0017_MaintainSaleOrder_Dtl_VW d INNER JOIN QBE0017_SO_TCD_VW e ON d.sodtl_line_no=e.[LINENO]
)
有两个额外的内连接,如下所示:
SELECT a.sodtl_order_no AS Document_Number
,a.ATTDESC AS Attribute_Desc
,b.Customer_name AS Cust_Name
,b.Address_Line1 AS Add_Line1
,b.City AS City
,b.State AS [State]
,b.Zip_Code AS Zipcode
,b.Item_Description AS Itemdescription
,b.Document_Date
,CONVERT(INT,c.sohdr_total_value) AS Total_Amount
,d.YYY -- values from d
,e.ZZZ -- values from e
FROM QBE0017_MaintainSaleOrder_QualityAttributes_VW a
INNER JOIN QBE0022_SALEORDER_VW b ON a.sodtl_order_no = b.Document_number AND a.sodtl_line_no = b.line_no
INNER JOIN QBE0017_MaintainSaleOrder_Hdr_VW c ON b.Document_Number = c.sohdr_order_no
INNER JOIN QBE0017_MaintainSaleOrder_Dtl_VW d ON d.sodtl_line_no = b.LINE_NO
INNER JOIN QBE0017_SO_TCD_VW e ON d.sodtl_line_no = e.[LINENO];
您唯一丢失的是子选择TOP 10
过滤器。我想你可以在某种程度上把它放在你的查询中,但我怀疑你是否真的想要那个。它以一种奇怪的方式过滤你的数据,说“给我10个随机值,我不介意它们是什么,我将从现在开始使用这些数据”。你可能会想到一些更好的过滤器: - )