3
Situation:
In a table there is a list of items with their respective values invoiced in invoice. This table has a single field that varies according to the insertion of the data, can repeat all other columns and does not have a PK
. An example would be:
fk_nota_fiscal | fk_produto | valor | campo2 | campoXXX
244060 | 0010101 | 100,00| X | A
244060 | 0010101 | 100,00| X | B
244060 | 0010101 | 100,00| X | C
Desired:
I need to get only the first result regardless of the variation of the column campoXXX
.
Problem encountered:
Using the functions ROW_NUMBER()
and OVER()
, I grouped the results into one CTE
and I was able to select only the first result, but the performance is absurdly slow and unviable for a considerable amount of records.
ROW_NUMBER() OVER(PARTITION BY fk_nota_fiscal,fk_produto ORDER BY fk_nota_fiscal,fk_produto) AS ROWID
SELECT * FROM FROM CTE_vwItensRefaturar WHERE ROWID = 1
you own an Intel for
fk_nota_fiscal
andfk_produto
?– Tobias Mesquita
No, Tobymosque. I’m seeing about it and if it somehow optimizes performance.
– Marcelo de Andrade
See the possibility to create a pk or Indice clustered, envovendo fk_nota_fiscal, fk_product and other field (campoXXX? ). I don’t see how you get a significant gain by replacing your current strategy with one of the ones suggested in the answers (including, except for the suggestion given by @Onosendai, you get a loss instead of gain).
– Tobias Mesquita