Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   SQL (https://www.clubdelphi.com/foros/forumdisplay.php?f=6)
-   -   Consulta que en función de un campo saque desglose o nombre del grupo (https://www.clubdelphi.com/foros/showthread.php?t=9570)

apicito 27-04-2004 08:59:51

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?

guillotmarc 27-04-2004 13:29:53

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.

apicito 27-04-2004 17:05:58

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".

guillotmarc 27-04-2004 17:18:35

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;


apicito 27-04-2004 17:42:08

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))

guillotmarc 27-04-2004 18:22:58

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.

apicito 28-04-2004 09:02:24

he probado a hacer el cast que me propones y el siguiente:
Código:

(CASE WHEN SESGRU_MOSTRAR = 1 THEN SESASU_ASUNTO ELSE cast(SESGRU_DESCRIP as blob) END)
y me sigue dando error. En este segundo caso: "Datatypes are not comparables". De todas formas, gracias por tu ayuda, pero voy a cambiar de vía por que me he dado de cuenta que aunque funcionase esta opción al final me sacaría el nombre del grupo por cada asunto que y lo que necesito es que si existen varios asuntos me aparezca solo una vez el nombre del grupo.
1.000.000 de gracias.


La franja horaria es GMT +2. Ahora son las 17:47:07.

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