使用SQL

时间:2016-07-07 20:12:11

标签: sql sql-server sql-server-2012

我正在尝试根据以下代码中显示的'Mathlogictable'中的'Yearmonth'和'Timedimensiondate'列将值重新转换为字符串....

我能够成功地完成它但现在我需要将字符串替换为数值逻辑表中没有的零值。

例如:如果您执行以下代码,我们可以找到第一行具有import-consumptionqty1(314)的值,但不能用于import consumtionQty(1539)..... like

  YEARMONTH         OUTPUTFORMULA                                   TIMEDIMENSIONDATE                 
  CV(N1)  2002: 10  236846.000 + Import - Consumption qty1(1539)    2002-10-01

我需要在公式列中将没有值的字符串替换为零......这样我的输出应该像

Yearmonth           Formula         TimeDimensionDate      
CV(N1)  2002: 10    236846.000 + 0  2002-10-01

我怎样才能做到这一点?

请注意,我创建了一个名为#tablevaluedfunction的tem表,并将输出插入该表....并进行了连接.....

以下是我正在尝试的代码

Begin
CREATE TABLE #MathLogicTable
(
IDNUM INTEGER IDENTITY(1,1),
FORMULA Varchar(160),
INPUTName varchar(160),
AttributeValue Decimal(15,3),
yearmonth varchar(160),
TimeDimensionDate date
)

INSERT INTO #MathLogicTable (FORMULA,INPUTName,AttributeValue,yearmonth,TimeDimensionDate)
VALUES ('Import - Consumption qty1(314) + Import - Consumption qty1(1539)','Import - Consumption qty1(1539)',855,'CV(N1)  2002: 1','2002-01-01')
,('Import - Consumption qty1(314) + Import - Consumption qty1(1539)','Import - Consumption qty1(1539)',4860,'CV(N1)  2002: 2','2002-02-01')
,('Import - Consumption qty1(314) + Import - Consumption qty1(1539)','Import - Consumption qty1(1539)',21683,'CV(N1)  2002: 3','2002-03-01')
,('Import - Consumption qty1(314) + Import - Consumption qty1(1539)','Import - Consumption qty1(1539)',12800,'CV(N1)  2002: 4','2002-04-01')
,('Import - Consumption qty1(314) + Import - Consumption qty1(1539)','Import - Consumption qty1(1539)',802,'CV(N1)  2002: 5','2002-05-01')
,('Import - Consumption qty1(314) + Import - Consumption qty1(1539)','Import - Consumption qty1(314)',250616,' CV(N1)  2002: 6 ','2002-06-01')
,('Import - Consumption qty1(314) + Import - Consumption qty1(1539)','Import - Consumption qty1(314)',333064,' CV(N1)  2002: 7 ','2002-07-01')
,('Import - Consumption qty1(314) + Import - Consumption qty1(1539)','Import - Consumption qty1(314)',131397,' CV(N1)  2002: 8 ','2002-08-01')
,('Import - Consumption qty1(314) + Import - Consumption qty1(1539)','Import - Consumption qty1(314)',294510,' CV(N1)  2002: 9 ','2002-09-01')
,('Import - Consumption qty1(314) + Import - Consumption qty1(1539)','Import - Consumption qty1(314)',236846,' CV(N1)  2002: 10 ','2002-10-01')
,('Import - Consumption qty1(314) + Import - Consumption qty1(1539)','Import - Consumption qty1(314)',397183,' CV(N1)  2002: 11 ','2002-11-01')
,('Import - Consumption qty1(314) + Import - Consumption qty1(1539)','Import - Consumption qty1(314)',128616,' CV(N1)  2002: 12 ','2002-12-01')

;WITH cteSplitFormula AS (
    SELECT
       IDNUM
       ,TimeDimensionDate
       ,CAST(LTRIM(RTRIM(LEFT(FORMULA, PATINDEX('%[+\*/^]%',FORMULA+'+')-1))) AS VARCHAR(5000)) AS FormulaItem
       ,STUFF(FORMULA, 1, PATINDEX('%[+\*/^]%',FORMULA+'+'), '')  as RemainingFromula
    FROM
       #MathLogicTable

    UNION ALL
    SELECT
       IDNUM
       ,TimeDimensionDate
       ,CAST(LTRIM(RTRIM(LEFT(RemainingFromula, PATINDEX('%[+\*/^]%',RemainingFromula+'+')-1))) AS VARCHAR(5000)) AS FormulaItem
       ,STUFF(RemainingFromula, 1, PATINDEX('%[+\*/^]%',RemainingFromula+'+'), '')  as RemainingFromula
    FROM
       cteSplitFormula
    WHERE 
       LEN(RemainingFromula) > 0

)

, ctePartsValues AS (
    SELECT
       sf.IDNUM
       ,sf.TimeDimensionDate
       ,sf.FormulaItem AS FormulaItem
       ,ISNULL(m.AttributeValue,0) AS ReplacementValue
       ,ROW_NUMBER() OVER(PARTITION BY sf.IDNUM ORDER BY (SELECT 1)) AS [ReplacementOrderID]
    FROM
       cteSplitFormula sf
       FULL OUTER JOIN #MathLogicTable m
       ON sf.FormulaItem = LTRIM(RTRIM(m.INPUTName))
       AND sf.TimeDimensionDate = m.TimeDimensionDate
)

, cteRecursiveDataSource AS (
    SELECT
       m.IDNUM
       ,REPLACE(m.Formula,pv.FormulaItem,pv.ReplacementValue) as Formula
       ,pv.ReplacementOrderID
    FROM
       #MathLogicTable m
       INNER JOIN ctePartsValues pv
       ON m.IDNUM = pv.IDNUM
       AND pv.ReplacementOrderID = 1

    UNION ALL

    SELECT
       rds.IDNUM
       ,REPLACE(rds.Formula,pv.FormulaItem,pv.ReplacementValue) as Formula
       ,pv.ReplacementOrderID
    FROM
       cteRecursiveDataSource rds
       INNER JOIN ctePartsValues pv
       ON rds.IDNUM = pv.IDNUM
       AND rds.ReplacementOrderID + 1 = pv.ReplacementOrderID
)

, cteIdentifyRowWithLastReplacementStatement AS (
    SELECT
       *
       ,ROW_NUMBER() OVER (PARTITION BY IDNUM ORDER BY ReplacementOrderId DESC) as FinalRow
    FROM
       cteRecursiveDataSource
)

SELECT *
FROM
    cteIdentifyRowWithLastReplacementStatement
WHERE
    FinalRow = 1


DROP table #MathLogicTable
END

由于

1 个答案:

答案 0 :(得分:1)

;WITH cteSplitFormula AS (
    SELECT
       IDNUM
       ,TimeDimensionDate
       ,CAST(LTRIM(RTRIM(LEFT(REPLACE(Formula,'Import -','ImportReplaced'), PATINDEX('%[+-\*/^]%',REPLACE(Formula,'Import -','ImportReplaced')+'+')-1))) AS VARCHAR(5000)) AS FormulaItem
       ,STUFF(REPLACE(Formula,'Import -','ImportReplaced'), 1, PATINDEX('%[+-\*/^]%',REPLACE(Formula,'Import -','ImportReplaced')+'+'), '')  as RemainingFromula
    FROM
       #MathLogicTable

    UNION ALL
    SELECT
       IDNUM
       ,TimeDimensionDate
       ,CAST(LTRIM(RTRIM(LEFT(RemainingFromula, PATINDEX('%[+-\*/^]%',RemainingFromula+'+')-1))) AS VARCHAR(5000)) AS FormulaItem
       ,STUFF(RemainingFromula, 1, PATINDEX('%[+-\*/^]%',RemainingFromula+'+'), '')  as RemainingFromula
    FROM
       cteSplitFormula
    WHERE 
       LEN(RemainingFromula) > 0

)

, ctePartsValues AS (
    SELECT
       sf.IDNUM
       ,sf.TimeDimensionDate
       ,REPLACE(sf.FormulaItem,'ImportReplaced','Import -') AS FormulaItem
       ,ISNULL(m.AttributeValue,0) AS ReplacementValue
       ,ROW_NUMBER() OVER(PARTITION BY sf.IDNUM ORDER BY (SELECT 1)) AS [ReplacementOrderID]
    FROM
       cteSplitFormula sf
       FULL OUTER JOIN #MathLogicTable m
       ON REPLACE(sf.FormulaItem,'ImportReplaced','Import -') = LTRIM(RTRIM(m.INPUTName))
       AND sf.TimeDimensionDate = m.TimeDimensionDate
)

, cteRecursiveDataSource AS (
    SELECT
       m.IDNUM
       ,REPLACE(m.Formula,pv.FormulaItem,pv.ReplacementValue) as Formula
       ,pv.ReplacementOrderID
    FROM
       #MathLogicTable m
       INNER JOIN ctePartsValues pv
       ON m.IDNUM = pv.IDNUM
       AND pv.ReplacementOrderID = 1

    UNION ALL

    SELECT
       rds.IDNUM
       ,REPLACE(rds.Formula,pv.FormulaItem,pv.ReplacementValue) as Formula
       ,pv.ReplacementOrderID
    FROM
       cteRecursiveDataSource rds
       INNER JOIN ctePartsValues pv
       ON rds.IDNUM = pv.IDNUM
       AND rds.ReplacementOrderID + 1 = pv.ReplacementOrderID
)

, cteIdentifyRowWithLastReplacementStatement AS (
    SELECT
       *
       ,ROW_NUMBER() OVER (PARTITION BY IDNUM ORDER BY ReplacementOrderId DESC) as FinalRow
    FROM
       cteRecursiveDataSource
)

SELECT *
FROM
    cteIdentifyRowWithLastReplacementStatement
WHERE
    FinalRow = 1