2015-09-03 63 views
1

我有一個Fact_Actuals yable與一個新的列「Segment_Id」。更新表內連接與第三個表

我需要從DimsegmentMaster插入段ID到Fact_Actuals中。

鏈接與SegmentMaster和Source Table列。

鏈接與FactTable是下面的Source Table列的度量值。

請提供我的更新查詢,因爲我下面的查詢不好。

UPDATE Application_DB.[cdw].[Fact_Actuals] 
    set segment_sid = 
    (SELECT c.SID 
    FROM Application_DB.[cdw].[Fact_Actuals] b 
    inner join Source_DB.STA.SourceTable a 
    ON convert(decimal(20,10),LTRIM(RTRIM(a.[K308]))) = b.NetExternalSales 
    and convert(decimal(20,10),LTRIM(RTRIM(a.[K203]))) = b.Quantity_CON 
    and convert(decimal(20,10),LTRIM(RTRIM(a.[K202]))) = b.Quantity_KG 
    inner join Application_DB.cdw.DimSegmentMaster c 
    ON RTRIM(a.[C005])=c.SegmentOriginal 
) 

回答

1

試試這個:

UPDATE b 
set segment_sid = c.sid 
FROM Application_DB.[cdw].[Fact_Actuals] b 
inner join Source_DB.STA.SourceTable a 
ON 
    convert(decimal(20,10),LTRIM(RTRIM(a.[K308])))=b.NetExternalSales 
    and convert(decimal(20,10),LTRIM(RTRIM(a.[K203])))=b.Quantity_CON 
    and convert(decimal(20,10),LTRIM(RTRIM(a.[K202])))=b.Quantity_KG 
inner join Application_DB.cdw.DimSegmentMaster c 
ON RTRIM(a.[C005])=c.SegmentOriginal