簡體   English   中英

SQL內部聯接計算列

[英]SQL Inner Join for calculated column

我想將銷售年至今設置為自動等於帳目代碼相等的發票抬頭總和。

ALTER TABLE DEBTORS_MASTER  
    ALTER COLUMN Sales_Year_To_Date AS 
          (SELECT SUM (INVOICE_HEADER.Total_Sell_Amount_Excl_VAT)
           FROM DEBTORS_MASTER
           INNER JOIN DEBTORS_MASTER ON INVOICE_HEADER.Account_Code = DEBTORS_MASTER.ACCOUNT_CODE);

我得到這個錯誤

關鍵字“ AS”附近的語法不正確

我認為你必須分開做

變更陳述

ALTER TABLE DEBTORS_MASTER
ALTER COLUMN Sales_Year_To_Date INT

更新聲明

UPDATE t1
SET Sales_Year_To_Date = Sales_Year_To_Date
FROM DEBTORS_MASTER t1
INNER JOIN (
    SELECT SUM(INVOICE_HEADER.Total_Sell_Amount_Excl_VAT) AS Sales_Year_To_Date
    FROM DEBTORS_MASTER
    INNER JOIN DEBTORS_MASTER ON INVOICE_HEADER.Account_Code = DEBTORS_MASTER.ACCOUNT_CODE
    ) t2 ON t2.ACCOUNT_CODE = t1.ACCOUNT_CODE

您可以使用此代碼,

UPDATE DM
SET Sales_Year_To_Date = IH.sumAmount
FROM DEBTORS_MASTER AS DM
INNER JOIN
    (
        SELECT Account_Code, SUM(Total_Sell_Amount_Excl_VAT) sumAmount
        FROM INVOICE_HEADER
        GROUP BY Account_Code
    ) IH
ON IH.Account_Code = DM.Account_Code

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM