Subquery with left Join

Asked

Viewed 136 times

0

I’m trying to search for records in a table where the product does not have certain characteristics. I would like to display only the records that do not have these characteristics, even if there are several others, I tried with the query below, it almost worked, rs. Thank you for your attention and collaboration.

select CODPROD
    from PRODUTOS
    left join (select CODPROD as CODPROD1, CARACT
                from PRODUTOS
                where DTFIM = '99991231'
                    and CARACT in ('X001','X002','X003')
                    and CODPROD <> ' '
                group by (CODPROD,CARACT))
     on CODPROD = CODPROD1
     group by CODPROD
  • I didn’t understand the need for a subquery, what exactly do you want to bring? It would be the CARACT in ('X001','X002','X003') or the denial of it?

1 answer

0

Analyzing your case I believe that it is not necessary to use the Subquery, but I would recommend creating the code as follows:

SELECT CODPROD, CARACT 
FROM PRODUTOS
WHERE CODPROD NOT IN (SELECT DISTINCT CODPROD
                  FROM PRODUTOS 
                  WHERE DTFIM = '99991231' 
                  AND CARACT IN ('X001','X002','X003')
                  AND CODPROD <> ' ')
GROYP BY CODPROD, CARACT

I used the Subquery in a Where seeking only the necessary information from CODPROD.

  • Thanks for the help, but it has not yet worked out, this way grouped by (CODPROD, CARACT) returns many records for the same product, need to return only 1, ie the product that does not have the features x001,X002 and x003.

  • And if you did AND CARACT NOT IN ('X001','X002','X003') since you don’t want to display these features?

  • I switched to NOT IN, take a test.

  • Thanks for the help, it worked with the following query: SELECT CODPROD FROM PRODUCTS WHERE CODPROD NOT IN (SELECT DISTINCT CODPROD FROM PRODUCTS WHERE DTFIM = '99991231' AND CARACT IN ('X001','X002','X003') AND CODPROD <> ' ') and CODPROD <> ' ' GROUP BY CODPROD

Browser other questions tagged

You are not signed in. Login or sign up in order to post.