Select to records that do not contain value in table [closed]

2

I have the following table

Pedimento | Partida | Identificador | Valor Agregado
6000001 | 1 | TL | 10
6000001 | 1 | PT | 10
6000002 | 1 | TL | 0
6000002 | 2 | XP | 0
6000003 | 3 | PT | 10
6000003 | 4 | TL | 10

How can I perform a query that returns all the records that contain the PT identifier and those that in the Value Added field is greater than 0 and do not contain the PT identifier?

Note: Motion - Part would be our group

The result should be something like this

Pedimento | Partida | Identificador | Valor Agregado
6000001 | 1 | PT | 10
6000003 | 3 | PT | 10
6000003 | 4 | TL | 10

Issue: The value added in the first record was wrong, it was 10 instead of 0, the problem with the query to filter those with added value greater than 0 is that it will bring me groups (pedimento - partida) duplicated (Por example the pediment 6000001 item 1 will be duplicated because one of its registers does not have PT identifier), what I need you to return me are pediment - heading groups that do not have a pt identifier in any of the rows

    
asked by Javier Toscano 11.02.2017 в 06:54
source

1 answer

0

Your query, translated literally, would look something like this:

select * from TABLA 
where (identificador = 'PT') 
   or (identificador != 'PT' and Valor_Agregado > 0) 
group by pedimento, partida, identificador, valor_agregado

Now the second where has a redundant part as @Mariano mentions, since all the values containing PT are returned anyway. Optimizing it would look like this:

select * from TABLA 
where (identificador = 'PT') 
   or (Valor_Agregado > 0) 
group by pedimento, partida, identificador, valor_agregado

The key point is that you should make a OR in where so that it is conditional.

And as @mariano mentions, even if you want to group only two fields, you should put them all together, just taking care to put at the beginning the two fields that interest you.

Taking your example, the result of the query would be like this:

Pedimento | Partida | Identificador | Valor Agregado
6000001 | 1 | PT | 10
6000003 | 3 | PT | 10
6000003 | 4 | TL | 10
    
answered by 11.02.2017 в 07:18