sql - 如何将变量中一列的值输出到
问题描述
有一个查询以这种形式返回数据:
| ID |
+----+
| 25 |
| 28 |
请求总是会返回 2 个值 比如我需要把数据 (25,28) 的值拉到一个变量中,也就是拉到 Request 中:
select ID from table1
where CODE in ('bonus', 'Pnlty') and dcl_id := iId
我尝试通过枢轴
with BaseData as (select ID, CODE from table1
where CODE in ('bonus', 'Pnlty') and dcl_id := iId)
select * from BaseData
PIVOT
(
MAX(ID)
FOR CODE in ('bonus', 'Pnlty')
)
但是如何将 id on 的含义推导出到这里呢?
我需要这样的查询:
select ID as id1,
ID as id2
into bId, pID
into from table1
where CODE in ('bonus', 'Pnlty') and dcl_id := iId
解决方案
每个代码一个变量。使用两个查询或使用条件聚合。
两个查询
create procedure p(iid integer) as
bid integer;
pid integer;
begin
select id into bid
from table1
where code = 'bonus' and dcl_id = iid;
select id into pid
from table1
where code = 'Pnlty' and dcl_id = iid;
end;
条件聚合
create procedure p(iid integer) as
bid integer;
pid integer;
begin
select
max(case when code = 'bonus' then id end),
max(case when code = 'Pnlty' then id end)
into bid, pid
from table1
where code in ('bonus', 'Pnlty') and dcl_id = iid;
end;
推荐阅读
- python-3.x - 使用 pd.read_sql_query 时在数据框的最后一行获取 NONE
- azure-devops - 获取项目下所有 CD 管道的发布 ID/编号
- html - 如何在悬停时制作项目转换的无序列表
- eclipse - 如何在 Eclipse 中终止调试会话?调试视图中未显示任何内容
- angular - 出口、延迟加载和多个子层的路由问题
- node.js - 旋转服务器时出现意外标识符
- c# - 无法使用 DI 解析类型“System.Net.Http.HttpClient”的服务
- java - Java Set 多线程处理
- php - 错误:1071,指定的密钥太长;最大密钥长度为 1000 字节
- r - 提取最多两位数