Ir para conteúdo
Fórum Script Brasil
  • 0

EXIBIR TODO OS CLIENTES


Vinicius Schuh

Pergunta

Bom dia Amigos, tenho o seguinte código SQL:

SELECT     
    dbo.tblClientes_grupos.NM As Grupo, 
    dbo.tblAtendimento.CD_Cliente, 
    dbo.Clientes.NomeDoCliente As Cliente, 
    dbo.tblAtendimento.ID As Att, 
    Month(dbo.tblAtendimento.DT_Atendimento) As Mes_Att,
    dbo.tblAtendimento.DT_Atendimento,
    NM_Att = Case Month(dbo.tblAtendimento.DT_Atendimento) 
               When '1' Then 'JANEIRO' When '2' Then 'FEVEREIRO' When '3' Then 'MARÇO' When '4' Then 'ABRIL' When '5' Then 'MAIO' When '6' Then 'JUNHO' When '7' Then 'JULHO' 
               When '8' Then 'AGOSTO' When '9' Then 'SETEMBRO' When '10' Then 'OUTUBRO' When '11' Then 'NOVEMBRO' When '12' Then 'DEZEMBRO' 
             End,
    dbo.tblOrdens_servico.ID As OS, 
    Month(dbo.tblOrdens_servico.DT_Os) As Mes_OS,
              dbo.tblOrdens_servico.DT_Os,
    NM_OS = Case Month(dbo.tblOrdens_servico.DT_Os)
              When '1' Then 'JANEIRO' When '2' Then 'FEVEREIRO' When '3' Then 'MARÇO' When '4' Then 'ABRIL' When '5' Then 'MAIO' When '6' Then 'JUNHO' When '7' Then 'JULHO' 
              When '8' Then 'AGOSTO' When '9' Then 'SETEMBRO' When '10' Then 'OUTUBRO' When '11' Then 'NOVEMBRO' When '12' Then 'DEZEMBRO' 
            End,
    Externo = Case (dbo.tblOrdens_servico.TP_Externo) When '1' Then 'X'  End,
    Interno = Case (dbo.tblOrdens_servico.TP_Interno)  When '1' Then 'X'  End,
   ATT_Status = Case (dbo.tblAtendimento.TP_Status) When '1' Then 'ABE' When '2' Then 'AND' When '3' Then 'FEC' End,
    OS_Status = Case When (dbo.tblOrdens_Servico.TP_Aberta = 1) Then 'ABE' When (dbo.tblOrdens_Servico.TP_Andamento = 1) Then 'AND' 
                    When (dbo.tblOrdens_Servico.TP_Concluida = 1) Then 'FEC' End,    
    ATT_AbertoTotal = Case When (dbo.tblAtendimento.TP_Status = 1 Or dbo.tblAtendimento.TP_Status = 2) Then Count(dbo.tblAtendimento.CD_Cliente) End,
              ATT_FechadoTotal = Case When (dbo.tblAtendimento.TP_Status = 3) Then Count(dbo.tblAtendimento.CD_Cliente) End,                 
              OS_AbertoTotal = Case  When (dbo.tblOrdens_Servico.TP_Aberta = 1 Or dbo.tblOrdens_Servico.TP_Andamento = 1) Then Count(dbo.tblAtendimento.CD_Cliente) End,
    OS_FechadoTotal = Case When (dbo.tblOrdens_Servico.TP_Concluida = 1) Then Count(dbo.tblAtendimento.CD_Cliente) End,
              GETDATE() As DT_Imp
FROM        
              dbo.tblAtendimento 
INNER JOIN
              dbo.Clientes On dbo.tblAtendimento.CD_Cliente = dbo.Clientes.CódigoDoCliente 
LEFT JOIN
              dbo.tblClientes_grupos On dbo.Clientes.ID_Grupo = dbo.tblClientes_grupos.ID 
INNER JOIN
              dbo.Funcionários On dbo.tblAtendimento.CD_Tecnico = dbo.Funcionários.CódigoDoFuncionário 
LEFT JOIN
              dbo.tblOrdens_servico On dbo.tblAtendimento.ID = dbo.tblOrdens_servico.ID_Atendimento
WHERE
             (dbo.tblAtendimento.CD_Cliente Is Not Null) And (dbo.Funcionários.NomeDoFuncionário Is Not Null)
GROUP BY
             dbo.tblClientes_grupos.NM,
             dbo.tblAtendimento.CD_Cliente,
             dbo.Clientes.NomeDoCliente,
             dbo.tblAtendimento.ID, 
             Month(dbo.tblAtendimento.DT_Atendimento),
             Month(dbo.tblOrdens_servico.DT_Os),
             dbo.tblOrdens_servico.ID,
             dbo.tblAtendimento.CD_Tecnico,
             dbo.tblOrdens_servico.TP_Externo,
             dbo.tblOrdens_servico.TP_Interno,
             dbo.tblAtendimento.DT_Atendimento,
             dbo.tblOrdens_servico.DT_Os,
             dbo.Funcionários.NomeDoFuncionário,
             dbo.tblAtendimento.TP_Status,
             dbo.tblOrdens_Servico.TP_Aberta,
             dbo.tblOrdens_Servico.TP_Andamento,
             dbo.tblOrdens_Servico.TP_concluida             
ORDER BY 
             dbo.Clientes.NomeDoCliente asc,             
             dbo.tblAtendimento.DT_Atendimento asc

Onde, resumidamente, ira me listar as ordens de serviço(OS) e Atendimentos(ATT) que um determinado cliente possui no relatório...

Até então, estava ok mas, preciso mostrar todos os clientes, indiferente se tem OS/ATT ou não, sei que com o inner join ele só me trás apenas os que tem OS/ATT vinculados, como poderia fazer para que me liste todos os clientes, se tem OS/ATT ou não?

Obg

Link para o comentário
Compartilhar em outros sites

6 respostass a esta questão

Posts Recomendados

  • 0

Boa tarde Vinicius,

Não tenho as tabelas para fazer os testes, mas tente fazer desta forma:

SELECT    
    dbo.tblClientes_grupos.NM As Grupo,
    dbo.tblAtendimento.CD_Cliente,
    dbo.Clientes.NomeDoCliente As Cliente,
    dbo.tblAtendimento.ID As Att,
    Month(dbo.tblAtendimento.DT_Atendimento) As Mes_Att,
    dbo.tblAtendimento.DT_Atendimento,
    NM_Att = Case Month(dbo.tblAtendimento.DT_Atendimento)
               When '1' Then 'JANEIRO' When '2' Then 'FEVEREIRO' When '3' Then 'MARÇO' When '4' Then 'ABRIL' When '5' Then 'MAIO' When '6' Then 'JUNHO' When '7' Then 'JULHO'
               When '8' Then 'AGOSTO' When '9' Then 'SETEMBRO' When '10' Then 'OUTUBRO' When '11' Then 'NOVEMBRO' When '12' Then 'DEZEMBRO'
             End,
    dbo.tblOrdens_servico.ID As OS,
    Month(dbo.tblOrdens_servico.DT_Os) As Mes_OS,
              dbo.tblOrdens_servico.DT_Os,
    NM_OS = Case Month(dbo.tblOrdens_servico.DT_Os)
              When '1' Then 'JANEIRO' When '2' Then 'FEVEREIRO' When '3' Then 'MARÇO' When '4' Then 'ABRIL' When '5' Then 'MAIO' When '6' Then 'JUNHO' When '7' Then 'JULHO'
              When '8' Then 'AGOSTO' When '9' Then 'SETEMBRO' When '10' Then 'OUTUBRO' When '11' Then 'NOVEMBRO' When '12' Then 'DEZEMBRO'
            End,
    Externo = Case (dbo.tblOrdens_servico.TP_Externo) When '1' Then 'X'  End,
    Interno = Case (dbo.tblOrdens_servico.TP_Interno)  When '1' Then 'X'  End,
   ATT_Status = Case (dbo.tblAtendimento.TP_Status) When '1' Then 'ABE' When '2' Then 'AND' When '3' Then 'FEC' End,
    OS_Status = Case When (dbo.tblOrdens_Servico.TP_Aberta = 1) Then 'ABE' When (dbo.tblOrdens_Servico.TP_Andamento = 1) Then 'AND'
                    When (dbo.tblOrdens_Servico.TP_Concluida = 1) Then 'FEC' End,    
    ATT_AbertoTotal = Case When (dbo.tblAtendimento.TP_Status = 1 Or dbo.tblAtendimento.TP_Status = 2) Then Count(dbo.tblAtendimento.CD_Cliente) End,
              ATT_FechadoTotal = Case When (dbo.tblAtendimento.TP_Status = 3) Then Count(dbo.tblAtendimento.CD_Cliente) End,                
              OS_AbertoTotal = Case  When (dbo.tblOrdens_Servico.TP_Aberta = 1 Or dbo.tblOrdens_Servico.TP_Andamento = 1) Then Count(dbo.tblAtendimento.CD_Cliente) End,
    OS_FechadoTotal = Case When (dbo.tblOrdens_Servico.TP_Concluida = 1) Then Count(dbo.tblAtendimento.CD_Cliente) End,
              GETDATE() As DT_Imp
FROM       
              dbo.Clientes
LEFT JOIN
              dbo.tblAtendimento On dbo.tblAtendimento.CD_Cliente = dbo.Clientes.CódigoDoCliente
LEFT JOIN
              dbo.tblClientes_grupos On dbo.Clientes.ID_Grupo = dbo.tblClientes_grupos.ID
INNER JOIN
              dbo.Funcionários On dbo.tblAtendimento.CD_Tecnico = dbo.Funcionários.CódigoDoFuncionário
LEFT JOIN
              dbo.tblOrdens_servico On dbo.tblAtendimento.ID = dbo.tblOrdens_servico.ID_Atendimento
WHERE
             (dbo.tblAtendimento.CD_Cliente Is Not Null) And (dbo.Funcionários.NomeDoFuncionário Is Not Null)
GROUP BY
             dbo.tblClientes_grupos.NM,
             dbo.tblAtendimento.CD_Cliente,
             dbo.Clientes.NomeDoCliente,
             dbo.tblAtendimento.ID,
             Month(dbo.tblAtendimento.DT_Atendimento),
             Month(dbo.tblOrdens_servico.DT_Os),
             dbo.tblOrdens_servico.ID,
             dbo.tblAtendimento.CD_Tecnico,
             dbo.tblOrdens_servico.TP_Externo,
             dbo.tblOrdens_servico.TP_Interno,
             dbo.tblAtendimento.DT_Atendimento,
             dbo.tblOrdens_servico.DT_Os,
             dbo.Funcionários.NomeDoFuncionário,
             dbo.tblAtendimento.TP_Status,
             dbo.tblOrdens_Servico.TP_Aberta,
             dbo.tblOrdens_Servico.TP_Andamento,
             dbo.tblOrdens_Servico.TP_concluida            
ORDER BY
             dbo.Clientes.NomeDoCliente asc,            
             dbo.tblAtendimento.DT_Atendimento asc

Link para o comentário
Compartilhar em outros sites

  • 0

Bom dia Fulvio, mudei o relacionamento e consregui listas todos os clientes, apenas no meu relatório que não, mas agora é só uma questão de design mesmo.

FROM        
   dbo.Clientes
LEFT JOIN
    dbo.tblAtendimento On dbo.Clientes.CódigoDoCliente = dbo.tblAtendimento.CD_Cliente
LEFT JOIN
    dbo.tblClientes_grupos On dbo.Clientes.ID_Grupo = dbo.tblClientes_grupos.ID 
LEFT JOIN
   dbo.Funcionários On dbo.tblAtendimento.CD_Tecnico = dbo.Funcionários.CódigoDoFuncionário 
LEFT JOIN
   dbo.tblOrdens_servico On dbo.tblAtendimento.ID = dbo.tblOrdens_servico.ID_Atendimento

Abraço

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,1k
    • Posts
      651,8k
×
×
  • Criar Novo...