查询1返回一个数字 AveSpace,即100
select avg(spaceusage) as AveSpace
from
(
select top (5) * from Employee
order by Date desc
) as s
查询2返回一个数字 TotalSpace,即1000
select top(1) TotalSpace from SpaceTable
order by date
我想将结果合并到一个数据集中,并添加第三列unitSpace,用于计算查询2 /查询1的结果,即1000/100 = 10
输出3列
[AveSpace] [TotalSpace] [unitSpace]
100 1000 10
答案 0 :(得分:2)
将每个单独的查询视为子查询,然后计算结果:
SELECT a.AveSpace, b.TotalSpace, (CAST(a.AveSpace AS float) / CAST(b.TotalSpace AS float)) as UnitSpace
FROM
(
SELECT TOP 1 AVG(spaceusage) as AveSpace FROM (SELECT TOP 5 * FROM Employee ORDER BY date DESC)
) a,
(
SELECT TOP 1 TotalSpace FROM SpaceTable ORDER BY date
) b
答案 1 :(得分:0)
宣布@TotalSpace FLOAT
SELECT TOP 1 @TotalSpace = SpaceTable的TotalSpace 按日期排序
SELECT选择avg(spaceusage)为AveSpace,@ TotalSpace AS TotalSpace,@ TotalSpace / avg(spaceusage)AS unitSpace 从 ( 从员工中选择top(5)* 按日期排序 )作为s