如何将不同行和不同列的值相乘?

时间:2011-10-02 13:01:14

标签: sql-server-2005

这就是表格的样子。

Code     Items      Unit  QTY   Price  Total
-------  ---------  ----  ----  -----  -----
HTM001   Cable Tie  pcs   null  1.00   ?
HTM001s  Cable Tie  null  20    null

我尝试了这样的查询...

SELECT VRIJ1 FROM dbo.INVELE WHERE FK_BODEFINITOIN_USERDEFINED IN (894) AS QTY,
SELECT RESTWRDE FROM dbo.INVELE WHERE FK_BODEFINITOIN_USERDEFINED IN (898) AS PRICE,
(QTY*PRICE) AS TOTAL
FROM dbo.INVELE

然后我得到了这些:

Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'AS'.
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'AS'.

我想要的是喜欢这个20*1.00=Total

任何人都请帮忙!!!

1 个答案:

答案 0 :(得分:0)

;WITH T1(QTY, CODE) AS
(
  SELECT VRIJ1, LEFT(CODE, 7)
  FROM dbo.INVELE
  WHERE FK_BODEFINITION_USERDEFINED IN (894)
),
T2(PRICE, CODE) AS
(
  SELECT RESTWRDE, CODE
  FROM dbo.INVELE 
  WHERE FK_BODEFINITION_USERDEFINED IN (898)
)
SELECT T1.QTY, 
       T2.PRICE, 
       T1.QTY*T2.PRICE AS TOTAL  
FROM T1
  INNER JOIN T2
    ON T1.CODE = T2.CODE