更新表中的字段

时间:2014-05-07 16:06:28

标签: sql-server

我有下表PNLReference

 PnlId  LineTotalisationId  Designation TypeTotalisation    Totalisation
1   A   Gross Fees  Formule A01+A02+A03+A04+A05
2   A01 GF1 Comptes imputables  975800|758000|706900|706000|706430|706420|706410|706400|706530|706520|706510|706001|706401|706431|706531|706902
3   A02 GF2 Comptes imputables  706500|709400|706130|706120|706110|706100|706830|706820|706810|706800|706730|706720|706710|706700|706330|706101|706131|706331|706501|706701|706801|706831|709401|706731

我已将表DimPNL填充如下

INSERT [dbo].[DimPNL] (
PNLCode
,PNLName
,PNLParentId
,Operator
)
SELECT *
FROM (
SELECT t.c.value('.', 'nvarchar(255)') AS PNLCode
    ,Ref.Designation AS PNLName
    ,split.LineTotalisationId AS PNLParentId
    ,split.Operator AS Operator
FROM (
    SELECT tbl.Designation
        ,tbl.LineTotalisationId
        ,tbl.TypeTotalisation
        ,tbl.PnlId
        ,tbl.Totalisation
        ,CAST('<t>' + REPLACE(tbl.Totalisation, tbl.Operator, '</t><t>') + '</t>' AS XML) x
        ,tbl.Operator
    FROM ##TTResults AS tbl
    ) split
CROSS APPLY x.nodes('/t') t(c)
INNER JOIN [dbo].[PNLReference] Ref
    ON Ref.LineTotalisationId = t.c.value('.', 'nvarchar(255)')
) Result

表dimpnl内容一个必须填写的字段符号:如果表PNLReference中的Totalisation中的所有数字都以7开头,则符号将为-1,否则符号将为1.如何执行?任何想法?

1 个答案:

答案 0 :(得分:0)

使用CASE WHEN LEFT(Totalisation,1)='7' then -1 else 1 END [SIGN]将为您提供一个可以取MAX(sign)的字段,如果它保持为-1,那么它们都以7开始