SQL : How can I use sub query in a query with group by section?

前端 未结 4 1219
一个人的身影
一个人的身影 2021-01-28 23:03

How can I use sub query in a query with group by section?

I use SQL Server 2008 R2 AND Delphi 2010

I receive this error:

Cannot perform an aggreg         


        
4条回答
  •  清歌不尽
    2021-01-28 23:53

    You should be able to put your sub-query under FROM clause, following this general pattern:

    SELECT TABLE1.ID, SUM(TABLE1.A), ROUND(SUM(T2.B1), 2)
    FROM TABLE1, (SELECT SUM(B) B1 FROM TABLE2 WHERE ...) T2
    GROUP BY TABLE1.ID
    

    Trying to "translate" your query, you'll probably get something similar to this:

    select 
        t1.sen, 
        sum(t1.d1)as d1, 
        sum(t1.d2)as d2, 
        sum(t1.d1+t1.d2) as d_sum,
        Round((sum((1000*(t1.d1+t1.d2))/(9500-(
            t2a.s
        )))),1) as SSS 
    from 
        tbl1 t1,
        (
           select sum(t2.t_shab+t2.t_rooz) s
           from tbl1 t2 
           where FCode=81 AND DCode=1 AND t2.sen<=t1.sen
        ) t2a
    where 
        FCode = 81 
        AND DCode = 1
    group by
        t1.sen
    

提交回复
热议问题