简体   繁体   English

有关SQL Server中的联接的查询

[英]query regarding joins in sql server

I tried the following code 我尝试了以下代码

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]
)

I have used inner query and I need to display some columns from d table and some from e table ie where should I mention the column name that needs to be displayed ? 我已经使用了内部查询,并且我需要显示d表中的某些列和e表中的某些列,即,我应该在哪里提到需要显示的列名?

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

As long as you select out the columns from you sub-select you can use them in your outer select that you join on to. 只要您从子选择中选择出列,就可以在要加入的外部选择中使用它们。

Note: If you're using a TOP clause you should probably also be using an ORDER BY clause to guarantee the order in which you get the results as SQL can decide on any old order if you don't explicitly tell it what you want which can lead to inconsistent results. 注意:如果使用的是TOP子句,则可能还应该使用ORDER BY子句来保证获得结果的顺序,因为如果您不明确告诉您想要的内容,SQL可以决定任何旧顺序。可能导致结果不一致。

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])

You could replace this subselection in the WHERE : 您可以在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]
)

with two extra inner joins, like this: 具有两个额外的内部联接,如下所示:

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];

The only thing you're losing then is the subselect TOP 10 filter. 然后,您唯一丢失的是次选择的TOP 10过滤器。 I think you could somehow put this in your query again, but I doubt if you really want that. 我认为您可以以某种方式再次将其放入查询中,但是我怀疑您是否真的想要那样。 It filters your data in a weird way, saying "Give me 10 random values back, I don't mind what they are, and I'll use that data from now on". 它以一种怪异的方式过滤您的数据,并说“给我10个随机值,我不在乎它们是什么,从现在开始我将使用这些数据”。 You can probably think of some better filters to put in :-) 您可能会想到一些更好的过滤器:-)

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

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