select join dentro de um case com select join

Jacqueline B.da Cruz 21 Reputation points
2021-03-17T20:20:53.59+00:00

Olá, Tudo bem ? Sou nova por aqui, pesquisei e não achei se é possível fazer um select case com condição, vou postar o código para mostrar o que preciso e vê se é possível, senão for me falem o que o que pode ser feito. select m.ArquivoOrdemMov,m.ContadorAgenteMov,m.DataMov,m.SeqOrdemMov,m.SeqProduto,m.SaldoTotalMov,p.DescricaoProduto ,case when m.ArquivoOrdemMov='OC’then ‘Possui preço de compra’ /*dentro do then preciso fazer um select join para pegar um preço que esta em uma tabela que faz join com a tabela de compras(OC) else 'Sem valor’end from Mov m join MovFisica f on m.SeqMovFisica=f.SeqMovFisica join OC c on m.ContadorAgenteMov=c.ContadorAgenteOC join Produto p on m.SeqProduto=p.SeqProduto where m.ArquivoOrdemMov=‘OC’ and c.SituacaoOC=‘FECHADA’

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,814 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,559 questions
{count} votes

Accepted answer
  1. MelissaMa-MSFT 24,176 Reputation points
    2021-03-23T02:25:26.587+00:00

    Hi @Jacqueline B.da Cruz ,

    Please refer below and check whether it is helpful.

    select  m.ArquivoOrdemMov,m.ContadorAgenteMov,m.DataMov,m.SeqOrdemMov,m.SeqProduto,m.SaldoTotalMov,p.DescricaoProduto   
     ,'Sem valor'  
     from Mov m   
     join MovFisica f on m.SeqMovFisica=f.SeqMovFisica  
     join MS s on m.ContadorAgenteMov=s.ContadorAgenteMS  
     join Produto p on m.SeqProduto=p.SeqProduto  
     where m.ArquivoOrdemMov='MS' and s.SituacaoMS='FECHADA'  
     union  
     select  m.ArquivoOrdemMov,m.ContadorAgenteMov,m.DataMov,m.SeqOrdemMov,m.SeqProduto,m.SaldoTotalMov,p.DescricaoProduto   
     ,'Sem valor'  
     from Mov m   
     join MovFisica f on m.SeqMovFisica=f.SeqMovFisica  
     join ME e on m.ContadorAgenteMov=e.ContadorAgenteME  
     join Produto p on m.SeqProduto=p.SeqProduto  
     where m.ArquivoOrdemMov='ME' and e.SituacaoME='FECHADA'  
     union  
     select  m.ArquivoOrdemMov,m.ContadorAgenteMov,m.DataMov,m.SeqOrdemMov,m.SeqProduto,m.SaldoTotalMov,p.DescricaoProduto  
    ,'Possui preço de compra'  
     from Mov m   
     join MovFisica f on m.SeqMovFisica=f.SeqMovFisica  
     join OC c on m.ContadorAgenteMov=c.ContadorAgenteOC  
     join Produto p on m.SeqProduto=p.SeqProduto  
     join OCItem ci on c.SeqOC=ci.SeqOC  
     where m.ArquivoOrdemMov='OC' and c.SituacaoOC='FECHADA'  
    

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


6 additional answers

Sort by: Most helpful
  1. Guoxiong 8,126 Reputation points
    2021-03-17T21:30:42.06+00:00

    ... dentro do then preciso fazer um select join para pegar um preço que esta em uma tabela que faz join com a tabela de compras(OC)

    Post the SELECT statement to get the price.


  2. MelissaMa-MSFT 24,176 Reputation points
    2021-03-18T02:15:31.717+00:00

    Hi @Jacqueline B.da Cruz

    Welcome to Microsoft Q&A!

    Please post this question in English so that all of us could undertand your requiremnet well and help you further.

    Besides, you could post your code by clicking below icon in red box.

    78983-code.png

    According to limited information you provided, please refer below example and check whether it is helpful.

    Suppose there is one column named price which is your purchase price from OC table.

    select m.ArquivoOrdemMov,m.ContadorAgenteMov,m.DataMov,m.SeqOrdemMov,m.SeqProduto,m.SaldoTotalMov,p.DescricaoProduto   
    ,case when m.ArquivoOrdemMov='OC'then (select c.price from OC c where m.ContadorAgenteMov=c.ContadorAgenteOC and c.SituacaoOC='FECHADA')   
    else 'Sem valor'end   
    from Mov m   
    join MovFisica f   
    on m.SeqMovFisica=f.SeqMovFisica   
    join Produto p on m.SeqProduto=p.SeqProduto   
    

    If above is not working, we recommend that you post CREATE TABLE statements for your tables together with INSERT statements with sample data, enough to illustrate all angles of the problem. We also need to see the expected result of the sample.

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Erland Sommarskog 101.9K Reputation points MVP
    2021-03-18T22:13:31.443+00:00

    Talvez:

    select m.ArquivoOrdemMov,m.ContadorAgenteMov,m.DataMov,m.SeqOrdemMov,m.SeqProduto,m.SaldoTotalMov,p.DescricaoProduto
         ,case when m.ArquivoOrdemMov='OC' then
              ( select ci.CustoOCItem from OCItem ci where ci.SeqOC=c.SeqOC)
         else 'Sem valor'end 
         from Mov m 
         join MovFisica f on m.SeqMovFisica=f.SeqMovFisica
         join OC c on m.ContadorAgenteMov=c.ContadorAgenteOC
         join Produto p on m.SeqProduto=p.SeqProduto
         where m.ArquivoOrdemMov='OC' and c.SituacaoOC='FECHADA'
    

    Mas são somente adivinhando...


  4. Guoxiong 8,126 Reputation points
    2021-03-19T14:17:08.727+00:00

    How about this:

    select m.ArquivoOrdemMov,
        m.ContadorAgenteMov,
        m.DataMov,
        m.SeqOrdemMov,
        m.SeqProduto,
        m.SaldoTotalMov,
        p.DescricaoProduto,
    case when m.ArquivoOrdemMov = 'OC' then ci.CustoOCItem else 'Sem valor' end
    from Mov m 
    join MovFisica f on m.SeqMovFisica=f.SeqMovFisica 
    join OC c on m.ContadorAgenteMov=c.ContadorAgenteOC 
    LEFT JOIN OCItem ci ON c.SeqOC = ci.SeqOC
    join Produto p on m.SeqProduto=p.SeqProduto 
    where m.ArquivoOrdemMov='OC' and c.SituacaoOC='FECHADA'