将计算列插入 table
Insert calculated column into a table
我一直在尝试在 SQL 中做一些事情,但我无法在 Stack Overflow 上找到正确的答案。我已经创建了一个 table(称为 RMBS_STANDARDIZED
),它有一列 LOAN_PERC_ISSUER
。 table 是我标准化命名约定的其他几个 table 的联合。
加载 table 后我想做的是用这个内部查询更新它(注意当我 运行 这个它 returns 返回正确的值但显然单列)。因此,我想将返回的查询存储在 table.
SELECT
ISSUER_SERIES,
SUM(COALESCE(LOAN_BALANCE, 0)) AS TOTAL_BALANCE
FROM
RMBS_STANDARDIZED
GROUP BY
ISSUER_SERIES
我尝试使用的插入查询是
INSERT INTO RMBS_STANDARDIZED (LOAN_PERC_ISSUER)
SELECT
COALESCE(T1.LOAN_BALANCE, 0) / T2.TOTAL_BALANCE
FROM
RMBS_STANDARDIZED T1
LEFT JOIN
(SELECT
ISSUER_SERIES,
SUM(COALESCE(LOAN_BALANCE, 0)) AS TOTAL_BALANCE
FROM
RMBS_STANDARDIZED
GROUP BY
ISSUER_SERIES) T2 ON T1.ISSUER_SERIES = T2.ISSUER_SERIES
但是我得到以下错误
Cannot insert the value NULL into column 'ISSUER'
如果我调整 ISSUER
列以允许 NULL,则该列不会更新或保持为 NULL。
或者,我尝试了以下方法
ALTER TABLE RMBS_STANDARDIZED
ADD New_Column AS (SELECT
COALESCE(T1.LOAN_BALANCE, 0) / T2.TOTAL_BALANCE
FROM
RMBS_STANDARDIZED T1
LEFT JOIN
(SELECT
ISSUER_SERIES,
SUM(COALESCE(LOAN_BALANCE, 0)) AS TOTAL_BALANCE
FROM
RMBS_STANDARDIZED GROUP BY ISSUER_SERIES) T2
ON T1.ISSUER_SERIES = T2.ISSUER_SERIES)
但是得到如下错误
Subqueries are not allowed in this context. Only scalar expressions are allowed.
任何想法都会有所帮助。在创建最终 table 之前,我最好使用 CTE 或创建 temp/hash table?我正在使用 SQL 服务器和 SQL 服务器管理工作室。
您的第一个查询失败,因为 LOAN_PERC_ISSUER 有一个 NOT NULL 约束。将您的查询重写为:
SELECT COALESCE(T1.LOAN_BALANCE / T2.TOTAL_BALANCE, 0)
FROM RMBS_STANDARDIZED AS T1
LEFT OUTER JOIN (SELECT ISSUER_SERIES,
SUM(COALESCE(LOAN_BALANCE, 0)) AS TOTAL_BALANCE
FROM RMBS_STANDARDIZED
GROUP BY ISSUER_SERIES) AS T2
ON T1.ISSUER_SERIES = T2.ISSUER_SERIES;
计算列不能有任何类型的子查询,并且必须仅使用来自行值的数据。有可能将查询放入 UDF 并将 UDF 用于计算列,但不太推荐...
一个更合适的方法是使用触发器
我一直在尝试在 SQL 中做一些事情,但我无法在 Stack Overflow 上找到正确的答案。我已经创建了一个 table(称为 RMBS_STANDARDIZED
),它有一列 LOAN_PERC_ISSUER
。 table 是我标准化命名约定的其他几个 table 的联合。
加载 table 后我想做的是用这个内部查询更新它(注意当我 运行 这个它 returns 返回正确的值但显然单列)。因此,我想将返回的查询存储在 table.
SELECT
ISSUER_SERIES,
SUM(COALESCE(LOAN_BALANCE, 0)) AS TOTAL_BALANCE
FROM
RMBS_STANDARDIZED
GROUP BY
ISSUER_SERIES
我尝试使用的插入查询是
INSERT INTO RMBS_STANDARDIZED (LOAN_PERC_ISSUER)
SELECT
COALESCE(T1.LOAN_BALANCE, 0) / T2.TOTAL_BALANCE
FROM
RMBS_STANDARDIZED T1
LEFT JOIN
(SELECT
ISSUER_SERIES,
SUM(COALESCE(LOAN_BALANCE, 0)) AS TOTAL_BALANCE
FROM
RMBS_STANDARDIZED
GROUP BY
ISSUER_SERIES) T2 ON T1.ISSUER_SERIES = T2.ISSUER_SERIES
但是我得到以下错误
Cannot insert the value NULL into column 'ISSUER'
如果我调整 ISSUER
列以允许 NULL,则该列不会更新或保持为 NULL。
或者,我尝试了以下方法
ALTER TABLE RMBS_STANDARDIZED
ADD New_Column AS (SELECT
COALESCE(T1.LOAN_BALANCE, 0) / T2.TOTAL_BALANCE
FROM
RMBS_STANDARDIZED T1
LEFT JOIN
(SELECT
ISSUER_SERIES,
SUM(COALESCE(LOAN_BALANCE, 0)) AS TOTAL_BALANCE
FROM
RMBS_STANDARDIZED GROUP BY ISSUER_SERIES) T2
ON T1.ISSUER_SERIES = T2.ISSUER_SERIES)
但是得到如下错误
Subqueries are not allowed in this context. Only scalar expressions are allowed.
任何想法都会有所帮助。在创建最终 table 之前,我最好使用 CTE 或创建 temp/hash table?我正在使用 SQL 服务器和 SQL 服务器管理工作室。
您的第一个查询失败,因为 LOAN_PERC_ISSUER 有一个 NOT NULL 约束。将您的查询重写为:
SELECT COALESCE(T1.LOAN_BALANCE / T2.TOTAL_BALANCE, 0)
FROM RMBS_STANDARDIZED AS T1
LEFT OUTER JOIN (SELECT ISSUER_SERIES,
SUM(COALESCE(LOAN_BALANCE, 0)) AS TOTAL_BALANCE
FROM RMBS_STANDARDIZED
GROUP BY ISSUER_SERIES) AS T2
ON T1.ISSUER_SERIES = T2.ISSUER_SERIES;
计算列不能有任何类型的子查询,并且必须仅使用来自行值的数据。有可能将查询放入 UDF 并将 UDF 用于计算列,但不太推荐...
一个更合适的方法是使用触发器