我打算做的是有一个剩余订单列,这将是以下计算的结果: [orderedvalue] - [totaljobvalue]
select [orderedvalue] = sum([ordervalue]),
jobtype,
[totaljobvalue] = (select sum([total value])
from job
where jobType = quotetrackerorders.jobType
and reference like '%40330%')
from quotetrackerorders
where quoteref like '%40330%'
group by jobtype
我尝试过以下代码:
select [orderedvalue] = sum([ordervalue]),
jobtype,
[totaljobvalue] = (select sum([total value])
from job
where jobType = quotetrackerorders.jobType
and reference like '%40330%'),
[orderremaining] = [orderedvalue] - [totaljobvalue]
from quotetrackerorders
where quoteref like '%40330%'
group by jobtype
但是这不起作用,因为它不会将任何别名识别为列...我该怎么做才能实现以下结果:
您知道如何让SQL将这些别名识别为列吗?
答案 0 :(得分:2)
考虑以下
Select A.*
,SomeName = [orderedvalue] - [totaljobvalue]
From (
Select [orderedvalue] = sum([ordervalue])
, jobtype
, [totaljobvalue] = (select sum([total value])
from job
where jobType = quotetrackerorders.jobType
and reference like '%40330%')
from quotetrackerorders
where quoteref like '%40330%'
group by jobtype
) A