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

前端 未结 4 1224
一个人的身影
一个人的身影 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:49

    Without any warranty, you can try, if it doesn't work let me know, I'll delete my answer

    create function getSumBSen(@pfcode number, @pdcode number, @pSen number) returns number
    as
    
    begin
         declare @r number;
         select 
             @r =sum(t2.t_shab + t2.t_rooz) 
         from 
             tbl1 t2 
         where 
             t2.FCode = @pfcode 
         and t2.DCode = @pdcode 
         and t2.sen <= @pSen
         group by t2.FCode, t2.DCode;
    
         return (@r);
    end;
    
    
    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-getSumBSen(t1.FCode, t1.dcode, t1.sen)))),1) as SSS 
    from 
        tbl1 t1
    where 
        t1.FCode = 81 
    and t1.DCode = 1 
    group by 
        t1.sen;
    

    memento:

    • Function creation

    added, it won't let me actually fix the code because my edit is too short. So I had to write some more miscellaneous junk so that the code fix will be accepted.

提交回复
热议问题