Ver Mensaje Individual
  #1 (permalink)  
Antiguo 25/04/2012, 11:04
m1di
 
Fecha de Ingreso: enero-2011
Mensajes: 58
Antigüedad: 13 años, 3 meses
Puntos: 0
Pregunta Calculo de campo. y sintaxis de funcion rank

hola amigos del foro me gustaria saber si habra alguna posibilidad de clcular el total por mes y ademas saber como colocar una funcion rank o una sentencvia rank de esta manera con un select dfentro de la misma funcion rank(select). de igual manera para hacer la funcion me gustaria no usar una union de manera que para lo que quieo me generaria un problema.
SELECT datename(month,caf.DOCDATE), case datename(month,CONVERT(VARCHAR,caf.DOCDATE))

WHEN 'Enero' THEN 'A'
WHEN 'Febrero' THEN 'B'
WHEN 'Marzo' THEN 'C'
WHEN 'Abril' THEN 'D'
WHEN 'Mayo' THEN 'F'
WHEN 'Junio' THEN 'G'
WHEN 'Julio' THEN 'H'
WHEN 'Agosto' THEN 'I'
WHEN 'Septiembre' THEN 'J'
WHEN 'Octubre' THEN 'K'
WHEN 'Noviembre' THEN 'L'
WHEN 'Diciembre' THEN 'M'
end AS MES,

(select top 1 e.[CUSTNAME]from RM00101 e where e.[CUSTNMBR] = caf.[CUSTNMBR]) CLIENTE ,

DENSE_RANK () OVER
(PARTITION BY caf.CUSTNMBR ORDER BY caf.CUSTNMBR DESC) AS rank
,
cast(isnull(((select sum(quantity) FROM [SOP30300] d
inner join [SOP30200] fIn on d.SOPNUMBE = fIn.SOPNUMBE
where fIn.VOIDSTTS<>1
and fIn.DOCDATE = caf.DOCDATE
and fin.CUSTNMBR = caf.CUSTNMBR
and fIn.SALSTERR <> '80000'
and fIn.SOPTYPE='3'
and fIn.DOCDATE between '01/01/2011' and '31/12/2011'
and d.[cmpntseq] not in ('16384','32768'))),0) -

isnull(((select sum(quantity) FROM [SOP30300] d
inner join [SOP30200] fIn on d.SOPNUMBE = fIn.SOPNUMBE
where fIn.VOIDSTTS<>1
and fIn.DOCDATE = caf.DOCDATE
and fin.CUSTNMBR = caf.CUSTNMBR
and fIn.SALSTERR <> '80000'
and fIn.SOPTYPE='4'
and fIn.DOCDATE between '01/01/2011' and '31/12/2011'
and d.[cmpntseq] not in ('16384','32768'))),0) as int) unidades_total,

caf.CUSTNMBR as CUSTMER

from SOP30200 caf
inner join SOP30300 de on caf.SOPNUMBE = de.SOPNUMBE

where

caf.VOIDSTTS<>'1'
and (caf.SOPTYPE = '3' or caf.SOPTYPE = '4')
and caf.SALSTERR <> '80000'
--and caf.CUSTNMBR between @Cliente_Desde and @Cliente_Hasta
and (caf.DOCDATE between '01/01/2011' and '31/12/2011')
group by caf.CUSTNMBR, caf.DOCDATE
--order by total desc