I have this query which works…
select
t.date,
sum(gk.d) as d,
sum(gk.p) as p,
(sum(d)-sum(p)) as s
But I’m wondering is there a way to select value ‘s’ as d-p, something like this:
select
t.date,
sum(gk.d) as d,
sum(gk.p) as p,
(d-p) as s // not working
SQL in general does not support referencing column aliases in the same SELECT clause, which is why the first version works for you but the second does not. The alternative is to use a derived table/inline view:
Mind that SQL Server requires you to define a table alias for derive tables/inline views – hence the “AS x”