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

Select case + count + 2 consultas

Estas en el tema de Select case + count + 2 consultas en el foro de SQL Server en Foros del Web. Buenas noches... Me podrian ayudar con esta consulta: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código SQL: Ver original SELECT   CASE   WHEN COUNT ( * ) = 0 ...
  #1 (permalink)  
Antiguo 21/08/2013, 23:54
 
Fecha de Ingreso: noviembre-2003
Mensajes: 209
Antigüedad: 21 años, 1 mes
Puntos: 0
Select case + count + 2 consultas

Buenas noches...

Me podrian ayudar con esta consulta:

Código SQL:
Ver original
  1. SELECT  CASE  WHEN COUNT(*) = 0 THEN
  2.  
  3. (SELECT     'Forecast:' AS Expr1, '0' AS Expr2, '0' AS Expr3, '0' AS Expr4, '0' AS Expr5, '0' AS Expr6, '0' AS Expr7, '0' AS Expr8, '0' AS Expr9, '0'
  4.                       AS Expr10, '0' AS Expr11, '0' AS Expr12, '0' AS Expr13
  5.                       UNION ALL
  6.                       SELECT     'PRQs:' AS Expr1, '0' AS Expr2, '0' AS Expr3, '0' AS Expr4, '0' AS Expr5, '0' AS Expr6, '0' AS Expr7, '0' AS Expr8, '0' AS Expr9, '0'
  7.                       AS Expr10, '0' AS Expr11, '0' AS Expr12, '0' AS Expr13
  8. UNION ALL
  9. SELECT     'Saldo:' AS Expr1, '0' AS Expr2, '0' AS Expr3, '0' AS Expr4, '0' AS Expr5, '0' AS Expr6, '0' AS Expr7, '0' AS Expr8, '0' AS Expr9, '0'
  10.                       AS Expr10, '0' AS Expr11, '0' AS Expr12, '0' AS Expr13)
  11.  
  12. ELSE
  13.  
  14. (SELECT     'Forecast:' AS Expr1, enero, febrero, marzo, abril, mayo, junio, julio, agosto, septiembre, octubre, noviembre, diciembre
  15. FROM         [tbl_forecast ]
  16. WHERE     (cuenta = 2000) AND (departamento = 65013) AND (anio = 2013)
  17. UNION ALL
  18. SELECT     'PRQs:' AS Expr1, '0' AS Expr2, '0' AS Expr3, '0' AS Expr4, '0' AS Expr5, '0' AS Expr6, '0' AS Expr7, '0' AS Expr8, '0' AS Expr9, '0'
  19.                       AS Expr10, '0' AS Expr11, '0' AS Expr12, '0' AS Expr13
  20. UNION ALL
  21. SELECT     'Saldo:' AS Expr1, '0' AS Expr2, '0' AS Expr3, '0' AS Expr4, '0' AS Expr5, '0' AS Expr6, '0' AS Expr7, '0' AS Expr8, '0' AS Expr9, '0'
  22.                       AS Expr10, '0' AS Expr11, '0' AS Expr12, '0' AS Expr13)
  23.                      
  24. END
  25. FROM         [tbl_forecast ]
  26. WHERE     (cuenta = 2000) AND (departamento = 65011) AND (anio = 2013)

Lo que necesito es: Si Count es = 0 entonces que ejecute una consulta.... si es igual a 1 entonces que ejecute la otra consulta....

Lo que tengo me manda un error, dice:

Msg 116, Level 16, State 1, Line 13
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Msg 116, Level 16, State 1, Line 25
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

¿que me falta o que me sobra?

Mcuhas gracias
  #2 (permalink)  
Antiguo 22/08/2013, 07:21
Avatar de Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 18 años, 4 meses
Puntos: 774
Respuesta: Select case + count + 2 consultas

lo que pasa es que en un case solo puedes mostrar un valor porque no usas algo como esto

if select count(*) from tu tabla=1
begin
query1
end
else
begin
query2
end

en lugar de tratar de ponerlo en un solo case?
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me
  #3 (permalink)  
Antiguo 22/08/2013, 11:06
 
Fecha de Ingreso: noviembre-2003
Mensajes: 209
Antigüedad: 21 años, 1 mes
Puntos: 0
Respuesta: Select case + count + 2 consultas

Excelente amigo Libras!, Eres un Master....

Quedo asi:

Código SQL:
Ver original
  1. IF (SELECT     COUNT(*) AS Expr1
  2. FROM         [tbl_forecast ]
  3. WHERE     (cuenta = 2000) AND (departamento = 65011) AND (anio = 2013)) = 1
  4. BEGIN
  5. SELECT     'Forecast:' AS Expr1, enero, febrero, marzo, abril, mayo, junio, julio, agosto, septiembre, octubre, noviembre, diciembre
  6. FROM         [tbl_forecast ]
  7. WHERE     (cuenta = 2000) AND (departamento = 65011) AND (anio = 2013)
  8. UNION ALL
  9. SELECT     'PRQs:' AS Expr1, '0' AS Expr2, '0' AS Expr3, '0' AS Expr4, '0' AS Expr5, '0' AS Expr6, '0' AS Expr7, '0' AS Expr8, '0' AS Expr9, '0'
  10.                       AS Expr10, '0' AS Expr11, '0' AS Expr12, '0' AS Expr13
  11. UNION ALL
  12. SELECT     'Saldo:' AS Expr1, '0' AS Expr2, '0' AS Expr3, '0' AS Expr4, '0' AS Expr5, '0' AS Expr6, '0' AS Expr7, '0' AS Expr8, '0' AS Expr9, '0'
  13.                       AS Expr10, '0' AS Expr11, '0' AS Expr12, '0' AS Expr13
  14. FROM         [tbl_forecast ]
  15. WHERE     (cuenta = 2000) AND (departamento = 65013) AND (anio = 2013)
  16. END
  17. ELSE
  18. BEGIN
  19. SELECT
  20. 'Forecast:' AS Expr1, '0' AS Expr2, '0' AS Expr3, '0' AS Expr4, '0' AS Expr5, '0' AS Expr6, '0' AS Expr7, '0' AS Expr8, '0' AS Expr9, '0'
  21.                       AS Expr10, '0' AS Expr11, '0' AS Expr12, '0' AS Expr13
  22.                       UNION ALL
  23.                       SELECT     'PRQs:' AS Expr1, '0' AS Expr2, '0' AS Expr3, '0' AS Expr4, '0' AS Expr5, '0' AS Expr6, '0' AS Expr7, '0' AS Expr8, '0' AS Expr9, '0'
  24.                       AS Expr10, '0' AS Expr11, '0' AS Expr12, '0' AS Expr13
  25. UNION ALL
  26. SELECT     'Saldo:' AS Expr1, '0' AS Expr2, '0' AS Expr3, '0' AS Expr4, '0' AS Expr5, '0' AS Expr6, '0' AS Expr7, '0' AS Expr8, '0' AS Expr9, '0'
  27.                       AS Expr10, '0' AS Expr11, '0' AS Expr12, '0' AS Expr13
  28.  
  29. END

Muchas gracias.

PD. aun te debo las frias.... no creas que se me ha olvidado.
  #4 (permalink)  
Antiguo 22/08/2013, 11:08
Avatar de Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 18 años, 4 meses
Puntos: 774
Respuesta: Select case + count + 2 consultas

que bien que todo funciono y de las frias ps espero que las tengas en el refri para que esten bien muertas jajajaja ;)

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

Etiquetas: case, count, 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 13:37.