How can I compute values in a TSQL Update statemen

2019-09-09 06:51发布

I want to create three temp tables and then combine their contents like so:

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

Now I want to replace the placeholder Nulls in the calculated fields, but don't know how to do that. My best idea so far is:

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

Is there a better way (one that actually works, for instance)?

3条回答
冷血范
2楼-- · 2019-09-09 07:22

Using computed columns:

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

And skip them in INSERT statement:

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

Warning:

Division by WEEK1PRICE so you could add (WEEK2PRICE - WEEK1PRICE) / NULLIF(WEEK1PRICE,0) to avoid Math exception or add CHECK constraint to ensure that WEEK1PRICE is not 0.

查看更多
The star\"
3楼-- · 2019-09-09 07:23

Update column in one table you can use Cross join on simple join

For example:

Update m
Set col1 = col1 - col2
From mytable m 
join mytable n On m.col2 = n.col2
查看更多
该账号已被封号
4楼-- · 2019-09-09 07:26

First thing: why are you using varchar datatype for following columns?

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

It is better to use decimal or integer data types. As you are using sum() function to calculate its value and it will return int/decimal value

Second: you can calculate USAGEVARIANCE,PRICEVARIANCE,PRICEVARIANCEPERCENTAGE while inserting into the table. For example:

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

etc and then insert it into the temporary table

查看更多
登录 后发表回答