Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Group By with Subquery

Author  Topic 

psfaro
Starting Member

49 Posts

Posted - 2012-02-07 : 12:32:46
Hi

I've a simple Group By Command, but i need to specify vl_n(that is a Subquery that returns a Value) in the group by.

How can i do this ?

(Can't specify Vl_ne neither the sub select in Group By )

select a.cod_fornec,b.nome_for,val_adj=sum(val_adj)
,vl_ne=(select sum(val_enc) from eaqenc where EAQENC.prc_compra=a.prc_compra and EAQENC.cod_fornec=a.cod_fornec)
from EAQCMP a
INNER JOIN siscont_demo.dbo.GGSFOR b ON a.cod_fornec=b.cod_fornec
where adjudicado='S' and prc_compra='6011E2'
GROUP BY a.cod_fornec,b.nome_for,vl_ne

Regards

Pedro faro

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-07 : 12:39:19
[code]
select a.cod_fornec,b.nome_for,val_adj=sum(val_adj),c.vl_ne
from EAQCMP a
INNER JOIN siscont_demo.dbo.GGSFOR b ON a.cod_fornec=b.cod_fornec
INNER JOIN (select cod_fornec,prc_compra,sum(val_enc) AS vl_ne
from eaqenc group by cod_fornec,prc_compra)c
on c.prc_compra=a.prc_compra and c.cod_fornec=a.cod_fornec
where adjudicado='S' and prc_compra='6011E2'
GROUP BY a.cod_fornec,b.nome_for,c.vl_ne
[/code]



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

psfaro
Starting Member

49 Posts

Posted - 2012-02-07 : 12:48:41
Hi VisaKh

Tanks Very much. Works Fine.

.. But MSSQL must be more smarter to agregate a Static Value,
Instead of need the Inner JOIN/Left Join ..

Regards

Pedro

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-07 : 13:03:33
Who said it was "Static"

And What's wrong with JOINS?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -