是否可以在同一查询中将列添加到现有表格并使用大小写设置该列的值?
ALTER TABLE #totalrevenue
ADD TotalRevenue_2003 Int CASE WHEN (Full2003 + Half2003 = 0) THEN NULL ELSE 1 END
FROM data.revenuesummary
WHERE #totalrevenue.ID = data.revenuesummary.ID'
谢谢!
答案 0 :(得分:0)
使用计算列怎么样? 在这里阅读更多: https://msdn.microsoft.com/en-us/library/ms188300.aspx
微小的例子
CREATE TABLE dbo.#Products
(
ID int IDENTITY (1,1) NOT NULL
, Full2003 smallint
, Half2003 smallint
, TotalRevenue2003 AS IIF(Full2003 + Half2003 = 0, NULL, 1)
);
INSERT INTO dbo.#Products
(Full2003, Half2003)
VALUES
(2, 3),
(2, 2),
(2, -2)
SELECT *
FROM dbo.#Products
结果:
ID Full2003 Half2003 TotalRevenue2003
1 2 3 1
2 2 2 1
3 2 -2 NULL