Add column as identifier

0

Greetings, I am working with a table that does not have a column to be able to identify the rows, that is, it does not have a identity which causes me a problem because I need to obtain information from a column of each one of the records, someone could indicate me some sql function that I add a column listing all the records and can be used in a condition WHERE .

this is what I have:

CVE_C         CVE_E
54231           1
54236           1
54237           1
54238           1
54240           1

this is what I need:

ID  CVE_C         CVE_E
1   57117            1
2   57116            1
3   57115            1
4   57114            1
5   57113            1
    
asked by ARR 23.02.2018 в 22:36
source

1 answer

2

First, create a column that has a number. Here I chose a partition according to what I thought, but in reality you should check which one suits you.

SELECT 
  ROW_NUMBER() OVER(ORDER BY CVE_C) AS fila,
  CVE_C,
  CVE_E, 
FROM TuTabla

And then, put that select in another select, and keep the records you want:

SELECT s.*
FROM
    (SELECT 
      ROW_NUMBER() OVER(ORDER BY CVE_C) AS fila,
      CVE_C,
      CVE_E, 
    FROM TuTabla) As s
WHERE fila < 500

Beware that ROW_NUMBER () does not ensure that in all the runs the same number comes.

    
answered by 23.02.2018 / 23:37
source