我需要使用表中其他列的值创建一些包含一些自动生成数据的列
我知道如果我做这样的事情就行了
CREATE TABLE #Temp(ColumnA VARCHAR(50), ColumnB INT)
INSERT INTO #Temp
VALUES('NAME1', 1)
, ('NAME2', 1)
, ('NAME3', 1)
, ('NAME4', 2)
, ('NAME5', 2)
, ('NAME6', 3)
, ('NAME7', 3)
;WITH Numbered
AS
(
SELECT ColumnA, ColumnB, ROW_NUMBER() OVER (PARTITION BY ColumnB ORDER BY ColumnA) AS RowNum
FROM #Temp
)
,RecordCounts
AS
(
SELECT ColumnB, COUNT(1) AS RecordCount
FROM #Temp
GROUP BY ColumnB
)
,RecursiveCTE
AS
(
SELECT ColumnB, CONVERT(VARCHAR(50), ColumnA) AS ColumnA, RowNum
FROM Numbered
WHERE RowNum = 1
UNION ALL
SELECT N.ColumnB, CONVERT(VARCHAR(50), R.ColumnA + ', ' + N.ColumnA), N.RowNum
FROM RecursiveCTE R
INNER JOIN Numbered N ON R.ColumnB = N.ColumnB
AND N.RowNum = R.RowNum + 1
)
SELECT ColumnA
FROM RecursiveCTE R
INNER JOIN RecordCounts RC ON R.ColumnB = RC.ColumnB
AND R.RowNum = RC.RecordCount
ORDER BY R.ColumnB
DROP TABLE #Temp
但是,我想在添加查询中进行强制转换,但这不起作用
ALTER TABLE Directions ADD RevisedLat INT
UPDATE Directions SET RevisedLat = CAST(Latitude*3200/90 AS INT)
让这项工作最好的方法是什么?只是添加和更新?
答案 0 :(得分:2)
尝试计算列
ALTER TABLE Directions
ADD RevisedLat as (Cast(Latitude*3200/90 as INT))