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'
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即我应该在哪里提到需要显示的列名?

4

3 回答 3

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 可以决定任何旧顺序可能导致不一致的结果。

于 2013-08-23T07:22:08.133 回答
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])
于 2013-08-23T07:03:31.810 回答
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 个随机值,我不介意它们是什么,从现在开始我将使用这些数据”。您可能会想到一些更好的过滤器来放置:-)

于 2013-08-23T07:20:33.733 回答