Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


TSQL SUM with GROUP BY


TSQL SUM with GROUP BY

Author
Message
luissantos
luissantos
Old Hand
Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)

Group: General Forum Members
Points: 319 Visits: 749
Hello comunity

I have the following script :
select CREND = PL.CREND, NOME = PO.NOME, NCONT = PO.NCONT, EVIRS = sum(PL.EVIRS), EREC = sum(PL.EREC), U_TXIRS = isnull((select
isnull(fo.txirs,0) from fo (nolock) where fo.fostamp = PL.fcstamp),0), U_VLIRS = sum(isnull((select (case when isnull(fo.txirs,0) > 0 then PL.evirs/
isnull(fo.txirs,0) * 100 else 0 end) from fo (nolock) where fo.fostamp = pl.fcstamp),0)) from pl (nolock) inner join po (nolock) on
pl.postamp=po.postamp inner join fo on pl.fcstamp=fo.fostamp
where (convert(char(10),PL.RDATA,121) BETWEEN '2007-01-01' AND '2007-12-31' and PL.EVIRS > 0)

group by pl.crend,po.nome,po.ncont,pl.fcstamp

But , sql server return this error :
37000(130)[Microsoft][SQL Native Client][SQL Server]Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

How can resolve this problem
Many thanks
Luis Santos



SQLZ
SQLZ
SSC Eights!
SSC Eights! (874 reputation)SSC Eights! (874 reputation)SSC Eights! (874 reputation)SSC Eights! (874 reputation)SSC Eights! (874 reputation)SSC Eights! (874 reputation)SSC Eights! (874 reputation)SSC Eights! (874 reputation)

Group: General Forum Members
Points: 874 Visits: 940
I might be missing something here but I'm not sure you need those sub-queries there as you're alread joining on the table fo. See if this works:


select CREND = PL.CREND, NOME = PO.NOME, NCONT = PO.NCONT, EVIRS = sum(PL.EVIRS), EREC = sum(PL.EREC), U_TXIRS = isnull(fo.txirs,0)
, U_VLIRS = sum(case when isnull(fo.txirs,0) > 0 then PL.evirs/ isnull(fo.txirs,0) * 100 else 0 end)
from pl (nolock)
inner join po (nolock) on pl.postamp=po.postamp inner join fo on pl.fcstamp=fo.fostamp
where convert(char(10),PL.RDATA,121) BETWEEN '2007-01-01' AND '2007-12-31'
AND PL.EVIRS > 0
group by pl.crend,po.nome,po.ncont,pl.fcstamp



Karl
source control for SQL Server
luissantos
luissantos
Old Hand
Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)

Group: General Forum Members
Points: 319 Visits: 749
Hello Karl
thanks for your reply, they work fine

bye
Luis Santos



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search