如何在TSQL Update语句中计算值?

时间:2016-01-16 17:14:07

标签: sql sql-server tsql sql-update calculated-columns

我想创建三个临时表,然后像这样组合它们的内容:

CREATE TABLE #TEMP1
MEMBERITEMCODE VARCHAR(25),
WEEK1USAGE VARCHAR(25),
WEEK1PRICE VARCHAR(25);

INSERT INTO #TEMP1 (MEMBERITEMCODE, WEEK1USAGE, WEEK1PRICE)
SELECT MEMBERITEMCODE, SUM(QTYSHIPPED), PRICE 
FROM INVOICEDETAIL 
WHERE UNIT=@UNIT AND INVOICEDATE BETWEEN @BEGDATE AND @WEEK1END
GROUP BY MEMBERITEMCODE, PRICE

CREATE TABLE #TEMP2
MEMBERITEMCODE VARCHAR(25),
WEEK2USAGE VARCHAR(25),
WEEK2PRICE VARCHAR(25);

INSERT INTO #TEMP2 (MEMBERITEMCODE, WEEK2USAGE, WEEK2PRICE)
SELECT MEMBERITEMCODE, SUM(QTYSHIPPED), PRICE 
FROM INVOICEDETAIL 
WHERE UNIT=@UNIT AND INVOICEDATE BETWEEN @WEEK2BEGIN AND @ENDDATE
GROUP BY MEMBERITEMCODE, PRICE

CREATE TABLE #TEMP3
MEMBERITEMCODE VARCHAR(25),
DESCRIPTION VARCHAR(200),
THIS VARCHAR(25),
THAT VARCHAR(25),
THEOTHERTHING VARCHAR(25);

INSERT INTO #TEMP3 (MEMBERITEMCODE, DESCRIPTION, THIS, THAT, THEOTHERTHING)
SELECT MEMBERITEMCODE, DESCRIPTION, THIS, THAT, THEOTHERTHING 
FROM INVOICEDETAIL 
WHERE UNIT=@UNIT AND INVOICEDATE BETWEEN @BEGDATE AND @ENDDATE

CREATE TABLE #TEMPCOMBINED
MEMBERITEMCODE VARCHAR(25),
DESCRIPTION VARCHAR(200),
THIS VARCHAR(25),
THAT VARCHAR(25),
THEOTHERTHING VARCHAR(25),
WEEK1USAGE VARCHAR(25),
WEEK1PRICE VARCHAR(25);
WEEK2USAGE VARCHAR(25),
WEEK2PRICE VARCHAR(25),
USAGEVARIANCE VARCHAR(25),
PRICEVARIANCE VARCHAR(25),
PRICEVARIANCEPERCENTAGE VARCHAR(25);

INSERT INTO #TEMPCOMBINED (MEMBERITEMCODE, DESCRIPTION, THIS, THAT, THEOTHERTHING, WEEK1USAGE, WEEK1PRICE, WEEK2USAGE, WEEK2PRICE, USAGEVARIANCE,
PRICEVARIANCE, PRICEVARIANCEPERCENTAGE)
SELECT T1.MEMBERITEMCODE, T3.DESCRIPTION, T3.THIS, T3.THAT, T3.THEOTHERTHING, T1.WEEK1USAGE, T1.WEEK1PRICE, T2.WEEK2USAGE, T2.WEEK2PRICE, NULL,  NULL,  NULL
FROM #TEMP1 T1
LEFT JOIN #TEMP2 T2 ON T1.MEMBERITEMCODE = T2.MEMBERITEMCODE
LEFT JOIN #TEMP3 T3 ON T1.MEMBERITEMCODE = T3.MEMBERITEMCODE

现在我想在计算字段中替换占位符Null,但不知道如何做到这一点。到目前为止,我最好的想法是:

UPDATE #TEMPCOMBINED
SET USAGEVARIANCE = WEEK2USAGE - WEEK1USAGE,
PRICEVARIANCE = WEEK2PRICE - WEEK1PRICE,
PRICEVARIANCEPERCENTAGE = (WEEK2PRICE - WEEK1PRICE) / WEEK1PRICE

是否有更好的方法(例如,实际上有效)?

3 个答案:

答案 0 :(得分:3)

第一件事:为什么你在后续列中使用varchar数据类型?

USAGEVARIANCE, ,WEEK2USAGE,WEEK1USAGE,PRICEVARIANCE , WEEK2PRICE , WEEK1PRICE, PRICEVARIANCEPERCENTAGE, WEEK2PRICE , WEEK1PRICE,WEEK1PRICE ;

最好使用十进制或整数数据类型。当您使用sum()函数计算其值时,它将返回int / decimal值

第二:您可以在插入表格时计算USAGEVARIANCE,PRICEVARIANCE,PRICEVARIANCEPERCENTAGE。例如:

select a,b, (a-b) as USAGEVARIANCE, (c-d) as PRICEVARIANCE from table 

等然后将其插入临时表

答案 1 :(得分:1)

更新您可以使用的表格中的列 简单连接上的交叉连接

例如:

Update m
Set col1 = col1 - col2
From mytable m 
join mytable n On m.col2 = n.col2

答案 2 :(得分:1)

使用计算列:

CREATE TABLE #TEMPCOMBINED(
  MEMBERITEMCODE VARCHAR(25),
  DESCRIPTION VARCHAR(200),
  THIS VARCHAR(25),
  THAT VARCHAR(25),
  THEOTHERTHING VARCHAR(25),
  WEEK1USAGE DECIMAL(18,10),
  WEEK1PRICE DECIMAL(18,10),
  WEEK2USAGE DECIMAL(18,10),
  WEEK2PRICE DECIMAL(18,10),
  USAGEVARIANCE   AS WEEK2USAGE - WEEK1USAGE,
  PRICEVARIANCE   AS WEEK2PRICE - WEEK1PRICE,
  PRICEVARIANCEPERCENTAGE  AS (WEEK2PRICE - WEEK1PRICE) / WEEK1PRICE
);

LiveDemo

并在INSERT声明中跳过它们:

INSERT INTO #TEMPCOMBINED (MEMBERITEMCODE, DESCRIPTION, THIS, THAT, THEOTHERTHING, WEEK1USAGE, WEEK1PRICE, WEEK2USAGE, WEEK2PRICE)
SELECT T1.MEMBERITEMCODE, T3.DESCRIPTION, T3.THIS, T3.THAT, T3.THEOTHERTHING, T1.WEEK1USAGE, T1.WEEK1PRICE, T2.WEEK2USAGE, T2.WEEK2PRICE
FROM #TEMP1 T1
LEFT JOIN #TEMP2 T2 ON T1.MEMBERITEMCODE = T2.MEMBERITEMCODE
LEFT JOIN #TEMP3 T3 ON T1.MEMBERITEMCODE = T3.MEMBERITEMCODE

警告:

WEEK1PRICE划分,以便您可以添加 (WEEK2PRICE - WEEK1PRICE) / NULLIF(WEEK1PRICE,0)以避免数学异常或添加CHECK约束以确保WEEK1PRICE不为0.