Foros del Web » Programación para mayores de 30 ;) » Bases de Datos General » SQL Server »

Como agrupar una columna subselect ? alias ?

Estas en el tema de Como agrupar una columna subselect ? alias ? en el foro de SQL Server en Foros del Web. Hola amigos estoy tratando de agrupar un alias pero no encuentro como hacerlo.. me gustaria por favor puedan ayudarme les gradeceria. a continuacion les muestro ...
  #1 (permalink)  
Antiguo 11/04/2012, 10:42
 
Fecha de Ingreso: enero-2011
Mensajes: 58
Antigüedad: 13 años, 3 meses
Puntos: 0
Pregunta Como agrupar una columna subselect ? alias ?

Hola amigos estoy tratando de agrupar un alias pero no encuentro como hacerlo.. me gustaria por favor puedan ayudarme les gradeceria.

a continuacion les muestro el codigo.

select '', '' AS MES,

(select top 1 case
WHEN RTRIM(caff.SALSTERR) between '10000' and '19999' THEN 'LA GRAN CARACAS'
WHEN RTRIM(caff.SALSTERR) between '20000' and '29999' THEN 'ZULIA-FALCON'
WHEN RTRIM(caff.SALSTERR) between '30000' and '39999' THEN 'CENTRO'
WHEN RTRIM(caff.SALSTERR) between '40000' and '49999' THEN 'LOS ANDES'
WHEN RTRIM(caff.SALSTERR) between '50000' and '59999' THEN 'LARA - LOS LLANOS'
WHEN RTRIM(caff.SALSTERR) between '60000' and '69999' THEN 'ORIENTE'
WHEN RTRIM(caff.SALSTERR) = '70000' THEN 'OFICINA'
end as ZONA FRom [SOP30200] caff where caff.SALSTERR = caf.SALSTERR group by caff.SALSTERR) as ZONA,


0 unidades_total,

cast(isnull(((select top 1 sum(quantity) FROM [SOP30300] d
inner join [SOP30200] fIn on d.SOPNUMBE = fIn.SOPNUMBE
where fIn.VOIDSTTS<>1
and fIn.SALSTERR = caf.SALSTERR
and fIn.SOPTYPE='3'
and fIn.SALSTERR between '10000' and '70000'
and fIn.DOCDATE between '01/01/2011' and '31/12/2011'
and d.[cmpntseq] not in ('16384','32768'))),0) -

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


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

where
caf.VOIDSTTS<>'1'
and de.[cmpntseq] not in ('16384','32768')
and (caf.SOPTYPE = '3' or caf.SOPTYPE = '4')
and( caf.SALSTERR between between '10000' and '79999')
and (caf.DOCDATE between '01/01/2011' and '31/12/2011')

group by caf.SALSTERR

el resultado q me da en la columna ZONA es el siguiente.
CENTRO
CENTRO
AMERICA
ZULIA
ZULIA

quisiera q apareciece
CENTRO
AMERICA
ZULIA

Gracias y espero puedan ayudarme.
  #2 (permalink)  
Antiguo 11/04/2012, 16:06
Avatar de Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 17 años, 8 meses
Puntos: 774
Respuesta: Como agrupar una columna subselect ? alias ?

Código SQL:
Ver original
  1. SELECT * FROM(
  2.  
  3. SELECT '', '' AS MES,
  4.  
  5. (SELECT top 1 CASE
  6. WHEN RTRIM(caff.SALSTERR) BETWEEN '10000' AND '19999' THEN 'LA GRAN CARACAS'
  7. WHEN RTRIM(caff.SALSTERR) BETWEEN '20000' AND '29999' THEN 'ZULIA-FALCON'
  8. WHEN RTRIM(caff.SALSTERR) BETWEEN '30000' AND '39999' THEN 'CENTRO'
  9. WHEN RTRIM(caff.SALSTERR) BETWEEN '40000' AND '49999' THEN 'LOS ANDES'
  10. WHEN RTRIM(caff.SALSTERR) BETWEEN '50000' AND '59999' THEN 'LARA - LOS LLANOS'
  11. WHEN RTRIM(caff.SALSTERR) BETWEEN '60000' AND '69999' THEN 'ORIENTE'
  12. WHEN RTRIM(caff.SALSTERR) = '70000' THEN 'OFICINA'
  13. END AS ZONA FROM [SOP30200] caff WHERE caff.SALSTERR = caf.SALSTERR GROUP BY caff.SALSTERR) AS ZONA,
  14.  
  15.  
  16. 0 unidades_total,
  17.  
  18. CAST(isnull(((SELECT top 1 SUM(quantity) FROM [SOP30300] d
  19. INNER JOIN [SOP30200] fIn ON d.SOPNUMBE = fIn.SOPNUMBE
  20. WHERE fIn.VOIDSTTS<>1
  21. AND fIn.SALSTERR = caf.SALSTERR
  22. AND fIn.SOPTYPE='3'
  23. AND fIn.SALSTERR BETWEEN '10000' AND '70000'
  24. AND fIn.DOCDATE BETWEEN '01/01/2011' AND '31/12/2011'
  25. AND d.[cmpntseq] NOT IN ('16384','32768'))),0) -
  26.  
  27. isnull(((SELECT top 1 SUM(quantity) FROM [SOP30300] d
  28. INNER JOIN [SOP30200] fIn ON d.SOPNUMBE = fIn.SOPNUMBE
  29. WHERE fIn.VOIDSTTS<>1
  30. AND fIn.SALSTERR = caf.SALSTERR
  31. AND fIn.SOPTYPE='4'
  32. AND fIn.SALSTERR BETWEEN '10000' AND '70000'
  33. AND fIn.DOCDATE BETWEEN '01/01/2011' AND '31/12/2011'
  34. AND d.[cmpntseq] NOT IN ('16384','32768'))),0) AS INT) TOTAL
  35.  
  36.  
  37. FROM SOP30200 caf
  38. INNER JOIN SOP30300 de ON caf.SOPNUMBE = de.SOPNUMBE
  39.  
  40. WHERE
  41. caf.VOIDSTTS<>'1'
  42. AND de.[cmpntseq] NOT IN ('16384','32768')
  43. AND (caf.SOPTYPE = '3' OR caf.SOPTYPE = '4')
  44. AND( caf.SALSTERR BETWEEN BETWEEN '10000' AND '79999')
  45. AND (caf.DOCDATE BETWEEN '01/01/2011' AND '31/12/2011')
  46.  
  47. GROUP BY caf.SALSTERR) AS t1 GROUP BY mes

algo asi :)
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me

Etiquetas: select
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 11:04.