SQL Group same ID with another condition

1

I believe this is a somewhat specific question, because I do not find anything related yet.

I have a table named contas_produtos , which is a NxN relation between the contas and the produtos tables.

The table has the following columns:

ID (PK)
conta_id (FK)
produto_id (FK)
precoFinal (float)

Well, this precoFinal indicates the final value of a given product, which can be changed by the application, that is, two products can have the same id , but with values other than precoFinal . p>

What I want to do is to be able to group the products of the same id , but with equal final prices, and to separate those products from those that have id but different final prices.

I know that I can not simply group by precoFinal because I would pick products with different prices.

What I've done so far has been grouping by id , but it does not suit me, so it does not even pay for me to put my SQL here.

Much text, I know, but I think it would be easier to explain like this. Thanks

    
asked by anonymous 14.02.2017 / 14:46

2 answers

1

See if it works out

SELECT conta_id, 
       produto_id, 
       precofinal 
FROM   ((produtos 
         INNER JOIN contas_produtos 
                 ON id = conta_id) 
        INNER JOIN produtos 
                ON produto_id = produto_id ) 
GROUP  BY id, 
          precofinal 
    
14.02.2017 / 14:52
-2

Well, I apologize, I managed to solve it just by using group by with more than one column, as follows:

SELECT Count(*) AS quantidade, 
       conta_id, 
       produto_id, 
       precofinal 
FROM   conta_produtos 
GROUP  BY produto_id, 
          precofinal
    
14.02.2017 / 14:58