我有一个生成结果集的SELECT
语句(e) 。我想将结果集减少到只有最新版本的数据,所以我WHERE
在我的选择末尾放了一个子句,内容为WHERE e.[Version] = (SELECT MAX(e.[Version]) FROM [dbo].[d_bpcunits])
. 然后我将JOIN
另一个表(f) LEFT JOIN [dbo].[d_bpc] as f on e.[bu_product_code] = f.idbpc
编辑到记录集,因为我希望从主表中显示一致的数据,而不是用户输入的不一致信息。然后,我使用(f)中的许多字段替换(e)中的字段,这应该会产生我想要的干净数据集。
这是我打算UNION ALL
一起使用的 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])
提前感谢您的帮助。