我需要创建一个临时函数,用于更大的查询,但我没有对数据库的创建权限(#TEMP 表除外)。
有什么方法可以为此目的使用 CTE 或 #temp 查询。也许我在这里遗漏了一些非常简单的东西。
示例(它可能是什么样子):-
with add1(x) as
return x+1
select add1(v.Value1), add1(v.Value2)
from Values v
Run Code Online (Sandbox Code Playgroud)
值表
Id Value1 Value2
1 1 4
2 2 5
3 3 6
Run Code Online (Sandbox Code Playgroud)
根据 Aaron Bertrand 的回答,我设法得到了一些工作。
CREATE TABLE #myTempTable
(
id int identity(1,1) primary key,
amount int,
col1 varchar(10),
col2 varchar(4)
);
-- quite a few more cols in my actual temp table,
-- omitted to show the real issue
INSERT #myTempTable(amount,col1, col2) VALUES(10,'a1', 'b1'),(15,'a2','b2');
;WITH processed AS
( …Run Code Online (Sandbox Code Playgroud) 不知道该怎么做:-
我有一个查询
select name, city, salary from Employee
我可以通过分组得到一个总和
select name, city, sum(salary) total_salary from Employee group by name, city
如何将每个城市的总数作为每个城市下的单独一行?
预期样本结果:
Name City Salary
n1 c1 10
n2 c1 20
T1 c1 30
n3 c2 20
n4 c2 50
T2 c2 70
Run Code Online (Sandbox Code Playgroud)