Vitor Braz
Vitor Braz

Reputation: 1

Case in Sub Select SQL Server

I'm trying use CASE in a subquery. I need get a info id_request from my table table_1, but, if the value of my id is null, I need to bring id_request from table_2. Can somebody help me?

There is part of my query with the problem:

(case 
    when MAX(id_pedido) is null 
       then (select MAX(id_pedido) 
             from sf_vendas_online 
             where id_venda = sf_vendas.id) 
       else MAX(id_pedido) 
 end id_pedido) id_pedido,

There is my query...

SELECT * 
FROM (
    SELECT ROW_NUMBER() OVER (ORDER BY sf_vendas.id desc) as row,
        sf_vendas.*,P.nome_pessoa,P.sobrenome_pessoa,
        ISNULL((SELECT top 1 descricao_documento FROM sf_vendas_parcelas INNER JOIN sf_tipo_documento ON sf_tipo_documento.id = sf_vendas_parcelas.tipo_documento WHERE id_venda = sf_vendas.id),'CORTESIA') id_vendas_parcelas, 
        (select MAX(bol_data_parcela) from sf_vendas_boleto where id_venda = sf_vendas.id) bol_data_parcela, 
        (select MAX(id) from sf_vendas_boleto where id_venda = sf_vendas.id) bol_id, 
        (select SUM(quantidade) from sf_vendas_itens where id_venda = sf_vendas.id) quantidade, 
        (select SUM(valor_bruto) from sf_vendas_itens where id_venda = sf_vendas.id) valor_bruto, 
        (select SUM(valor_desconto) from sf_vendas_itens where id_venda = sf_vendas.id) valor_desconto, 
        (select MAX(data_pagamento) from sf_vendas_parcelas where id_venda = sf_vendas.id) data_pagameto, 
        (select SUM(valor_pago) from sf_vendas_parcelas where id_venda = sf_vendas.id) valor_pago, 
        (select sum(valor_bruto - valor_desconto) from sf_vendas_itens where id_venda = sf_vendas.id) valor_total,
        (select MAX(bol_valor) from sf_vendas_boleto where id_venda = sf_vendas.id) bol_valor, 
        -- CASE statement is this line:       
        (case when  MAX(id_pedido) is null then (select MAX(id_pedido) from sf_vendas_online where id_venda = sf_vendas.id) else MAX(id_pedido) end id_pedido) id_pedido,
        (select MAX(bol_nosso_numero) from sf_vendas_boleto where id_venda = sf_vendas.id) bol_nosso_numero
    FROM dbo.sf_vendas 
    INNER JOIN sf_pessoa P ON P.id = sf_vendas.pessoa_venda
) as x 
ORDER BY id desc

Upvotes: 0

Views: 59

Answers (3)

Joel Coehoorn
Joel Coehoorn

Reputation: 415840

This should help dramatically improve performance, by reducing the number of trips to the additional tables:

SELECT * 
FROM (
    SELECT ROW_NUMBER() OVER (ORDER BY sf_vendas.id desc) as row,
        sf_vendas.*,P.nome_pessoa,P.sobrenome_pessoa,
        ISNULL(
          (SELECT top 1 descricao_documento 
            FROM sf_vendas_parcelas 
            INNER JOIN sf_tipo_documento ON sf_tipo_documento.id = sf_vendas_parcelas.tipo_documento 
            WHERE id_venda = sf_vendas.id),
          'CORTESIA') id_vendas_parcelas, 

        vb.bol_data_parcela, vb.bol_id, vb.bol_valor, vb.bol_nosso_numero
        vi.quantidade, vi.valor_bruto, vi.valor_desconto, vi.valor_total,             
        vp.data_pagamento, vp.valor_pago,

        COALESCE(MAX(id_pedido),
            (SELECT MAX(id_pedido) 
             FROM sf_vendas_online 
             WHERE id_venda = sf_vendas.id) 
       ) end id_pedido

    FROM dbo.sf_vendas 
    INNER JOIN sf_pessoa P ON P.id = sf_vendas.pessoa_venda
    OUTER APPLY (
         SELECT MAX(bol_data_parcela) bol_data_parcela, MAX(id) bol_id, 
             MAX(bol_valor) bol_valor, MAX(bol_nosso_numero) bol_nosso_numero
         FROM sf_vendas_boleto
         WHERE id_venda = sf_vendas.id 
    ) vb
    OUTER APPLY (
        SELECT SUM(quantidade) quantidade, SUM(valor_bruto) valor_bruto, 
           SUM(valor_desconto) valor_desconto,  sum(valor_bruto - valor_desconto) valor_total
        FROM sf_vendas_itens
        WHERE id_venda = sf_vendas.id
    ) vi
    OUTER APPLY (
        SELECT MAX(data_pagamento) data_pagamento,  SUM(valor_pago) valor_pago
        FROM sf_vendas_parcelas 
        WHERE id_venda = sf_vendas.id
    ) vp
) as x 
ORDER BY id desc

I suspect you can further greatly improve things by using GROUP BY and simple LEFT JOIN operations, rather than OUTER APPLY, but I don't know enough about your system to attempt that.

Upvotes: 1

Vitor Braz
Vitor Braz

Reputation: 1

I resolved with the ISNULL function that is similar to coalesc

There is my code...

ISNULL((SELECT MAX(id_pedido) FROM sf_vendas_online where id_venda = sf_vendas.id),(SELECT MAX(id_pedido) FROM sf_vendas_boleto where id_venda = sf_vendas.id)) id_pedido, 

Upvotes: 0

Zaynul Abadin Tuhin
Zaynul Abadin Tuhin

Reputation: 31993

Use COALESCE() function it will return 1st non null value and use left join with two tables

   select COALESCE ( max(t1.id),max(t2.id))
   from t1 left join t2 on t1.id=t2.id

Upvotes: 0

Related Questions