sql - 两个临时表的 SQL 加法
问题描述
我有两个临时表正在计算一些 ID。我想组合这些表来计算每个表,然后将它们加在一起。这就是我到目前为止所拥有的。
if object_id('tempdb..#order') is not null drop table #order
select count (a.patientSID) as 'Order Count'
into #order
from CPRSOrder.CPRSOrder a
join sstaff.SStaff b on b.staffSID = a.EnteredbyStaffSID
join spatient.spatient c on c.patientSID = a.patientSID
where b.staffName = xxxxxxxx
and a.enteredDateTime >= '20180801' and a.enteredDateTime <= '20180828'
if object_id('tempdb..#note') is not null drop table #note
select count (a.patientSID) as 'Note Count'
into #note
from tiu.tiudocument a
join sstaff.SStaff b on b.staffSID = a.EnteredbyStaffSID
--join spatient.spatient c on c.patientSID = a.patientSID
where b.staffName = xxxxxxxx
and a.episodeBeginDateTime >= '20180801' and a.episodeBeginDateTime <= '20180828'
select (select [Note Count] from #note) as 'Note Count',
(select [Order Count] from #order) as 'Order Count',
sum((select [Order Count] from #order) + (select [Note Count] from #note)) as Total
解决方案
删除sum()
, 除非你想聚合。此外,由于每个表只包含一行,因此可以通过使用交叉连接来稍微简化一下。
SELECT n.[Note Count],
o.[Order Count],
n.[Note Count] + o.[Order Count] [Total]
FROM #note n
CROSS JOIN #order o;
推荐阅读
- javascript - Electron.js 如何在 html 脚本标签中使用 require 函数
- sql-server - 如何从 sql 脚本逆向工程生成 ER 图
- c++ - C++ 运算符 - 方法
- java - 我无法从 android studio 中的子活动返回数据
- abap - 为什么时间戳减法在结果中给出减号?
- checksum - 如何为开源项目制作(功能)校验和?
- android - 在文件管理器中选择文件后更正真实路径
- android - 将 AsyncTask 与 Runnable 一起使用
- arrays - 如何将 .csv 文件的值添加到 Matlab 中的数组?
- c# - 如何在 C# 中解析多个元素属性值