문제

I have a t-sql query where sum function is duplicated. How to avoid duplicating those statements?

select 
    Id,
    sum(Value)
from
    SomeTable
group by 
    Id
having
    sum(Value) > 1000
도움이 되었습니까?

해결책

It look like table aliasing is not supported. I think with should work:

with tmptable (id,sumv)
as
(select
    Id,
    sum(Value) as sumv
from
    SomeTable
group by 
    Id  
)
select
    id,
    sumv 
from
    tmptable
where
   sumv>1000

And a fiddle:

http://sqlfiddle.com/#!6/0d3f2/2

다른 팁

You need to remove the sum(Value) from the group by clause.

You can use GROUP BY 1, 2 to group by (in this case) the first and second columns, and thus avoid duplication in the GROUP BY clause.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top