【发布时间】:2021-04-25 23:17:50
【问题描述】:
我从 SQL Server 开始。我首先编写了一个创建表的查询。有了这张表,我想添加一些行。
下面的代码创建了表格。
select
Element = [Key],
New = max(case when time_index=1 then value end),
'Current' = max(case when time_index>=2 then value end)
from
(select
[time_index], B.*
from
(select *
from ifrs17.output_bba
where id in (602677, 602777)) A
cross apply
(select [Key], Value
from OpenJson((select A.* FOR JSON PATH, WITHOUT_ARRAY_WRAPPER))
where [Key] not in ('time_index')) B
) A
group by
[Key]
结果在这里
| Element | New | Current |
|---|---|---|
| AAA | 10 | 20 |
| BBB | 15 | 34 |
| CCC | 17 | 22 |
现在,我想(例如)复制第二行(“BBB”)并将名称(“Element”)更改为“DDD”。
| Element | New | Current |
|---|---|---|
| AAA | 10 | 20 |
| BBB | 15 | 34 |
| CCC | 17 | 22 |
| DDD | 15 | 34 |
您知道如何进行吗?
【问题讨论】:
-
这能回答你的问题吗? Possible to store value of one select column and use it for the next one? 你会发现我的回答很有用,见第 3 部分
-
如果您看到我向您展示的参考资料,您将了解如何根据需要加倍。在
apply中,使用union all和where的任意组合以获得所需的结果
标签: sql sql-server union