sql - 子查询返回多个值。这是不允许的
问题描述
我正在尝试从不同的表中提取数据并尝试根据日期和客户 ID 获取 golivedata。下面是查询。
select c.customerID,
c.CustomerName,
cs.ContactName,
e.ComponentName,
e.ComponentVersion,
(
select ps.GoLiveDate
from Projects ps
where ps.GoLiveDate <= CURRENT_TIMESTAMP
and ps.CustomerID = c.CustomerID
) as CurrentGoLiveDate
from projects p
join customers c on c.CustomerID = p.CustomerID
join contacts cs on cs.ProjectID = p.ProjectID
join Environments e on e.ProjectID = p.ProjectID
where e.ComponentName like 'RP%'
and cs.ContactType = 'Account Manager'
但我收到一个错误:
解决方案
预期的结果会有所帮助,您可以利用存在并以这种方式获得价值。我假设您正在寻找至少有一行的客户(如果有多行,同一客户有多个上线日期),上线日期小于当前时间戳。
我认为这也可以帮助您获得预期的输出。
select c.customerID, c.CustomerName, cs.ContactName, e.ComponentName, e.ComponentVersion,
ps.GoLiveDate as CurrentGoLiveDate
from projects p
join customers c on c.CustomerID = p.CustomerID
join contacts cs on cs.ProjectID = p.ProjectID
join Environments e on e.ProjectID = p.ProjectID
where e.ComponentName like 'RP%' and cs.ContactType= 'Account Manager'
and exists (select 1 from Projects ps where ps.GoLiveDate <= CURRENT_TIMESTAMP and ps.CustomerID = c.CustomerID)
推荐阅读
- sas - 如何在 SAS 中将 SAS 表数据以及硬编码值插入 DB2 数据库
- html - 在网络抓取时从站点获取名称列表
- docker - 如何在 Ubuntu 18 上安装较旧的 Docker 版本(18.06.2)?
- python - 无法通过 Conda 导入因子分析器 Spyder 3.3.6 Python
- java - 我的 String 和 StringBuilder 不在控制台中显示输出
- reactjs - 在哪里存储用户的敏感数据?
- javascript - 如何处理淡出和删除的jQuery组合
- javascript - 默认选择选项Angular 8 Reactive forms
- python - 有效地计算在一组其他分组变量中分组的多个独立列的 value_counts
- sqlite - 文本列上的索引可以加快基于前缀的 LIKE 查询吗?