Sql server SQL语法选择和子选择并联接

Sql server SQL语法选择和子选择并联接,sql-server,syntax,left-join,where-clause,sql-subselect,Sql Server,Syntax,Left Join,Where Clause,Sql Subselect,我有一个SELECT语句e,它生成一个结果集。我想将结果集简化为数据的最新版本,因此我在select的末尾放了一个WHERE子句,它读取WHERE.[version]=select MAXe.[version]FROM[dbo].[DBPCUnits]。 然后,我将另一个表f LEFT JOIN[dbo].[d_bpc]作为e.[bu_product_code]=f.idbpc上的f加入到记录集,因为我希望主表中显示一致的数据,而不是用户输入的不一致的信息。然后,我使用f中的许多字段替换e中的字

我有一个SELECT语句e,它生成一个结果集。我想将结果集简化为数据的最新版本,因此我在select的末尾放了一个WHERE子句,它读取WHERE.[version]=select MAXe.[version]FROM[dbo].[DBPCUnits]。 然后,我将另一个表f LEFT JOIN[dbo].[d_bpc]作为e.[bu_product_code]=f.idbpc上的f加入到记录集,因为我希望主表中显示一致的数据,而不是用户输入的不一致的信息。然后,我使用f中的许多字段替换e中的字段,这将得到我想要的干净数据集

这是我打算合并的4个结果集之一,以使我能够在大型分析中使用

我正在运行SQL Server 14.0

我的选择查询如下:

SELECT
e.[bu_product_code] AS ProductCode
,f.[bpc_desc] AS Description
,NULL AS BPCCountry
,f.[bpc_business_nature] AS BusinessNature
,e.[bu_year_month] AS YearMonth
,f.[bpc_active_ingredient] AS ActiveIngredient
,f.[bpc_ai_content] AS AIContent
,f.[bpc_formulation_type] AS FormulationType
,f.[bpc_brand] AS Brand
,f.[bpc_min_pack] AS MinPack
,f.[bpc_product_category] AS ProductCategory
,e.[bu_entity] AS Entity
,e.[bu_ship_to_ctry] AS ShipToCtry
,e.[bu_ccy] AS CCY
,e.[bu_data_type] AS DataType
,NULL AS ProductType
,e.[bu_latest] AS Latest
,e.[Version] AS Version
,e.[bu_account] AS UAccount
,e.[bu_entity_code] AS UEntityCode
,e.[bu_interco_code] AS UIntercoCode
,e.[bu_pic] AS Upic
,e.[bu_source] AS USource
,e.[bu_sales_rep] AS USalesRep
,e.[bu_qty_type] AS UQtyType
,e.[bu_amount] AS UAmount
FROM
[dbo].[d_bpcunits] e
LEFT JOIN [dbo].[d_bpc] as f on e.[bu_product_code] = f.idbpc
WHERE e.[Version] = (SELECT MAX(e.[Version]) FROM [dbo].[d_bpcunits])

提前感谢您的帮助。

我在您的查询中看到的唯一问题是最后一行

  --SELECT MAX(e.[Version]) FROM [dbo].[d_bpcunits]
需要换成什么

    SELECT MAX([Version]) FROM [dbo].[d_bpcunits]
然后查询变成

SELECT
e.[bu_product_code] AS ProductCode
,f.[bpc_desc] AS Description
,NULL AS BPCCountry
,f.[bpc_business_nature] AS BusinessNature
,e.[bu_year_month] AS YearMonth
,f.[bpc_active_ingredient] AS ActiveIngredient
,f.[bpc_ai_content] AS AIContent
,f.[bpc_formulation_type] AS FormulationType
,f.[bpc_brand] AS Brand
,f.[bpc_min_pack] AS MinPack
,f.[bpc_product_category] AS ProductCategory
,e.[bu_entity] AS Entity
,e.[bu_ship_to_ctry] AS ShipToCtry
,e.[bu_ccy] AS CCY
,e.[bu_data_type] AS DataType
,NULL AS ProductType
,e.[bu_latest] AS Latest
,e.[Version] AS Version
,e.[bu_account] AS UAccount
,e.[bu_entity_code] AS UEntityCode
,e.[bu_interco_code] AS UIntercoCode
,e.[bu_pic] AS Upic
,e.[bu_source] AS USource
,e.[bu_sales_rep] AS USalesRep
,e.[bu_qty_type] AS UQtyType
,e.[bu_amount] AS UAmount
FROM
[dbo].[d_bpcunits] e
LEFT JOIN [dbo].[d_bpc] as f on e.[bu_product_code] = f.idbpc
WHERE e.[Version] = (SELECT MAX([Version]) FROM [dbo].[d_bpcunits])

您得到的实际错误是什么?您是否希望每个产品或在d_bcpunits中表示的任何实体都具有相同的版本值集,以便检索版本4检索所有产品,因为它们都有版本4行?或者产品X可能从未改变,所以它在版本1,而产品Y在版本4?亲爱的DhruvJoshi,感谢您的评论。我刚试过,效果很好。这么简单的错误和简单的修复,却引起了这么多的麻烦。我真的很感谢你的支持。最好的问候,BB。@Honkonger谢谢你的帮助!请考虑以投票/评分作为答案。