Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > SQL
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 27-04-2004
Avatar de apicito
apicito apicito is offline
Miembro
 
Registrado: may 2003
Ubicación: Ourense
Posts: 341
Poder: 22
apicito Va por buen camino
Consulta que en función de un campo saque desglose o nombre del grupo

Utilizando Firebird 1.0 tengo dos tablas
Código:
CREATE TABLE SES'+Part+'GRU('+
  'SESGRU_CODIGO       N4 NOT NULL,'+
  'SESGRU_TIPO         CODIGO NOT NULL,'+
  'SESGRU_DESCRIP      DESCRIP,'+
  'SESGRU_T1           DESCRIP,'+
  'SESGRU_T2           DESCRIP,'+
  'SESGRU_COD1         CODIGO,'+
  'SESGRU_COD2         CODIGO,'+
  'SESGRU_COD3         CODIGO,'+
  'SESGRU_MOSTRAR      LOGICO,'+
  'SESGRU_ACTIVO       LOGICO,'+
  'primary key (SESGRU_CODIGO))';
Código:
CREATE TABLE SES'+Part+'ASU('+
    'SESASU_CODIGO       CODIGO NOT NULL,'+
    'SESASU_SESION       CODIGO NOT NULL,'+
    'SESASU_GRUPO        N4,'+
    'SESASU_ORDEN        CODIGO NOT NULL,'+
    'SESASU_ASUNTO       BLOB SUB_TYPE 1 SEGMENT SIZE 80,'+
    'SESASU_DEBATE       BLOB SUB_TYPE 1 SEGMENT SIZE 80,'+
    'SESASU_RESOLU       BLOB SUB_TYPE 1 SEGMENT SIZE 80,'+
    'primary key (SESASU_CODIGO),'+
    'foreign key (SESASU_SESION) references SES'+Part+'DAT(SESDAT_CODIGO)on delete cascade,'+
    'foreign key (SESASU_GRUPO) references SES'+Part+'GRU(SESGRU_CODIGO))';
Tengo que generar un listado de asuntos (SESASU) en el que si el grupo al que pertenece tiene el campo MOSTRAR a 1 liste el campo SESASU_ASUNTO, pero si el valor de SESGRU_MOSTRAR es 0 solo muestre el título del grupo y no el desglose de asuntos.
Podría hacerse esto con una SQL o tendré que hacer un recorrido manual por los asuntos e ir seleccionandolo sobre la marcha?
Responder Con Cita
  #2  
Antiguo 27-04-2004
Avatar de guillotmarc
guillotmarc guillotmarc is offline
Miembro
 
Registrado: may 2003
Ubicación: Huelva
Posts: 2.638
Poder: 24
guillotmarc Va por buen camino
Hola.

¿ Puedes actualizarte a Firebird 1.5 ? En esta caso seguramente te será útil poder hacer construcciones CASE dentro de una consulta.

select ..., (CASE WHEN SESGRU_MOSTRAR = 1 THEN SEASU_ASUNTO ELSE SESGRU_DESCRIP) as ASUNTO
from ...

NOTA: Consulta las Release Notes de Firebird 1.5 para ampliar la información de la construcción CASE. Aquí las puedes encontrar en castellano. http://www.ibphoenix.com/downloads/F...tesSpanish.pdf

Saludos.
__________________
Marc Guillot (Hi ha 10 tipus de persones, els que saben binari i els que no).
Responder Con Cita
  #3  
Antiguo 27-04-2004
Avatar de apicito
apicito apicito is offline
Miembro
 
Registrado: may 2003
Ubicación: Ourense
Posts: 341
Poder: 22
apicito Va por buen camino
He probado:
Código:
  with QueryAsu do
    begin
      SQL.Clear;
      SQL.Add('select SESASU_CODIGO,SESASU_GRUPO,SESASU_ORDEN,');
      SQL.Add('SESGRU_CODIGO,SESGRU_DESCRIP,SESGRU_MOSTRAR,');
      SQL.Add('(CASE WHEN SESGRU_MOSTRAR = 1 THEN SEASU_ASUNTO ELSE SESGRU_DESCRIP) as ASUNTO ');
      SQL.Add('from SES'+Part+'ASU A inner join SES'+Part+'GRU on SESGRU_CODIGO=A.SESASU_GRUPO ');
      SQL.Add('where SESASU_SESION='+#39+inttostr(SesCodigo)+#39);
      SQL.Add(' order by SESASU_GRUPO,SESASU_ORDEN');
      Open;
    end;
y algunas variantes despues de instalar el servidor 1.5 y me da error de ejecución. con esta versión del SQL concretamente, en la posición 68. Osea, en el "AS".
Responder Con Cita
  #4  
Antiguo 27-04-2004
Avatar de guillotmarc
guillotmarc guillotmarc is offline
Miembro
 
Registrado: may 2003
Ubicación: Huelva
Posts: 2.638
Poder: 24
guillotmarc Va por buen camino
Hola.

Creo que tienes que finalizar el CASE con un END.

O sea :
Código:
with QueryAsu do
    begin
      SQL.Clear;
      SQL.Add('select SESASU_CODIGO,SESASU_GRUPO,SESASU_ORDEN,');
      SQL.Add('SESGRU_CODIGO,SESGRU_DESCRIP,SESGRU_MOSTRAR,');
      SQL.Add('(CASE WHEN SESGRU_MOSTRAR = 1 THEN SEASU_ASUNTO ELSE SESGRU_DESCRIP END) as ASUNTO ');
      SQL.Add('from SES'+Part+'ASU A inner join SES'+Part+'GRU on SESGRU_CODIGO=A.SESASU_GRUPO ');
      SQL.Add('where SESASU_SESION='+#39+inttostr(SesCodigo)+#39);
      SQL.Add(' order by SESASU_GRUPO,SESASU_ORDEN');
      Open;
    end;
__________________
Marc Guillot (Hi ha 10 tipus de persones, els que saben binari i els que no).
Responder Con Cita
  #5  
Antiguo 27-04-2004
Avatar de apicito
apicito apicito is offline
Miembro
 
Registrado: may 2003
Ubicación: Ourense
Posts: 341
Poder: 22
apicito Va por buen camino
Faltaba el case, pero ahora me da:
Código:
Datatypes are not comparable in expression CASE
el campo SESGRU_MOSTRAR es un smallint default 0 check(value in (0,1))
Responder Con Cita
  #6  
Antiguo 27-04-2004
Avatar de guillotmarc
guillotmarc guillotmarc is offline
Miembro
 
Registrado: may 2003
Ubicación: Huelva
Posts: 2.638
Poder: 24
guillotmarc Va por buen camino
Hola.

¿ SEASU_ASUNTO y SESGRU_DESCRIP són del mismo tipo ?. En caso de ser de tamaño distinto, deberias forzar el mismo tamaño con un CAST. Ejplo. cast(SEASU_ASUNTO as varchar(100))

Saludos.
__________________
Marc Guillot (Hi ha 10 tipus de persones, els que saben binari i els que no).
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro


La franja horaria es GMT +2. Ahora son las 23:15:32.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi