Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Sql group by, selected fields not equal to group by fields

Posted on 2011-03-17
4
Medium Priority
?
583 Views
Last Modified: 2012-05-11
I have this query and works well, but i need another field in  "from  (select entidade,moeda,sinal," line, but did not want to add more fields to group by " group by entidade,moeda,sinal ". Is possible

select MovDet.*, Cli.nome, Cli.morada, Cli.localidade,cli.CodigoPostal,cli.Telefone,
CONVERT(varchar, '1900-01-01', 102) AS Datadocumento, '' as numdocumento, 'Saldo Anterior' as Descritivo,
0.00 as saldoanterior,  '' as empresanome, '' as titulomapa, '' as criteriomapa, MovDet.entidade as Idstamp
from  (select entidade,moeda,sinal,
case
      when Sinal= 'C' and moeda= 'EURO' Then sum(Debito)
      when Sinal= 'C' and moeda <> 'EURO' Then sum(Debito/cotacao)
      else 0
end as debito,

case
      when Sinal= 'D' and moeda= 'EURO' Then sum(Credito)
      when Sinal= 'D' and moeda <> 'EURO' Then sum(Credito/cotacao)
      else 0
end as credito
from DetDocCCclientes
where
DetDocCCclientes.DataDocumento <'2011-03-17'
and
DetDocCCclientes.entidade >=200
and  
DetDocCCclientes.entidade <=200 group by entidade,moeda,sinal ) MovDet
left join clientes Cli  on Cli.CodigoCliente = MovDet.entidade
0
Comment
Question by:rflorencio
  • 2
4 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 400 total points
ID: 35157257
it depends ....
read this article to understand the issue and find the sql solutions:
http://www.experts-exchange.com/A_3203.html
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 35157440
as angeliii says it depends...

what do you want to also display?

you current query may be better written as

select MovDet.*, Cli.nome, Cli.morada, Cli.localidade,cli.CodigoPostal,cli.Telefone,
CONVERT(varchar, '1900-01-01', 102) AS Datadocumento, '' as numdocumento, 'Saldo Anterior' as Descritivo,
0.00 as saldoanterior,  '' as empresanome, '' as titulomapa, '' as criteriomapa, MovDet.entidade as Idstamp
from  (select entidade,moeda,sinal,
sum(case sinal when 'C' then debito / (case moeda when 'EURO' then 1.000000 else cotacao end) else 0 end) as debito
sum(case sinal when 'D' then credito / (case moeda when 'EURO' then 1.000000 else cotacao end) else 0 end) as credito

from DetDocCCclientes
where DetDocCCclientes.DataDocumento <'2011-03-17'
and DetDocCCclientes.entidade >=200
and  DetDocCCclientes.entidade <=200
group by entidade,moeda,sinal ) MovDet
left join clientes Cli
 on Cli.CodigoCliente = MovDet.entidade
0
 

Author Comment

by:rflorencio
ID: 35157866
in this line i need add another field (idstamp), but in this case i need add to gropu bt this field also, and i do not want.

from  (select entidade,moeda,sinal,idstamp
case
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 1600 total points
ID: 35158225
does this provide what you desire?

it would be easier to assist if you could tell us what it is that you are trying to display
and why you need to avoid adding into the group by clause...

some sample data/expected result would also assist....

an inital "standard" method would be to just add max(idstamp) as idstamp
to the select list....

did you read the article angeliii pointed you too?
select MovDet.*, Cli.nome, Cli.morada, Cli.localidade,cli.CodigoPostal,cli.Telefone,
CONVERT(varchar, '1900-01-01', 102) AS Datadocumento, '' as numdocumento, 'Saldo Anterior' as Descritivo,
0.00 as saldoanterior,  '' as empresanome, '' as titulomapa, '' as criteriomapa, MovDet.entidade as Idstamp
from  (select distinct entidade,moeda,sinal,idstamp
,sum(case sinal when 'C' then debito / (case moeda when 'EURO' then 1.000000 else cotacao end) else 0 end) 
 over (partition by entidade,moeda,sinal order by entidade) as debito
,sum(case sinal when 'D' then credito / (case moeda when 'EURO' then 1.000000 else cotacao end) else 0 end) 
 over (partition by entidade,moeda,sinal order by entidade) as credito

from DetDocCCclientes
where DetDocCCclientes.DataDocumento <'2011-03-17'
and DetDocCCclientes.entidade >=200
and  DetDocCCclientes.entidade <=200 
left join clientes Cli 
 on Cli.CodigoCliente = MovDet.entidade

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question