Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

brunoguitarman

Preciso de com algo tipo um group by

Recommended Posts

Tenho um SQL aqui que é assim:

 

select a.nridebem, b.nmideden, a.nmmarbem, a.nmmodbem, nrserbem,
		  a.cdideunp, a.cdidesup, a.cdidelot, a.dsidebem, 
		  c.cdidecon, c.nmidecon, d.cdidesub, d.nmidesub, a.vlaqubem
from zcbp7100 a, zcbp7070 b, zcbp7050 c, zcbp7060 d
where a.multiusu = 720
  and a.multiusu = b.multiusu
  and a.cdideden = b.cdideden
  and c.multiusu = a.multiusu
  and c.cdidecon = b.cdidecon
  and d.multiusu = a.multiusu
  and d.cdidecon = b.cdidecon
  and d.cdidesub = b.cdidesub
  and a.cdideunp = 1
  and a.cdidesup = 1
  and a.cdidelot = 99990000
  and a.cdidebai = 0
order by c.cdidecon, d.cdidesub, a.nridebem, b.nmideden

Ele retorna algo do tipo

 

502	Teclado										   	NULL	NULL	NULL	1	1	99990000	NULL	5	Conta 05 - Equipamentos Diversos					  2	Subconta 02 - Informática						 	1000.0000
502	Teclado										   	NULL	NULL	NULL	1	1	99990000	NULL	5	Conta 05 - Equipamentos Diversos					  2	Subconta 02 - Informática						 	1000.0000
100000	Teclado										   	NULL	NULL	NULL	1	1	99990000	NULL	5	Conta 05 - Equipamentos Diversos					  2	Subconta 02 - Informática						 	1000.0000
4	Denominação 51										NULL	NULL	NULL	1	1	99990000	NULL	5	Conta 05 - Equipamentos Diversos					  5	Subconta 05									   	1000.0000
4	Denominação 51										NULL	NULL	NULL	1	1	99990000	NULL	5	Conta 05 - Equipamentos Diversos					  5	Subconta 05									   	1000.0000
4	Denominação 51										NULL	NULL	NULL	1	1	99990000	NULL	5	Conta 05 - Equipamentos Diversos					  5	Subconta 05									   	1000.0000
5	DENOMINAÇÃO 52										NULL	NULL	NULL	1	1	99990000	DESCRIÇÃO DO BEM NÚMERO 5. DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDFIM																																	5	Conta 05 - Equipamentos Diversos					  5	Subconta 05									   	1000.0000

O problema é que cada bem desses possui, além do número, uma incorporação. Assim sendo, como não estou, nem quero, selecionar a incorporação, bens como no caso do 4 podem repetir (por que o bem 4 tem as incorporações 0, 1 e 2, ou seja, seria o bem 4-0, 4-1 e 4-2). Só que não quero que ele repita, quero que nesses casos apareça só uma vez, mas somando o campo a.vlaqubem. No caso do bem 4, o campo a.vlaqubem ficaria como 3000.

 

Pensei em algo como um group by a.nridebem, mas deu o seguinte erro:

 

Server: Msg 8120, Level 16, State 1, Line 1
Column 'b.NMIDEDEN' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'a.NMMARBEM' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'a.NMMODBEM' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'a.NRSERBEM' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'a.CDIDEUNP' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'a.CDIDESUP' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'a.CDIDELOT' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'a.DSIDEBEM' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'c.CDIDECON' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'c.NMIDECON' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'd.CDIDESUB' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'd.NMIDESUB' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'a.VLAQUBEM' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Então tenho a impressão de que isso não vai dar muito certo :P.

 

Alguém tem alguma idéia pra me ajudar?

Compartilhar este post


Link para o post
Compartilhar em outros sites

Coloquei assim agora:

 

select a.nridebem, b.nmideden, a.nmmarbem, a.nmmodbem, nrserbem,
		  a.cdideunp, a.cdidesup, a.cdidelot, a.dsidebem, 
		  c.cdidecon, c.nmidecon, d.cdidesub, d.nmidesub, 
		  (select sum(vlaqubem) from zcbp7100 where multiusu = 720 and zcbp7100.nridebem = a.nridebem) as 'vlaqubem'
from zcbp7100 a, zcbp7070 b, zcbp7050 c, zcbp7060 d
where a.multiusu = 720
  and a.multiusu = b.multiusu
  and a.cdideden = b.cdideden
  and c.multiusu = a.multiusu
  and c.cdidecon = b.cdidecon
  and d.multiusu = a.multiusu
  and d.cdidecon = b.cdidecon
  and d.cdidesub = b.cdidesub
  and a.cdideunp = 1
  and a.cdidesup = 1
  and a.cdidelot = 99990000
  and a.cdidebai = 0
order by c.cdidecon, d.cdidesub, a.nridebem, b.nmideden

E pareceu dar certo. Alguém poderia me dar um feedback se tem algum erro de lógica aí?

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.